Hi,
I have noticed that my custom scripts have started failing to execute occasionally when loading forms. Below are the warnings that I get in the console when the issue occurs:
[Violation] 'load' handler took 200ms
[Violation] Forced reflow while executing JavaScript took 47ms
I’m at a complete loss for ideas to resolve this issue. I would greatly appreciate any help that can be provided.
It sounds like your scripts are drawing too many resources to process effectively. Can you share the script? We could help troubleshoot more effectively if you did.
The first thing that jumps out at me is the setTimeout in your refresh function. I don’t know much offhand about how load functions work, but it seems possible that the timeout is interrupting rendering as per your error message.
I’ve never needed to use a timeout to use default action buttons. Are you sure it’s necessary here? If you remove it, do you still get the error?
I had put in the timeout because without it the buttons were being rendered after the functions to remove the built in buttons are executed. Perhaps I should look for a different trigger to execute those functions on. I just went with the timeout as that was the suggested method that I found on the forums.
One thing to add is that the problem has only started popping up recently. The most recent change is the frappe.require() method at the top, as I have moved all of the common functions across my scripts to library files. They do not contain any code that was not already in the scripts.
Ah, yes, I didn’t notice that before, but I’d strongly suspect that’s the issue. I don’t know much about frappe.require, but I assume it’s a wrapper around the nodejs method. If that’s true, it would explain the error you’re getting. If you want to include a library, probably better to do it the old fashioned way with hooks.
Alright, I will try using hooks instead of frappe.require() and I will get back to you. It might be that using frappe.require() is too much for the handler when used for the entire form as the examples used in the documentation indicate it being used inside of events and functions.
Hi @peterg. I’m sorry that it has taken me so long to get back to you- I haven’t had the chance to try using the hooks like we discussed until now. I am struggling a bit with the hooks and I was hoping that you might know the solution:
When I use the hooks to extend the the form script it works fine but if I try to hook in a script that just has functions and call the functions from a client script, I get a reference error stating that the function is undefined.
I found my solution by tinkering- I tried doing normal imports but that fails because the scripts aren’t hooked in as modules. The solution is frappe.require() but it has to be placed at the top, then continue with your script below it. An example using the code sample that I provided above: