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
Show file tree
Hide file tree
Changes from 7 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/twelve-seals-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: allow service-worker.js to import assets
22 changes: 14 additions & 8 deletions packages/kit/src/exports/vite/build/build_service_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as vite from 'vite';
import { dedent } from '../../../core/sync/utils.js';
import { s } from '../../../utils/misc.js';
import { get_config_aliases } from '../utils.js';
import { assets_base } from './utils.js';

/**
* @param {string} out
Expand Down Expand Up @@ -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

build: {
lib: {
entry: /** @type {string} */ (service_worker_entry_file),
name: 'app',
formats: ['es']
},
modulePreload: false,
rollupOptions: {
input: {
'service-worker': service_worker_entry_file
},
output: {
entryFileNames: 'service-worker.js'
entryFileNames: '[name].js',
assetFileNames: `${kit.appDir}/immutable/assets/[name].[hash][extname]`,
inlineDynamicImports: true
}
},
outDir: `${out}/client`,
Expand All @@ -83,6 +82,13 @@ export async function build_service_worker(
configFile: false,
resolve: {
alias: [...get_config_aliases(kit), { find: '$service-worker', replacement: service_worker }]
},
experimental: {
renderBuiltUrl(filename) {
return {
runtime: `new URL(${JSON.stringify(filename)}, location.href).pathname`
};
}
}
});
}
8 changes: 0 additions & 8 deletions packages/kit/src/exports/vite/build/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,6 @@ export function resolve_symlinks(manifest, file) {
return { chunk, file };
}

/**
* @param {import('types').ValidatedKitConfig} config
* @returns {string}
*/
export function assets_base(config) {
return (config.paths.assets || config.paths.base || '.') + '/';
}

const method_names = new Set(['GET', 'HEAD', 'PUT', 'POST', 'DELETE', 'PATCH', 'OPTIONS']);

// If we'd written this in TypeScript, it could be easy...
Expand Down
4 changes: 2 additions & 2 deletions packages/kit/src/exports/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { load_config } from '../../core/config/index.js';
import { generate_manifest } from '../../core/generate_manifest/index.js';
import { build_server_nodes } from './build/build_server.js';
import { build_service_worker } from './build/build_service_worker.js';
import { assets_base, find_deps } from './build/utils.js';
import { find_deps } from './build/utils.js';
import { dev } from './dev/index.js';
import { is_illegal, module_guard, normalize_id } from './graph_analysis/index.js';
import { preview } from './preview/index.js';
Expand Down Expand Up @@ -548,7 +548,7 @@ function kit({ svelte_config }) {
const ext = kit.output.preloadStrategy === 'preload-mjs' ? 'mjs' : 'js';

new_config = {
base: ssr ? assets_base(kit) : './',
base: ssr ? `${kit.paths.assets || kit.paths.base || '.'}/` : './',
build: {
cssCodeSplit: true,
outDir: `${out}/${ssr ? 'server' : 'client'}`,
Expand Down
Binary file added packages/kit/test/apps/options-2/src/image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions packages/kit/test/apps/options-2/src/service-worker.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { base, build, files, prerendered, version } from '$service-worker';
import src from './image.jpg?url';

self.base = base;
self.build = build;
self.image_src = src;

const name = `cache-${version}`;

Expand Down
8 changes: 6 additions & 2 deletions packages/kit/test/apps/options-2/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ test.describe('paths', () => {
test.describe('Service worker', () => {
if (process.env.DEV) return;

test('build /basepath/service-worker.js', async ({ request }) => {
test('build /basepath/service-worker.js', async ({ baseURL, request }) => {
const response = await request.get('/basepath/service-worker.js');
const content = await response.text();

Expand All @@ -54,12 +54,16 @@ test.describe('Service worker', () => {
build: null
};

const pathname = '/basepath/service-worker.js';

fn(self, {
pathname: '/basepath/service-worker.js'
href: baseURL + pathname,
pathname
});

expect(self.base).toBe('/basepath');
expect(self.build[0]).toMatch(/\/basepath\/_app\/immutable\/entry\/start\.[a-z0-9]+\.js/);
expect(self.image_src).toMatch(/\/basepath\/_app\/immutable\/assets\/image\.[a-z0-9]+\.jpg/);
});

test('does not register /basepath/service-worker.js', async ({ page }) => {
Expand Down