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

replace timestamp in $service-worker with version #4213

Merged
merged 2 commits into from
Mar 5, 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/five-eagles-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[breaking] Replace timestamp in \$service-worker with version
4 changes: 2 additions & 2 deletions documentation/docs/80-migrating.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ This file also has no direct equivalent, since SvelteKit apps can run in serverl

Most imports from `@sapper/service-worker` have equivalents in [`$service-worker`](/docs/modules#$service-worker):

- `timestamp` is unchanged
- `files` is unchanged
- `shell` is now `build`
- `routes` has been removed
- `shell` is now `build`
- `timestamp` is now `version`

#### src/template.html

Expand Down
9 changes: 8 additions & 1 deletion packages/kit/src/core/build/build_service_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ export async function build_service_worker(
fs.writeFileSync(
service_worker,
`
export const timestamp = ${Date.now()};
// TODO remove for 1.0
export const timestamp = {
toString: () => {
throw new Error('\`timestamp\` has been removed from $service-worker. Use \`version\` instead');
}
};

export const build = [
${Array.from(app_files)
Expand All @@ -58,6 +63,8 @@ export async function build_service_worker(
.map((path) => s(normalize_path(path, config.kit.trailingSlash)))
.join(',\n\t\t\t\t')}
];

export const version = ${s(config.kit.version.name)};
`
.replace(/^\t{3}/gm, '')
.trim()
Expand Down
6 changes: 3 additions & 3 deletions packages/kit/test/apps/basics/src/service-worker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { timestamp, build } from '$service-worker';
import { build, version } from '$service-worker';

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

self.addEventListener('install', (event) => {
// @ts-expect-error
Expand All @@ -12,7 +12,7 @@ self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then(async (keys) => {
for (const key of keys) {
if (!key.includes(String(timestamp))) caches.delete(key);
if (!key.includes(version)) caches.delete(key);
}
})
);
Expand Down
6 changes: 3 additions & 3 deletions packages/kit/test/apps/options-2/src/service-worker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { timestamp, build } from '$service-worker';
import { build, version } from '$service-worker';

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

self.addEventListener('install', (event) => {
// @ts-expect-error
Expand All @@ -12,7 +12,7 @@ self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then(async (keys) => {
for (const key of keys) {
if (!key.includes(String(timestamp))) caches.delete(key);
if (!key.includes(version)) caches.delete(key);
}
})
);
Expand Down
6 changes: 3 additions & 3 deletions packages/kit/types/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ declare module '$lib' {}

/**
* ```ts
* import { build, files, timestamp } from '$service-worker';
* import { build, files, prerendered, version } from '$service-worker';
* ```
*
* This module is only available to [service workers](/docs/service-workers).
Expand All @@ -233,9 +233,9 @@ declare module '$service-worker' {
*/
export const prerendered: string[];
/**
* The result of calling `Date.now()` at build time. It's useful for generating unique cache names inside your service worker, so that a later deployment of your app can invalidate old caches.
* See [`config.kit.version`](/docs/configuration#version). It's useful for generating unique cache names inside your service worker, so that a later deployment of your app can invalidate old caches.
Rich-Harris marked this conversation as resolved.
Show resolved Hide resolved
*/
export const timestamp: number;
export const version: string;
}

declare module '@sveltejs/kit/hooks' {
Expand Down