Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Script Manager Output Caching #291

Closed
whitter opened this issue Sep 26, 2024 · 4 comments · Fixed by #294
Closed

Script Manager Output Caching #291

whitter opened this issue Sep 26, 2024 · 4 comments · Fixed by #294

Comments

@whitter
Copy link
Contributor

whitter commented Sep 26, 2024

The current caching of the Script Manager view output is causing issues when mixed with CSP Manager.

So for instance when wanting to apply CSP nonce for Google Tag Manger we would create a custom view to override the default GTM HeadBottom.cshtml view from Script Manager to added the extra bit of JS to configure the nonce.

When using CSP Manager this nonce creation is handled with a tag helper so when end up with a view template like this:

@model string
<!-- Google Tag Manager -->
<script csp-manager-add-nonce="true">(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
        new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
        j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
        'https://www.googletagmanager.com/gtm.js?id='+i+dl;var n=d.querySelector('[nonce]');
        n&&j.setAttribute('nonce',n.nonce||n.getAttribute('nonce'));f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','@Model');</script>
<!-- End Google Tag Manager -->

CSP Manager will then keep track of the creation of nonce on the HttpContext and its middleware will output the CSP response headers based on the state of the data in the context. With the caching of the view output in Script Manager, subsequent request do not create the context data that CSP Manager expects and no nonce is output in the CSP header.

Based on my understanding of the Script Manager code the caching could be removed (or toggled on/off with a setting) with no detrimental affect.

@patrickdemooij9
Copy link
Owner

Hi @whitter

Thanks for the report, though I am not sure if removing the cache would fix your issue here. I think only the output of the scripts are cached, not the whole view. So if you are overwriting the HeadBottom.cshtml your code shouldn't be cached.

But those are just my initial thoughts, I'll try to reproduce this myself and see if I can find a fix for it :)

@whitter
Copy link
Contributor Author

whitter commented Oct 15, 2024

Hi @patrickdemooij9,

Just calling in to see if you had a chance to reproduce the above and see if you need anymore information.

@patrickdemooij9
Copy link
Owner

Hi @whitter
Sorry for the late reply, been quite busy with the Umbraco 14 version. I was able to reproduce this issue and it was indeed caused by the caching. I've added a new property to disable the caching for the rendering, which should fix your issue. I am hoping to get this released with a new version in the coming weeks.

Thank you for the report!

@patrickdemooij9
Copy link
Owner

Hi @whitter
Sorry for the late wait, it totally slipped my mind to get this published. The newest version (3.6.1) now has this fix in place. You can now use the following app setting to control the caching:

"SeoToolkit": {
    "ScriptManager": {
      "DisableRenderCaching": true
    }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants