This project is read-only.

Script.Require intermittently placing scripts in incorrect order

Topics: Customizing Orchard, Troubleshooting
Sep 4, 2013 at 6:08 PM
Edited Sep 4, 2013 at 6:11 PM
This is intermittent and only appears when we deploy in release mode. Apologies in advance for the difficulty.

We have a few Javascript dependencies specified in various ResourceManifest classes. In our layout.cs, we have Script.Require("jQuery").AtHead() as one of the first few lines of the file, appearing after some Script.Include directives. Here is an excerpt:

Orchard renders <script src="/Modules/Orchard.jQuery/Scripts/jquery-1.8.3.min.js" type="text/javascript"></script>
in the <head> section as expected, but intermittently, and even without us deploying new files, the jQuery <script> line is inserted after other scripts that expect jQuery. As a result, jQuery is not defined in the correct order, resulting in Javascript errors.

We have not seen this behavior when the project is compiled in debug mode. And it is not consistent in release mode. And like I said, it happens intermittently even when we have not deployed or altered files in our production environment.

Are there recommended workarounds in this case? Has anyone seen this behavior?
Sep 4, 2013 at 6:22 PM
What version of Orchard are you using?
Sep 4, 2013 at 6:36 PM
We are using 1.7.
Sep 4, 2013 at 6:42 PM
Please file a bug.
Sep 4, 2013 at 6:43 PM
Thats odd... The jQuery version bundled with Orchard 1.7 is 1.9.1... are you sure you are using Orchard 1.7?
Sep 4, 2013 at 6:44 PM
Good point. You may have an incomplete upgrade here.
Sep 4, 2013 at 11:46 PM
I apologize for the confusion. We use 1.7 on a different project, but this error occurs in our Orchard 1.6 project.

Is it perhaps a known issue that was fixed in 1.7?

Also, we found that the issue can be (at least temporarily) resolved by changing and saving the Resource Debug setting in Settings on the dashboard.
Sep 5, 2013 at 12:29 AM
This issue was found in 1.6 quite some time ago and fixed in a subsequent release. Can you upgrade to Orchard 1.6.3? or even 1.7?

If not let me know and I will see if I can hunt down the patch.
Sep 5, 2013 at 12:58 AM
Edited Sep 5, 2013 at 1:01 AM
Thanks, Jetski5822. That is great information to know!

We can't upgrade to 1.7 quite yet due to the size of our existing project. But we can consider upgrading to 1.6.3.

If the patch is easy to find and apply, I'd love to get my hands on it, though. That would be ideal.
Sep 5, 2013 at 1:34 AM
Edited Sep 5, 2013 at 1:41 AM
@Nick, this was a pretty complex issue which actually needed multiple fixes. I pushed the last one somewhere around 1.7 RC which finally solved the issue once and for all (a multi-threading bug that was extremely hard to repro).

@ayitey Applying a patch can help but first, try to change a few things slightly and see if that helps. Checklist:
  • avoid using dashes and other non-alphanumeric characters in resource names
  • make sure resource names are different from any existing script/stylesheet filenames. Eg. prefix them with something.
  • do not put resource files in subfolders - everything needs to be directly under /Scripts or /Styles. This is very important - Orchard does not scan subfolders when discovering resource shapes which in effect makes all calls to Script.Require("MyResFromSubfolder") behave like Script.Include(...). This issue has been discussed a few times on forums.
And please be aware that all scripts/styles included with .Include, will always render before anything included with .Require. Try not to mix them. There is a slightly different rendering mechanism between those two (Requires' rendering is postponed whereas Includes' are written directly to the response stream).
Sep 5, 2013 at 1:52 AM
Thanks, pszmyd. We will try all of these things. Very much appreciate your help!!