diff --git a/.changeset/green-cycles-shake.md b/.changeset/green-cycles-shake.md new file mode 100644 index 000000000000..5b0d39476f01 --- /dev/null +++ b/.changeset/green-cycles-shake.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: prevent esbuild adding phantom exports to service worker diff --git a/.changeset/tricky-meals-perform.md b/.changeset/tricky-meals-perform.md deleted file mode 100644 index 029af8321852..000000000000 --- a/.changeset/tricky-meals-perform.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@sveltejs/kit': patch ---- - -fix: build service workers in IIFE format diff --git a/packages/kit/src/exports/vite/build/build_service_worker.js b/packages/kit/src/exports/vite/build/build_service_worker.js index 8bb292dc68c6..3d3849c6d727 100644 --- a/packages/kit/src/exports/vite/build/build_service_worker.js +++ b/packages/kit/src/exports/vite/build/build_service_worker.js @@ -70,10 +70,8 @@ export async function build_service_worker( 'service-worker': service_worker_entry_file }, output: { - // default 'es' format would be nicer - // iife is workaround for https://github.com/vitejs/vite/issues/15379 - format: 'iife', - entryFileNames: '[name].js', + // .mjs so that esbuild doesn't incorrectly inject `export` https://github.com/vitejs/vite/issues/15379 + entryFileNames: 'service-worker.mjs', assetFileNames: `${kit.appDir}/immutable/assets/[name].[hash][extname]`, inlineDynamicImports: true } @@ -95,4 +93,7 @@ export async function build_service_worker( } } }); + + // rename .mjs to .js to avoid incorrect MIME types with ancient webservers + fs.renameSync(`${out}/client/service-worker.mjs`, `${out}/client/service-worker.js`); }