Repository navigation
fix(router): bundle service workers into a single classic script - #9227
Merged
Merged
Conversation
Service workers are registered as classic scripts, but the client build emitted them as ES modules importing shared chunks, so registering one that imported any module failed. The import paths were also relative to the chunk's original location in build/, which the rename to /service-worker.js broke. Inline the imported chunks into an IIFE before the rename. Fixes QwikDev#8075
🦋 Changeset detectedLatest commit: 93b34c6 The changes in this PR will be included in the next version bump. This PR includes changesets to release 8 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
The nested build wrapped inlined dynamic imports in Vite's __vitePreload again. Its error path uses window, which service workers lack, so a failing import threw a ReferenceError instead of its own error.
The bundle is already keyed by file name, and the e2e test now covers bundling before the rename.
This reverts commit b2012c3. It only changed the error a service worker reports when a module it loads with import() throws, and it relied on Vite's internal plugin names.
@qwik.dev/core
@qwik.dev/router
@qwik.dev/utils
eslint-plugin-qwik
create-qwik
@qwik.dev/optimizer
@qwik.dev/devtools
@qwik.dev/mcp
commit: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What is it?
Description
Fixes #8075.
Service workers are registered as classic scripts (
navigator.serviceWorker.register(url), notype: 'module'), but the client build emittedservice-worker.jsas an ES module chunk. As soon as the service worker imported anything, an npm package or a module shared with the app, the bundler split that code into its own chunk, and registration failed:A second problem: that import path is relative to the chunk's original location in
build/, and the chunk is renamed to/service-worker.jsafterwards, so the path would be wrong even with module service workers.This PR fixes both by inlining a service worker's imported chunks into a single IIFE in the client
generateBundle, before the rename. Service workers without imports are left untouched. The re-bundle runs on the already-built chunks with Vite'sbuild()(no new dependency) and uses the client build'sminifysetting.It also replaces the nested rename loop with a small
findEntryChunkhelper, so the service-worker lookup and the rename share it.Verification
bundle-service-worker.unit.ts: a static import and a dynamic import are each inlined, and the result runs as a classic script; a service worker without imports is unchanged. These tests failed before the implementation.pnpm vitest run packages/qwik-router: 809 tests pass.pnpm tsc.checkand eslint pass.src/routes/service-worker.tsimports an npm package and a module shared with a route, then registered the output in a real browser (Edge via Playwright):main:ServiceWorker script evaluation failedI didn't run the full e2e suites locally.
Checklist
pnpm changeSummary by CodeRabbit