Skip to content
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/ready-dragons-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: populate `version` in service workers
20 changes: 15 additions & 5 deletions packages/kit/src/core/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,33 +238,42 @@ export function create_sveltekit_env_public(variables, env, prelude) {
* `env.js`. If there are none, values are inlined.
* @param {Record<string, EnvVarConfig<any>> | null} variables
* @param {Record<string, string>} env
* @param {string} version
* @param {string} global
* @param {string} base
* @param {string} app_dir
*/
export function create_sveltekit_env_service_worker(variables, env, global, base, app_dir) {
export function create_sveltekit_env_service_worker(
variables,
env,
version,
global,
base,
app_dir
) {
const has_dynamic_public_env = Object.values(variables ?? {}).some(
(config) => config.public && !config.static
);

if (!has_dynamic_public_env) {
return create_sveltekit_env_service_worker_dev(variables, env, global);
return create_sveltekit_env_service_worker_dev(variables, env, version, global);
}

return dedent`
import { env } from '${base}/${app_dir}/env.js';

${global} = { env };
${global} = { env, version: ${JSON.stringify(version)} };
`;
}

/**
* Creates the `__sveltekit/env/service-worker` module used in development
* @param {Record<string, EnvVarConfig<any>> | null} variables
* @param {Record<string, string>} env
* @param {string} version
* @param {string} global
*/
export function create_sveltekit_env_service_worker_dev(variables, env, global) {
export function create_sveltekit_env_service_worker_dev(variables, env, version, global) {
/** @type {string[]} */
const properties = [];

Expand All @@ -284,7 +293,8 @@ export function create_sveltekit_env_service_worker_dev(variables, env, global)
${global} = {
env: {
${properties.join(',\n\t\t') || '// empty'}
}
},
version: ${JSON.stringify(version)}
};
`;
}
Expand Down
40 changes: 27 additions & 13 deletions packages/kit/src/exports/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -691,11 +691,17 @@ function kit({ svelte_config }) {
? create_sveltekit_env_service_worker(
explicit_env_config,
env,
kit.version.name,
kit_global,
kit.paths.base,
kit.appDir
)
: create_sveltekit_env_service_worker_dev(explicit_env_config, env, kit_global);
: create_sveltekit_env_service_worker_dev(
explicit_env_config,
env,
kit.version.name,
kit_global
);
}
}
}
Expand Down Expand Up @@ -1198,11 +1204,17 @@ function kit({ svelte_config }) {
? create_sveltekit_env_service_worker(
explicit_env_config,
env,
kit.version.name,
kit_global,
kit.paths.base,
kit.appDir
)
: create_sveltekit_env_service_worker_dev(explicit_env_config, env, kit_global);
: create_sveltekit_env_service_worker_dev(
explicit_env_config,
env,
kit.version.name,
kit_global
);
}

if (id === sveltekit_env_public_client) {
Expand Down Expand Up @@ -1233,17 +1245,19 @@ function kit({ svelte_config }) {
applyToEnvironment(environment) {
return !!service_worker_entry_file && environment.config.consumer === 'client';
},
transform(code, id) {
if (id !== service_worker_entry_file) return;

// prepend the service worker with an import that configures
// `env`, in case `$app/env/public` is imported. In production
// this is required: dynamic public env vars aren't known at
// build time, so `env.js` is loaded at runtime. In dev, the
// imported module just inlines the current values instead.
return {
code: `import '__sveltekit/env/service-worker';\n${code}`
};
transform: {
handler(code, id) {
if (id !== service_worker_entry_file) return;

// prepend the service worker with an import that configures
// `env`, in case `$app/env/public` is imported. In production
// this is required: dynamic public env vars aren't known at
// build time, so `env.js` is loaded at runtime. In dev, the
// imported module just inlines the current values instead.
return {
code: `import '__sveltekit/env/service-worker';\n${code}`
};
}
}
};

Expand Down
3 changes: 2 additions & 1 deletion packages/kit/test/apps/basics/src/service-worker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { build, version } from '$service-worker';
import { build } from '$service-worker';
import { version } from '$app/env';
import { PUBLIC_STATIC } from '$app/env/public';

const name = `cache-${version}-${PUBLIC_STATIC}`;
Expand Down
3 changes: 2 additions & 1 deletion packages/kit/test/apps/options-2/src/service-worker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { base, build, version } from '$service-worker';
import { base, build } from '$service-worker';
import { version } from '$app/env';
import { MESSAGE } from '$app/env/public';
import src from './image.jpg?url';

Expand Down
Loading