How to load changes in hooks.py?

I created an custom app today with some JS assets defined in hooks.py:

doctype_js = {
	"Contact" : "public/js/doctypes/contact.js",
	"Lead" : "public/js/doctypes/lead.js",
	"Quotation" : "public/js/doctypes/quotation.js",
	"Opportunity" : "public/js/doctypes/opportunity.js",
	}

This worked fine, until I added more assets in app_include_js. Something broke and now none of my assets are loaded anymore, even with app_include_js commented out. The hooks file looks like before.

The server is in developer mode. The app is installed:

user@server:~/frappe-bench$ bench --site sitename list-apps

frappe  13.19.0 version-13
erpnext 13.19.0 version-13
c_app   0.0.1   master

frappe.get_hooks("doctype_js") shows all assets correctly:

In [1]: frappe.get_hooks("doctype_js")
Out[1]:
{'Web Page': ['public/js/frappe/utils/web_template.js'],
 'Website Settings': ['public/js/frappe/utils/web_template.js'],
 'Address': ['public/js/address.js'],
 'Communication': ['public/js/communication.js'],
 'Event': ['public/js/event.js'],
 'Newsletter': ['public/js/newsletter.js'],
 'Contact': ['public/js/contact.js', 'public/js/doctypes/contact.js'],
 'Lead': ['public/js/doctypes/lead.js'],
 'Quotation': ['public/js/doctypes/quotation.js'],
 'Opportunity': ['public/js/doctypes/opportunity.js']}

I did bench restart and cleared the browser cache multiple times, but when opening one of the DocTypes defined above, the JS file is not loaded (confirmed in Chrome Dev Tools Sources and Console).

When the problems started I did bench migrate. Then I completely uninistalled and reinstalled the Custom App, but it’s still not working. The JS file paths are correct.

Here is a snippet of quotation.js:

frappe.ui.form.on('Quotation', {
    setup(frm) {
        console.log("Loaded JS Asset quotation.js");
    },
});

I am at a loss here, what could I have changed that the files are not loaded anymore? Is there anything I could check? The browser console shows no errors at all.

I rebooted the VM today which caused my assets to load correctly again. But the problem still persists: changes to hooks.py are not picked up. To confirm this I removed a file from doctype_js, but it is still loaded!

Here are all the commands I tried to get the system to reload the hooks file and serve the correct assets:

  • bench clear-cache
  • bench migrate
  • bench build
  • bench restart
  • clear browser cache / delete all cookies and website data

But the new hooks.py is not loaded. frappe.get_hooks("doctype_js") shows the correct assets, but when browsing to Quotation, the JS script is loaded even though I removed the hook.

There must be an answer to this question. Anyone?

What is the correct way to load changes to hooks.py?

I think I read something in the documentation about the order of load among hooks, custom app, etc. for css and js.
I don’t remember where so I can’t get the link. But maybe you can search in the frappe or erpnext documentation (maybe in customization section)

As far as I can tell, the correct way to do this is:

bench --site yoursite clear-cache

It also appears that restarting web services (or rebooting server) causes a clear-cache operation.

1 Like

@philippm @James_Houx @rahy After making any changes try clearing browser cache or it would be better if you open it in a incognito tab, so you can see the changes. It helped me when I was trying to make some css changes with hooks.

Sorry for replying to an old thread.

What helps me (in production mode) is to restart bench either by:

$ bench restart

or,

$ sudo supervisorctl stop all
$ sudo supervisorctl start all

Only then the changes in hooks.py are picked up