You want the script rendered when editors views the public pages? Or only when they browse any parts of the CMS UI?
Hi Anders,
If you want to follow a more Optimizely-based approach rather than using middleware, I would suggest exploring a protected Shell module with a client module initializer. Optimizely documents module initializers as the extension point for running JavaScript when a client module is initialized:
https://docs.developers.optimizely.com/content-management-system/docs/adding-a-module-initializer
Configure the client module to run after the Optimizely Shell module rather than depending only on the CMS module. The CMS dependency is primarily associated with the edit interface, whereas the Shell module may provide a broader loading context for the authenticated Optimizely UI.
In module.config you could try:
<moduleDependencies>
<add dependency="Shell" type="RunAfter" />
</moduleDependencies>
You can then register the JavaScript as a required client resource and execute the logic from the module initializer’s initialize() method.
Since the Optimizely UI behaves as a single-page application, the initializer will normally run when the module is loaded. If the script must execute after every internal navigation, you may additionally need to listen for route or view changes.
I would validate the Shell dependency across Dashboard, Reports, Settings and Add-ons, as the initializer documentation explains the extension mechanism but does not explicitly guarantee that one module will load in every authenticated view.
Hey,
Your module approach almost worked – you just missed one thing: type="Dojo" in the module.config.
Do this:
1. Create module.config in your site root (or add to existing one):
<module>
<clientModule>
<add name="epi.globalhack" path="ClientResources/global-hack.js" type="Dojo" />
</clientModule>
</module>
2. Create global-hack.js in wwwroot/ClientResources/ with this:
require(['epi'], function(epi) {
console.log('CMS script loaded everywhere');
// Your custom code here
});
That's it. The type="Dojo" is the key – it loads your script across the entire CMS UI, not just Edit view.
Works on CMS 12, no C# code needed.
I would like a small JavaScript to be executed every time a content manager loads a page while being logged in (i.e. not for pages rendered for end users). I have tried to do this via a module (module.config + JS file with and without Dojo) both under modules\_protected and in root/wwwroot\ClientResources). I (believe) that I got this working for pages in "Edit", but not for "Dashboard", "Reports", "Settings", "Add-ons" etc. I have asked a number of LLMs for advice but so far with no luck.
I have an alternative solution based on a middleware that seems to work. However, this feels a bit like a hack and I cannot help to think that there must be a better, more Optimizely based way to do this. Any suggestions on how to go about appreciated.
We are using CMS 12.