From 9dc5c0e3e01a3c07010e9996688169be68e1dde8 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Wed, 15 Jan 2025 17:04:03 +0100 Subject: [PATCH] fix: service worker base path in dev mode (#12577) Fixes #12039 --- .changeset/nice-geese-serve.md | 5 +++++ packages/kit/src/exports/vite/dev/index.js | 2 +- packages/kit/test/apps/options-2/test/test.js | 15 ++++++++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 .changeset/nice-geese-serve.md diff --git a/.changeset/nice-geese-serve.md b/.changeset/nice-geese-serve.md new file mode 100644 index 000000000000..406180bebe27 --- /dev/null +++ b/.changeset/nice-geese-serve.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: service worker base path in dev mode diff --git a/packages/kit/src/exports/vite/dev/index.js b/packages/kit/src/exports/vite/dev/index.js index 3935daf28565..bca3f84160cb 100644 --- a/packages/kit/src/exports/vite/dev/index.js +++ b/packages/kit/src/exports/vite/dev/index.js @@ -462,7 +462,7 @@ export async function dev(vite, vite_config, svelte_config) { res.writeHead(200, { 'content-type': 'application/javascript' }); - res.end(`import '${to_fs(resolved)}';`); + res.end(`import '${svelte_config.kit.paths.base}${to_fs(resolved)}';`); } else { res.writeHead(404); res.end('not found'); diff --git a/packages/kit/test/apps/options-2/test/test.js b/packages/kit/test/apps/options-2/test/test.js index 1c5c0ffd525a..ba5f10003284 100644 --- a/packages/kit/test/apps/options-2/test/test.js +++ b/packages/kit/test/apps/options-2/test/test.js @@ -1,4 +1,6 @@ +import path from 'node:path'; import process from 'node:process'; +import { fileURLToPath } from 'node:url'; import { expect } from '@playwright/test'; import { test } from '../../../utils.js'; @@ -73,7 +75,18 @@ test.describe('trailing slash', () => { }); test.describe('Service worker', () => { - if (process.env.DEV) return; + if (process.env.DEV) { + test('import proxy /basepath/service-worker.js', async ({ request }) => { + const __dirname = path.dirname(fileURLToPath(import.meta.url)); + const response = await request.get('/basepath/service-worker.js'); + const content = await response.text(); + expect(content).toEqual( + `import '${path.join('/basepath', '/@fs', __dirname, '../src/service-worker.js')}';` + ); + }); + + return; + } test('build /basepath/service-worker.js', async ({ baseURL, request }) => { const response = await request.get('/basepath/service-worker.js');