sveltekit-cache-first 0.0.5 → 1.1.0

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/README.md CHANGED
@@ -1,3 +1,4 @@
1
+ <<<<<<< HEAD
1
2
  # SvelteKit Cache First
2
3
 
3
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.
@@ -67,4 +68,70 @@ const options = {
67
68
  };
68
69
 
69
70
  setupServiceWorker(self, { version, build, files, options });
71
+ =======
72
+ # Svelte library
73
+
74
+ Everything you need to build a Svelte library, powered by [`sv`](https://npmjs.com/package/sv).
75
+
76
+ Read more about creating a library [in the docs](https://svelte.dev/docs/kit/packaging).
77
+
78
+ ## Creating a project
79
+
80
+ If you're seeing this, you've probably already done this step. Congrats!
81
+
82
+ ```sh
83
+ # create a new project in the current directory
84
+ npx sv create
85
+
86
+ # create a new project in my-app
87
+ npx sv create my-app
88
+ ```
89
+
90
+ To recreate this project with the same configuration:
91
+
92
+ ```sh
93
+ # recreate this project
94
+ pnpm dlx sv@0.16.6 create --template library --types ts --install pnpm sveltekit-cache-first
95
+ ```
96
+
97
+ ## Developing
98
+
99
+ Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:
100
+
101
+ ```sh
102
+ npm run dev
103
+
104
+ # or start the server and open the app in a new browser tab
105
+ npm run dev -- --open
106
+ ```
107
+
108
+ Everything inside `src/lib` is part of your library, everything inside `src/routes` can be used as a showcase or preview app.
109
+
110
+ ## Building
111
+
112
+ To build your library:
113
+
114
+ ```sh
115
+ npm pack
116
+ ```
117
+
118
+ To create a production version of your showcase app:
119
+
120
+ ```sh
121
+ npm run build
122
+ ```
123
+
124
+ You can preview the production build with `npm run preview`.
125
+
126
+ > To deploy your app, you may need to install an [adapter](https://svelte.dev/docs/kit/adapters) for your target environment.
127
+
128
+ ## Publishing
129
+
130
+ 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/)).
131
+
132
+ To publish your library to [npm](https://www.npmjs.com):
133
+
134
+ ```sh
135
+ npm publish
136
+ >>>>>>> b1866e7 (Fix imports and double updates)
70
137
  ```
@@ -0,0 +1 @@
1
+ export declare function onUpdate(handle: (onAccept: () => void) => void): void;
package/dist/client.js ADDED
@@ -0,0 +1,68 @@
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
+ let handlers = [];
22
+ let listenerSetUp = false;
23
+ function sendOnce(handleAccept) {
24
+ handlers.forEach((handle) => handle(handleAccept));
25
+ handlers = [];
26
+ }
27
+ function setupSWListener() {
28
+ if (!('serviceWorker' in navigator)) {
29
+ return () => { };
30
+ }
31
+ navigator.serviceWorker.getRegistration().then(async (reg) => {
32
+ if (!reg)
33
+ return;
34
+ listenerSetUp = true;
35
+ // 1️⃣ Detect if a waiting SW already exists
36
+ if (reg.waiting) {
37
+ const sw = reg.waiting;
38
+ if (await checkVersionChanged(sw)) {
39
+ console.log('existing waiting SW', sw);
40
+ sendOnce(() => handleAccept(sw));
41
+ return;
42
+ }
43
+ }
44
+ // 2️⃣ Listen for new SW installations
45
+ reg.addEventListener('updatefound', () => {
46
+ const sw = reg.installing;
47
+ if (!sw)
48
+ return;
49
+ sw.addEventListener('statechange', () => {
50
+ if (sw.state === 'installed' && navigator.serviceWorker.controller) {
51
+ console.log('state change', sw);
52
+ sendOnce(() => handleAccept(sw));
53
+ return;
54
+ }
55
+ });
56
+ });
57
+ });
58
+ // 3️⃣ Reload page when the new SW takes control
59
+ navigator.serviceWorker.addEventListener('controllerchange', () => {
60
+ window.location.reload();
61
+ });
62
+ }
63
+ export function onUpdate(handle) {
64
+ handlers.push(handle);
65
+ if (!listenerSetUp) {
66
+ setupSWListener();
67
+ }
68
+ }
package/dist/index.d.ts CHANGED
@@ -1,4 +1 @@
1
- import UpdateAvailable from "./update-available.svelte";
2
- import { onUpdate } from "./update.ts";
3
- import NoUpdate from "./no-update.svelte";
4
- export { onUpdate, UpdateAvailable, NoUpdate };
1
+ export { default as UpdateAvailable } from "./update-available.svelte";
package/dist/index.js CHANGED
@@ -1,5 +1 @@
1
- // Reexport your entry components here
2
- import UpdateAvailable from "./update-available.svelte";
3
- import { onUpdate } from "./update.js";
4
- import NoUpdate from "./no-update.svelte";
5
- export { onUpdate, UpdateAvailable, NoUpdate };
1
+ export { default as UpdateAvailable } from "./update-available.svelte";
@@ -1,6 +1,6 @@
1
1
  <script lang="ts">
2
2
  import { onMount, type Snippet } from 'svelte';
3
- import { onUpdate } from './update.js';
3
+ import { onUpdate } from './client.js';
4
4
 
5
5
  let { children }: { children?: Snippet } = $props();
6
6
 
@@ -1,6 +1,6 @@
1
1
  <script lang="ts">
2
2
  import { onMount, type Snippet } from 'svelte';
3
- import { onUpdate } from './update.js';
3
+ import { onUpdate } from './client.js';
4
4
 
5
5
  let { children }: { children?: Snippet<[{ accept: () => void }]> } = $props();
6
6
 
package/package.json CHANGED
@@ -1,8 +1,7 @@
1
1
  {
2
2
  "name": "sveltekit-cache-first",
3
- "version": "0.0.5",
3
+ "version": "1.1.0",
4
4
  "repository": "github:isaxk/sveltekit-cache-first",
5
- "license": "MIT",
6
5
  "scripts": {
7
6
  "dev": "vite dev",
8
7
  "build": "vite build && npm run prepack",
@@ -14,7 +13,6 @@
14
13
  "lint": "prettier --check . && eslint .",
15
14
  "format": "prettier --write ."
16
15
  },
17
-
18
16
  "files": [
19
17
  "dist",
20
18
  "!dist/**/*.test.*",
@@ -31,14 +29,18 @@
31
29
  "types": "./dist/index.d.ts",
32
30
  "svelte": "./dist/index.js"
33
31
  },
34
- "./sw": {
32
+ "sw": {
35
33
  "types": "./dist/sw.d.ts",
36
- "default": "./dist/sw.js"
34
+ "svelte": "./dist/sw.js"
35
+ },
36
+ "client": {
37
+ "types": "./dist/client.d.ts",
38
+ "svelte": "./dist/client.js"
37
39
  }
38
40
  },
39
41
  "peerDependencies": {
40
- "svelte": "^5.0.0",
41
- "@sveltejs/kit": "^2.63.0"
42
+ "@sveltejs/kit": "^2.63.0",
43
+ "svelte": "^5.0.0"
42
44
  },
43
45
  "devDependencies": {
44
46
  "@eslint/js": "^10.0.1",