sveltekit-cache-first 1.0.0 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Isaac Boorman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,65 +1,71 @@
1
- # Svelte library
2
1
 
3
- Everything you need to build a Svelte library, powered by [`sv`](https://npmjs.com/package/sv).
2
+ # SvelteKit Cache First
4
3
 
5
- Read more about creating a library [in the docs](https://svelte.dev/docs/kit/packaging).
4
+ A small library to make your SvelteKit Web App / PWA use a cache first approach: instant second visit loads and fast performance on poor network conditions.
6
5
 
7
- ## Creating a project
6
+ ## Setup
8
7
 
9
- If you're seeing this, you've probably already done this step. Congrats!
10
-
11
- ```sh
12
- # create a new project in the current directory
13
- npx sv create
14
-
15
- # create a new project in my-app
16
- npx sv create my-app
8
+ ```bash
9
+ npm install sveltekit-cache-first
17
10
  ```
18
11
 
19
- To recreate this project with the same configuration:
20
-
21
- ```sh
22
- # recreate this project
23
- pnpm dlx sv@0.16.6 create --template library --types ts --install pnpm sveltekit-cache-first
24
- ```
25
-
26
- ## Developing
27
-
28
- Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:
29
-
30
- ```sh
31
- npm run dev
12
+ ```js
13
+ // src/service-worker.js
14
+ import { version, build, files } from '$service-worker';
15
+ import { setupServiceWorker } from 'sveltekit-cache-first/sw';
32
16
 
33
- # or start the server and open the app in a new browser tab
34
- npm run dev -- --open
17
+ setupServiceWorker(self, { version, build, files });
35
18
  ```
36
19
 
37
- Everything inside `src/lib` is part of your library, everything inside `src/routes` can be used as a showcase or preview app.
38
-
39
- ## Building
40
-
41
- To build your library:
42
-
43
- ```sh
44
- npm pack
20
+ #### Component method
21
+ ```svelte
22
+ <script>
23
+ import { UpdateAvailable } from 'sveltekit-cache-first';
24
+ </script>
25
+
26
+ <UpdateAvailable>
27
+ {#snippet children({ accept })}
28
+ <h2>An update is available</h2>
29
+ <p>Refresh to update</p>
30
+ <button onclick={() => accept()}>Refresh</button>
31
+ {/snippet}
32
+ </UpdateAvailable>
33
+
34
+ <!-- Optional -->
35
+ <!-- Note: This will disappear after the update has been detected, which may take a few seconds to appear. Only use this if your app must always be running the latest version. And if that is that is the case, strongly consider if cache-first is the right approach, or use other methods like api versioning. -->
36
+ <NoUpdate>
37
+ Main logic here
38
+ </NoUpdate>
45
39
  ```
46
40
 
47
- To create a production version of your showcase app:
48
-
49
- ```sh
50
- npm run build
41
+ #### Custom handler method, using [svelte-sonner](https://github.com/wobsoriano/svelte-sonner)
42
+ ```js
43
+ // src/routes/+layout.svelte
44
+ import { toast } from 'svelte-sonner';
45
+ import { onUpdate } from 'sveltekit-cache-first/client';
46
+
47
+ onMount(() => {
48
+ onUpdate((accept) => {
49
+ // Your notification logic here, eg:
50
+ toast('An update is available', {
51
+ description: 'Refresh to update',
52
+ action: {
53
+ label: 'Refresh',
54
+ onClick: () => accept()
55
+ }
56
+ });
57
+ });
58
+ });
51
59
  ```
52
60
 
53
- You can preview the production build with `npm run preview`.
54
-
55
- > To deploy your app, you may need to install an [adapter](https://svelte.dev/docs/kit/adapters) for your target environment.
56
-
57
- ## Publishing
58
-
59
- Go into the `package.json` and give your package the desired name through the `"name"` option. Also consider adding a `"license"` field and point it to a `LICENSE` file which you can create from a template (one popular option is the [MIT license](https://opensource.org/license/mit/)).
61
+ ## Config
60
62
 
61
- To publish your library to [npm](https://www.npmjs.com):
63
+ ```js
64
+ // default values
65
+ const options = {
66
+ cachePageData: false, // Cache _data.json: data returned by load functions
67
+ ignoredRoutes: ['/api'] // Routes to always fetch fresh for
68
+ };
62
69
 
63
- ```sh
64
- npm publish
70
+ setupServiceWorker(self, { version, build, files, options });
65
71
  ```
@@ -0,0 +1 @@
1
+ export declare function onUpdate(handle: (onAccept: () => void) => void): void;
package/dist/update.js ADDED
@@ -0,0 +1,63 @@
1
+ function getVersion(worker) {
2
+ return new Promise((resolve) => {
3
+ const channel = new MessageChannel();
4
+ channel.port1.onmessage = (e) => resolve(e.data ?? null);
5
+ worker.postMessage({ type: 'SKLOCALFIRST_GET_VERSION' }, [channel.port2]);
6
+ });
7
+ }
8
+ async function checkVersionChanged(worker) {
9
+ const version = await getVersion(worker);
10
+ if (!version)
11
+ return false;
12
+ const lastPrompted = localStorage.getItem('lastPrompted');
13
+ if (lastPrompted === version)
14
+ return false;
15
+ localStorage.setItem('lastPrompted', version);
16
+ return true;
17
+ }
18
+ function handleAccept(worker) {
19
+ worker.postMessage({ type: 'SKLOCALFIRST_SKIP_WAITING' });
20
+ }
21
+ const handlers = [];
22
+ let listenerSetUp = false;
23
+ function setupSWListener() {
24
+ if (!('serviceWorker' in navigator)) {
25
+ return () => { };
26
+ }
27
+ navigator.serviceWorker.getRegistration().then(async (reg) => {
28
+ if (!reg)
29
+ return;
30
+ // 1️⃣ Detect if a waiting SW already exists
31
+ if (reg.waiting) {
32
+ const sw = reg.waiting;
33
+ if (await checkVersionChanged(sw)) {
34
+ handlers.forEach((handle) => handle(() => handleAccept(sw)));
35
+ }
36
+ }
37
+ // 2️⃣ Listen for new SW installations
38
+ reg.addEventListener('updatefound', () => {
39
+ const sw = reg.installing;
40
+ if (!sw)
41
+ return;
42
+ sw.addEventListener('statechange', async () => {
43
+ if (sw.state === 'installed' && navigator.serviceWorker.controller) {
44
+ if (await checkVersionChanged(sw)) {
45
+ handlers.forEach((handle) => handle(() => handleAccept(sw)));
46
+ }
47
+ }
48
+ });
49
+ });
50
+ });
51
+ // 3️⃣ Reload page when the new SW takes control
52
+ navigator.serviceWorker.addEventListener('controllerchange', () => {
53
+ window.location.reload();
54
+ });
55
+ listenerSetUp = true;
56
+ }
57
+ export function onUpdate(handle) {
58
+ handlers.push(handle);
59
+ if (!listenerSetUp) {
60
+ setupSWListener();
61
+ }
62
+ handlers.push(handle);
63
+ }
package/package.json CHANGED
@@ -1,60 +1,70 @@
1
1
  {
2
- "name": "sveltekit-cache-first",
3
- "version": "1.0.0",
4
- "repository": "github:isaxk/sveltekit-cache-first",
5
- "scripts": {
6
- "dev": "vite dev",
7
- "build": "vite build && npm run prepack",
8
- "preview": "vite preview",
9
- "prepare": "svelte-kit sync || echo ''",
10
- "prepack": "svelte-kit sync && svelte-package && publint",
11
- "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
12
- "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
13
- "lint": "prettier --check . && eslint .",
14
- "format": "prettier --write ."
15
- },
16
- "files": [
17
- "dist",
18
- "!dist/**/*.test.*",
19
- "!dist/**/*.spec.*"
20
- ],
21
- "sideEffects": [
22
- "**/*.css"
23
- ],
24
- "svelte": "./dist/index.js",
25
- "types": "./dist/index.d.ts",
26
- "type": "module",
27
- "exports": {
28
- ".": {
29
- "types": "./dist/index.d.ts",
30
- "svelte": "./dist/index.js"
31
- }
32
- },
33
- "peerDependencies": {
34
- "@sveltejs/kit": "^2.63.0",
35
- "svelte": "^5.0.0"
36
- },
37
- "devDependencies": {
38
- "@eslint/js": "^10.0.1",
39
- "@sveltejs/adapter-auto": "^7.0.1",
40
- "@sveltejs/kit": "^2.63.0",
41
- "@sveltejs/package": "^2.5.8",
42
- "@sveltejs/vite-plugin-svelte": "^7.1.2",
43
- "@types/node": "^24",
44
- "eslint": "^10.4.1",
45
- "eslint-config-prettier": "^10.1.8",
46
- "eslint-plugin-svelte": "^3.19.0",
47
- "globals": "^17.6.0",
48
- "prettier": "^3.8.3",
49
- "prettier-plugin-svelte": "^4.1.0",
50
- "publint": "^0.3.21",
51
- "svelte": "^5.56.1",
52
- "svelte-check": "^4.6.0",
53
- "typescript": "^6.0.3",
54
- "typescript-eslint": "^8.60.1",
55
- "vite": "^8.0.16"
56
- },
57
- "keywords": [
58
- "svelte"
59
- ]
60
- }
2
+ "name": "sveltekit-cache-first",
3
+ "version": "1.1.2",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "github:isaxk/sveltekit-cache-first"
7
+ },
8
+ "license": "MIT",
9
+ "files": [
10
+ "dist",
11
+ "!dist/**/*.test.*",
12
+ "!dist/**/*.spec.*"
13
+ ],
14
+ "sideEffects": [
15
+ "**/*.css"
16
+ ],
17
+ "svelte": "./dist/index.js",
18
+ "types": "./dist/index.d.ts",
19
+ "type": "module",
20
+ "exports": {
21
+ ".": {
22
+ "types": "./dist/index.d.ts",
23
+ "svelte": "./dist/index.js"
24
+ },
25
+ "sw": {
26
+ "types": "./dist/sw.d.ts",
27
+ "svelte": "./dist/sw.js"
28
+ },
29
+ "client": {
30
+ "types": "./dist/client.d.ts",
31
+ "svelte": "./dist/client.js"
32
+ }
33
+ },
34
+ "peerDependencies": {
35
+ "@sveltejs/kit": "^2.63.0",
36
+ "svelte": "^5.0.0"
37
+ },
38
+ "devDependencies": {
39
+ "@eslint/js": "^10.0.1",
40
+ "@sveltejs/adapter-auto": "^7.0.1",
41
+ "@sveltejs/kit": "^2.63.0",
42
+ "@sveltejs/package": "^2.5.8",
43
+ "@sveltejs/vite-plugin-svelte": "^7.1.2",
44
+ "@types/node": "^24",
45
+ "eslint": "^10.4.1",
46
+ "eslint-config-prettier": "^10.1.8",
47
+ "eslint-plugin-svelte": "^3.19.0",
48
+ "globals": "^17.6.0",
49
+ "prettier": "^3.8.3",
50
+ "prettier-plugin-svelte": "^4.1.0",
51
+ "publint": "^0.3.21",
52
+ "svelte": "^5.56.1",
53
+ "svelte-check": "^4.6.0",
54
+ "typescript": "^6.0.3",
55
+ "typescript-eslint": "^8.60.1",
56
+ "vite": "^8.0.16"
57
+ },
58
+ "keywords": [
59
+ "svelte"
60
+ ],
61
+ "scripts": {
62
+ "dev": "vite dev",
63
+ "build": "vite build && npm run prepack",
64
+ "preview": "vite preview",
65
+ "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
66
+ "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
67
+ "lint": "prettier --check . && eslint .",
68
+ "format": "prettier --write ."
69
+ }
70
+ }