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

Allow service workers to import assets #9285

Merged
merged 9 commits into from Dec 13, 2023
Merged

Allow service workers to import assets #9285

merged 9 commits into from Dec 13, 2023

Conversation

Rich-Harris
Copy link
Member

@Rich-Harris Rich-Harris commented Mar 2, 2023

Closes #9256. I don't know if this is a good idea or not but thought I'd try. Basically, build.lib always results in a single JavaScript file with everything inlined, whereas build.rollupOptions gives us more flexibility. But I'm not sure if it creates other problems. For example, a dynamic import(...) will be inlined at the moment; with this PR it will keep the dynamic import (which won't work in a non-module service worker, and we can't use module service workers yet) and will inject Vite's module preloading logic, which won't work because it references document.

There might be a combination of configuration options that gives us the bits we need without the bits we don't — for now I'm just going to leave this here

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

@benmccann
Copy link
Member

and will inject Vite's module preloading logic, which won't work because it references document.

Vite has an option to disable that I believe. I also thought I'd mention that the PWA plugin just calls Rollup directly to build their service worker and doesn't use Vite to do it, so that's potentially another option - although it means options from the vite.config.js won't get used in that build, so I'm not necessarily sure it's better

@Rich-Harris
Copy link
Member Author

Options from vite.config.js (apart from define) already don't get used, because in general they don't make sense in this context. But using Rollup would mean we needed to add support for define and TypeScript and probably other things, so I'm not sure it's worth it unless absolutely necessary

Vite has an option to disable that I believe

Any idea what it's called? I can't find it

@benmccann
Copy link
Member

I think you can set build.modulePreload: false

@Rich-Harris
Copy link
Member Author

Oh, interesting, I thought that only related to <link rel="modulepreload">. Somewhat surprised that they're controlled by the same option. Tried it, it works

@Rich-Harris Rich-Harris marked this pull request as ready for review March 6, 2023 18:00
@Rich-Harris
Copy link
Member Author

This should get a test but that'll be easier after merging #9250, so it can use the same mechanism

@changeset-bot
Copy link

changeset-bot bot commented Mar 30, 2023

🦋 Changeset detected

Latest commit: f0aec4e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

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

@Rich-Harris
Copy link
Member Author

Added a test, I think this is ready to go

@Rich-Harris Rich-Harris added this to the 1.x milestone Dec 13, 2023
@benmccann
Copy link
Member

For example, a dynamic import(...) will be inlined at the moment; with this PR it will keep the dynamic import

Can we set a rollup option to inline the dynamic import? I imagine that's what lib is doing under the covers

@Rich-Harris
Copy link
Member Author

that was done in a34f855

@benmccann benmccann added feature request New feature or request service worker Stuff related to service workers and removed feature request New feature or request labels Dec 13, 2023
Copy link
Member

@benmccann benmccann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm as long as this works when base is set

@@ -64,16 +63,16 @@ export async function build_service_worker(
);

await vite.build({
base: assets_base(kit),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit suspicious that we were using the base before and are not now.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we use location.href, so the service worker is portable. this is more robust than letting vite hard-code stuff

@Rich-Harris Rich-Harris merged commit 40bb1e6 into master Dec 13, 2023
14 checks passed
@Rich-Harris Rich-Harris deleted the gh-9256 branch December 13, 2023 05:39
@github-actions github-actions bot mentioned this pull request Dec 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
service worker Stuff related to service workers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sw.js inline import ?url filecontent, it should be return a file url.
3 participants