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

How to handle nonce on entry.worker.js? #259

Open
TimoWestland opened this issue Sep 9, 2024 · 2 comments · Fixed by #269
Open

How to handle nonce on entry.worker.js? #259

TimoWestland opened this issue Sep 9, 2024 · 2 comments · Fixed by #269
Assignees
Labels
blocked Issues blocked by other tasks or dependencies roadmap Issues planned for future development suggestion Ideas or proposals for new features or improvements

Comments

@TimoWestland
Copy link

I was following the steps in the offline guide using: https://remix-pwa.run/docs/main/offline which is using the epic-stack.

However, the epic stack uses a nonce on all scripts for the CSP. This nonce is not added to the entry.worker.js file that is created, which is causing this error to be logged to the console, and afaict, prevents the service worker from registering.

image

Is there a way to add the nonce to this service worker script?

@ShafSpecs
Copy link
Member

ShafSpecs commented Sep 18, 2024

I was following the steps in the offline guide using: https://remix-pwa.run/docs/main/offline which is using the epic-stack.

However, the epic stack uses a nonce on all scripts for the CSP. This nonce is not added to the entry.worker.js file that is created, which is causing this error to be logged to the console, and afaict, prevents the service worker from registering.

image Is there a way to add the nonce to this service worker script?

@TimoWestland I didn't add support to modify the script injection, but you can disable the auto-script injection and use it yourself. In your vite.config.ts, set registerSW to null:

remixPWA({
  registerSW: null
})

And then, you can add the script to your root manually:

[
"<script type='module' id='vite-plugin-remix-pwa:loader::inject-sw' dangerouslySetInnerHTML={{",
' __html: `',
' async function register() {',
` const reg = await navigator.serviceWorker.register('/${ctx.options.workerName}.js', {`,
` scope: ${JSON.stringify(ctx.options.scope)},`,
" type: 'classic',",
" updateViaCache: 'none',",
' })',
'',
' window.$ServiceWorkerHMRHandler$ = async () => {',
' await reg.update();',
' }',
' }',
'',
" if ('serviceWorker' in navigator) {",
" if (document.readyState === 'complete' || document.readyState === 'interactive') {",
' register();',
' } else {',
" window.addEventListener('load', register);",
' }',
' }',
' `}}',
'/>',
'</head>',
]

scope here is /
workerName is entry.worker

I would work on extending the script support tho 👍!

@ShafSpecs ShafSpecs self-assigned this Sep 18, 2024
@ShafSpecs ShafSpecs added bug Something isn't working enhancement New feature or request labels Sep 18, 2024
@ShafSpecs
Copy link
Member

Thanks for your suggestion ❤️! I am taking this into consideration for the next major roadmap, including shipping a component to do just this

@ShafSpecs ShafSpecs added suggestion Ideas or proposals for new features or improvements roadmap Issues planned for future development and removed bug Something isn't working enhancement New feature or request labels Sep 26, 2024
@ShafSpecs ShafSpecs linked a pull request Oct 5, 2024 that will close this issue
@ShafSpecs ShafSpecs added the blocked Issues blocked by other tasks or dependencies label Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked Issues blocked by other tasks or dependencies roadmap Issues planned for future development suggestion Ideas or proposals for new features or improvements
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants