sveltekit-cache-first 1.0.0 → 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/LICENSE +21 -0
- package/README.md +72 -0
- package/dist/update.d.ts +1 -0
- package/dist/update.js +63 -0
- package/package.json +9 -1
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,3 +1,74 @@
|
|
|
1
|
+
<<<<<<< HEAD
|
|
2
|
+
# SvelteKit Cache First
|
|
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.
|
|
5
|
+
|
|
6
|
+
## Setup
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
npm install sveltekit-cache-first
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
```js
|
|
13
|
+
// src/service-worker.js
|
|
14
|
+
import { version, build, files } from '$service-worker';
|
|
15
|
+
import { setupServiceWorker } from 'sveltekit-cache-first/sw';
|
|
16
|
+
|
|
17
|
+
setupServiceWorker(self, { version, build, files });
|
|
18
|
+
```
|
|
19
|
+
|
|
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>
|
|
39
|
+
```
|
|
40
|
+
|
|
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';
|
|
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
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Config
|
|
62
|
+
|
|
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
|
+
};
|
|
69
|
+
|
|
70
|
+
setupServiceWorker(self, { version, build, files, options });
|
|
71
|
+
=======
|
|
1
72
|
# Svelte library
|
|
2
73
|
|
|
3
74
|
Everything you need to build a Svelte library, powered by [`sv`](https://npmjs.com/package/sv).
|
|
@@ -62,4 +133,5 @@ To publish your library to [npm](https://www.npmjs.com):
|
|
|
62
133
|
|
|
63
134
|
```sh
|
|
64
135
|
npm publish
|
|
136
|
+
>>>>>>> b1866e7 (Fix imports and double updates)
|
|
65
137
|
```
|
package/dist/update.d.ts
ADDED
|
@@ -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,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sveltekit-cache-first",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"repository": "github:isaxk/sveltekit-cache-first",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"dev": "vite dev",
|
|
@@ -28,6 +28,14 @@
|
|
|
28
28
|
".": {
|
|
29
29
|
"types": "./dist/index.d.ts",
|
|
30
30
|
"svelte": "./dist/index.js"
|
|
31
|
+
},
|
|
32
|
+
"sw": {
|
|
33
|
+
"types": "./dist/sw.d.ts",
|
|
34
|
+
"svelte": "./dist/sw.js"
|
|
35
|
+
},
|
|
36
|
+
"client": {
|
|
37
|
+
"types": "./dist/client.d.ts",
|
|
38
|
+
"svelte": "./dist/client.js"
|
|
31
39
|
}
|
|
32
40
|
},
|
|
33
41
|
"peerDependencies": {
|