oqtane.framework: [BUG] Module resources don't seem to load when navigating to a page with the module
Oqtane Info
Version - 5.1.2 Render Mode - Static Database - SQL Server
Describe the bug
A module can describe resources to load - JS/CSS. The idea is that these are automatically added to the page by Oqtane.
But on our tests, it only appears to work if the first full load happens on the page with the module.
Steps To Reproduce
So in our tests, this works:
- module on page About-Us
- Fully load page About us
- all is ok
…but this fails:
- Module is on page About us
- fully load page home
- browse to About us
Resources seem to not load.
Anything else?
The module is configured to have RenderMode.Static.
@tvatavuk just fyi
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 1
- Comments: 33 (33 by maintainers)
@iJungleboy it is not clear if you are saying that the JavaScript references are not being added to the DOM, or if they are not being executed (ie. calling onload), or both.
The fact that Blazor does not call onload when navigating from page to page has been well documented previously - it is due to Blazor’s “enhanced navigation” which I covered in the “building beautiful websites” webinar I presented 2 months ago (based on the Arsha theme). In scenarios where you need JavaScript to execute on navigation you need to hook the onupdated event.
Can you please provide more information on your specific scenario.
If this is for js files, you can make your js script to have below functions:
then register your js file with below format:
in this case your code will be executed every time navigate to the page.
This would actually be a really nice addition for the developer, as I was also getting a bit confused with javascript in static ssr (see #4038). Although you would still need to implement the onLoad, onUpdate and onDispose events when required, so the problem does not totally disappear.
One other thing I want to explore is if it is really necessary to force developers to include the Reload = True property in their Resources declaration. Instead, the framework could determine that the site is using Static rendering and use the page-script approach by default for Script resources. This would automatically wire up the onLoad, onUpdate, onDispose events if they exist - and if a JavaScript library does not utilize them, it should not cause any issues. This would simplify the experience for developers as they would not need to worry about setting an extra property which is only applicable for a specific render mode.
@tvatavuk I reached out to Mackinnon Buck and he explained that that page-script custom element can include additional attributes - he created an example here:
https://github.com/MackinnonBuck/blazor-page-script/commit/44ad6833b8375a9eaa9b0fa9ace70e024b823f2d
The most important part is that the attribute rel=“modulepreload” needs to be included so that the dynamic module imports will respect the attributes set on the link element:
https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/modulepreload
Oqtane will need to be modified to include the additional attributes - and then it will be able to support remote resources.
@tvatavuk thank you for confirming that Oqtane is injecting the reference to the JavaScript resource into the page DOM - this means that Oqtane is behaving properly. The fact that the JavaScript was not being executed is not an Oqtane issue - it is related to Blazor SSR and how Enhanced Navigation behaves in .NET 8. There is plenty of information available online about this behavior (ie. https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/static-server-rendering?view=aspnetcore-8.0). As explained in the Building Beautiful Websites webinar, Oqtane is utilizing a solution created by Mackinnon Buck from the Blazor Dev Team which enables JavaScript navigation callbacks (https://github.com/MackinnonBuck/blazor-page-script). If this solution does not work for you, you are free to use your own custom solution.
just to clarify what @tvatavuk said - turnOn doesn’t ensure that the JS is loaded into the browser, it’s job is to determine when all the requirements of a script are given, and then start some code.
A bit like a browser onLoad, but in a way that can handle late-added scripts, complicated dependencies, and CSP safe since it’s configuration only, meaning that it can be used even in very strict CSP scenarios. CSP = Client Security Policy. because it can receive parameters as JSON in the source code - which doesn’t require CSP fingerprinting.
@sbwalker thx for looking into this. The module has
If it’s on the initially loaded page - eg. ctrl-F5 - it’s available.
If the user starts on another page, and hen navigates to the page with the module, it’s not.
This is using SSR.
If we need to call some event to make it happen, no problem.
Note btw that we also needed some trickery to even detect if the page is being loaded “initial” or on a subsequent “magic navigation” effect (to try to implement a workaround in that scenario). We figured it out, but I think we’re reading some special HTTP headers for this.
If it turns out that other modules need this regularly, I also recommend having some official property to detect this scenario.
@zyhfish that’s good for code that must run every time, but not for external files that should be loaded - especially larger code.