-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
fix: build service workers in IIFE format #11129
Conversation
Registration code uses classic format but SW is built in es module format. Build serviceworker in iife format.
🦋 Changeset detectedLatest commit: 8a7ac4d The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
I discussed this PR with the rest of the team and there was some concern that Vite shouldn't be including an |
I changed I'm not sure why you would build as an ES module but then register it as a classic JS file though? Shouldn't it be built as an IIFE format anyway until browser support is ready for service worker ES Modules? It seems like it is just working accidentally in the case that you haven't imported anything. |
An ES module and classic JS file should be indistinguishable if there are no exports. I think the question is why is there an |
oh, it looks like there's a merge conflict on this PR that will need to be addressed as well |
once the CI turns green again we can merge this as a workaround for vitejs/vite#15379 |
I didn't think about it, but we rely on ESM for dev mode. Still I think this code path is only called during build, so we're probably safe with this change still |
huh? it's the opposite, the service worker is only built in prod mode? looking at vitejs/vite#15379 (comment), it seems that we may be zeroing in on a solution. if we're close to one, i'd much rather adopt that rather than bloat out everyone's service workers unnecessarily, even if the overhead is relatively small |
i have a solution locally, will revert this PR and open a new one |
This reverts commit 5f052a3.
Yes, that's what I meant to write. I updated my comment above to make sense now |
* Revert "fix: build service workers in IIFE format (#11129)" This reverts commit 5f052a3. * better fix * changeset * remove format --------- Co-authored-by: Rich Harris <[email protected]>
Fixes #6654 for imported graphql package in service worker code.
The documented service worker registration code uses classic format in a production build but SW is built in es module format. Vite produces an
export
statement that breaks classic mode registration when at least some external packages are imported into service worker code (nostr-tools and graphql reported in linked issue). I've changed the vite build option for the service worker to use 'iife' format instead of 'es'. I've locally tested dev and production builds and things appear to be working normally. I'm not sure what further testing is needed for this PR to be acceptable.Reference: SW es module support is still missing in several major browsers including Firefox and Safari. source.
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpm changeset
and following the prompts. Changesets that add features should beminor
and those that fix bugs should bepatch
. Please prefix changeset messages withfeat:
,fix:
, orchore:
.