sveltekit-cache-first 0.0.1 → 0.0.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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # SvelteKit Cache First
2
2
 
3
- A small library to make your SvelteKit Web App / PWA local first.
3
+ 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.
4
4
 
5
5
  ## Setup
6
6
 
@@ -16,9 +16,24 @@ import { setupServiceWorker } from 'sveltekit-cache-first';
16
16
  setupServiceWorker(self, { version, build, files });
17
17
  ```
18
18
 
19
+ #### Component method
20
+ ```svelte
21
+ <script>
22
+ import { UpdateAvailable } from 'sveltekit-cache-first';
23
+ </script>
24
+
25
+ <UpdateAvailable>
26
+ {#snippet children({ accept })}
27
+ <h2>An update is available</h2>
28
+ <p>Refresh to update</p>
29
+ <button onclick={() => accept()}>Refresh</button>
30
+ {/snippet}
31
+ </UpdateAvailable>
32
+ ```
33
+
34
+ #### Custom handler method, using [svelte-sonner](https://github.com/wobsoriano/svelte-sonner)
19
35
  ```js
20
36
  // src/routes/+layout.svelte
21
- // Example using toast from svelte-sonner
22
37
  import { toast } from 'svelte-sonner';
23
38
  import { onUpdate } from 'sveltekit-cache-first';
24
39
 
@@ -26,7 +41,7 @@ onMount(() => {
26
41
  onUpdate((accept) => {
27
42
  // Your notification logic here, eg:
28
43
  toast('An update is available', {
29
- description: 'Refresh the page to update',
44
+ description: 'Refresh to update',
30
45
  action: {
31
46
  label: 'Refresh',
32
47
  onClick: () => accept()
@@ -41,8 +56,9 @@ onMount(() => {
41
56
  ```js
42
57
  // default values
43
58
  const options = {
44
- cachePageData: false, // Cache _data.json: data returned by load functions
45
- ignoredRoutes: ['/api'] // Routes to always fetch fresh for
46
- }
59
+ cachePageData: false, // Cache _data.json: data returned by load functions
60
+ ignoredRoutes: ['/api'] // Routes to always fetch fresh for
61
+ };
47
62
 
48
63
  setupServiceWorker(self, { version, build, files, options });
64
+ ```
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  import { setupServiceWorker } from "./sw.ts";
2
+ import UpdateAvailable from "./update-available.svelte";
2
3
  import { onUpdate } from "./update.ts";
3
- export { setupServiceWorker, onUpdate, };
4
+ export { setupServiceWorker, onUpdate, UpdateAvailable };
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  // Reexport your entry components here
2
2
  import { setupServiceWorker } from "./sw.js";
3
+ import UpdateAvailable from "./update-available.svelte";
3
4
  import { onUpdate } from "./update.js";
4
- export { setupServiceWorker, onUpdate, };
5
+ export { setupServiceWorker, onUpdate, UpdateAvailable };
@@ -0,0 +1,17 @@
1
+ <script lang="ts">
2
+ import { onMount, type Snippet } from 'svelte';
3
+ import { onUpdate } from './update.js';
4
+
5
+ let { children }: { children?: Snippet<[{ accept: () => void }]> } = $props();
6
+
7
+ let handleAccept: (() => void) | null = $state(null);
8
+ onMount(() => {
9
+ onUpdate((accept) => {
10
+ handleAccept = accept;
11
+ });
12
+ });
13
+ </script>
14
+
15
+ {#if handleAccept !== null}
16
+ {@render children?.({ accept: handleAccept })}
17
+ {/if}
@@ -0,0 +1,9 @@
1
+ import { type Snippet } from 'svelte';
2
+ type $$ComponentProps = {
3
+ children?: Snippet<[{
4
+ accept: () => void;
5
+ }]>;
6
+ };
7
+ declare const UpdateAvailable: import("svelte").Component<$$ComponentProps, {}, "">;
8
+ type UpdateAvailable = ReturnType<typeof UpdateAvailable>;
9
+ export default UpdateAvailable;
package/dist/update.d.ts CHANGED
@@ -1 +1 @@
1
- export declare function onUpdate(handle: (onAccept: () => void) => void): () => void;
1
+ export declare function onUpdate(handle: (onAccept: () => void) => void): void;
package/dist/update.js CHANGED
@@ -18,7 +18,9 @@ async function checkVersionChanged(worker) {
18
18
  function handleAccept(worker) {
19
19
  worker.postMessage({ type: 'SKLOCALFIRST_SKIP_WAITING' });
20
20
  }
21
- export function onUpdate(handle) {
21
+ const handlers = [];
22
+ let listenerSetUp = false;
23
+ function setupSWListener() {
22
24
  if (!('serviceWorker' in navigator)) {
23
25
  return () => { };
24
26
  }
@@ -29,7 +31,7 @@ export function onUpdate(handle) {
29
31
  if (reg.waiting) {
30
32
  const sw = reg.waiting;
31
33
  if (await checkVersionChanged(sw)) {
32
- handle(() => handleAccept(sw));
34
+ handlers.forEach((handle) => handle(() => handleAccept(sw)));
33
35
  }
34
36
  }
35
37
  // 2️⃣ Listen for new SW installations
@@ -39,8 +41,7 @@ export function onUpdate(handle) {
39
41
  return;
40
42
  sw.addEventListener('statechange', () => {
41
43
  if (sw.state === 'installed' && navigator.serviceWorker.controller) {
42
- // SW installed, new version waiting
43
- handle(() => handleAccept(sw));
44
+ handlers.forEach((handle) => handle(() => handleAccept(sw)));
44
45
  }
45
46
  });
46
47
  });
@@ -49,5 +50,12 @@ export function onUpdate(handle) {
49
50
  navigator.serviceWorker.addEventListener('controllerchange', () => {
50
51
  window.location.reload();
51
52
  });
52
- return () => { };
53
+ listenerSetUp = true;
54
+ }
55
+ export function onUpdate(handle) {
56
+ handlers.push(handle);
57
+ if (!listenerSetUp) {
58
+ setupSWListener();
59
+ }
60
+ handlers.push(handle);
53
61
  }
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "sveltekit-cache-first",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
+ "repository": "github:isaxk/sveltekit-cache-first",
4
5
  "scripts": {
5
6
  "dev": "vite dev",
6
7
  "build": "vite build && npm run prepack",