Skip to content

[fix] service-worker builder constant replacement #7140

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

Merged
merged 3 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chilled-suns-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Apply define config to service worker
2 changes: 1 addition & 1 deletion documentation/docs/10-service-workers.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ In SvelteKit, if you have a `src/service-worker.js` file (or `src/service-worker

> You can change the [location of your service worker](/docs/configuration#files) and [disable automatic registration](/docs/configuration#serviceworker) in your project configuration.

Inside the service worker you have access to the [`$service-worker` module](/docs/modules#$service-worker).
Inside the service worker you have access to the [`$service-worker` module](/docs/modules#$service-worker). If your Vite config specifies `define`, this will be applied to service workers as well as your server/client builds.

Because it needs to be bundled (since browsers don't yet support `import` in this context), and depends on the client-side app's build manifest, **service workers only work in the production build, not in development**. To test it locally, use `vite preview`.
3 changes: 2 additions & 1 deletion packages/kit/src/exports/vite/build/build_service_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { assets_base } from './utils.js';
* @param {import('vite').Manifest} client_manifest
*/
export async function build_service_worker(
{ config, manifest_data, output_dir, service_worker_entry_file },
{ config, vite_config, manifest_data, output_dir, service_worker_entry_file },
prerendered,
client_manifest
) {
Expand Down Expand Up @@ -79,6 +79,7 @@ export async function build_service_worker(
outDir: `${output_dir}/client`,
emptyOutDir: false
},
define: vite_config.define,
configFile: false,
resolve: {
alias: {
Expand Down
3 changes: 3 additions & 0 deletions packages/kit/test/prerendering/basics/src/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if (process.env.MY_ENV === 'MY_ENV DEFINED') {
console.log(process.env.MY_ENV);
}
5 changes: 5 additions & 0 deletions packages/kit/test/prerendering/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,9 @@ test('injects relative service worker', () => {
assert.ok(content.includes(`navigator.serviceWorker.register('./service-worker.js')`));
});

test('define service worker variables', () => {
const content = read('service-worker.js');
assert.ok(content.includes(`MY_ENV DEFINED`));
});

test.run();
4 changes: 4 additions & 0 deletions packages/kit/test/prerendering/basics/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const config = {

plugins: [sveltekit()],

define: {
'process.env.MY_ENV': '"MY_ENV DEFINED"'
},

server: {
fs: {
allow: [path.resolve('../../../src')]
Expand Down