Bug #13623
openJavascript executing twice on application startup
0%
Description
I noticed that javascript code is executed twice during application startup when calling WApplication::doJavaScript("...", false)
before WApplication::require
is called. I added a small example to demonstrate the issue and a patch to fix it.
Best,
Steven
Files
Updated by Matthias Van Ceulebroeck about 1 month ago
- Target version set to 4.12.1
Thanks for the report, and the example, Steven.
I'm not too sure this is the correct fix, as I suspect a reset may need to happen inside WApplication
itself, once it has streamed the JS that is to be executed before the load.
The fact that the WebRenderer
is touching WApplication
state is a little suspect though. I'll schedule this for 4.12.1.
Best,
Matthias
Updated by Steven Köhler about 1 month ago
Hi Matthias,
I think it actually kind of does reset once the javascript is streamed: In WApplication::streamBeforeLoadJavaScript
, line 1712 in WApplication.c
, there is a command newBeforeLoadJavaScript_ = 0;
. The reset to zero should ensure that only javascript added after the streaming is taken into account the next time the javascript is streamed. According to my tests with the debugger, this works as intended. It is actually the line removed in the patch that is messing with WApplication
's internal bookkeeping, as it basically overrides it. I do agree that it is suspicious that WebController
changes the applications internal state.
Please correct me if I'm wrong: I suspect that javascript added before a call to require
is supposed to be executed before the required library is loaded, otherwise there would be no real reason to create a ScriptLibrary
instance for each required library, which explicitly contains all "before javascript". My tests with the fixed version shows the following output (the same issues exists with the unfixed version):
js1
js2
this is executed twice
this is executed twice, too
this is not
This assumes the js1.js and js2.js files do acutally exist and contain a simple console.log
command. It seems that the "before javascript" is executed after the javascript from the loaded libraries, which opposes the expected behavior. Maybe you could take a look into that as well, when you look into the initial issue.
Best,
Steven
Updated by Matthias Van Ceulebroeck about 1 month ago
Hey Steven,
Ok, thank you for the additional information.
It does indeed seem that more is wrong here. The expected output ought to be:
this is executed twice
js1.js
this is executed twice, too
js2.js
this is not
so I will investigate more when the ticket will be implemented!