sveltekit-cache-first 0.0.4 → 1.0.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,70 +1,65 @@
1
- # SvelteKit Cache First
1
+ # Svelte library
2
2
 
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.
3
+ Everything you need to build a Svelte library, powered by [`sv`](https://npmjs.com/package/sv).
4
4
 
5
- ## Setup
5
+ Read more about creating a library [in the docs](https://svelte.dev/docs/kit/packaging).
6
6
 
7
- ```bash
8
- npm install sveltekit-cache-first
7
+ ## Creating a project
8
+
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
9
17
  ```
10
18
 
11
- ```js
12
- // src/service-worker.js
13
- import { version, build, files } from '$service-worker';
14
- import { setupServiceWorker } from 'sveltekit-cache-first/sw';
19
+ To recreate this project with the same configuration:
15
20
 
16
- setupServiceWorker(self, { version, build, files });
21
+ ```sh
22
+ # recreate this project
23
+ pnpm dlx sv@0.16.6 create --template library --types ts --install pnpm sveltekit-cache-first
17
24
  ```
18
25
 
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
- <!-- Optional -->
34
- <!-- 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. -->
35
- <NoUpdate>
36
- Main logic here
37
- </NoUpdate>
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
32
+
33
+ # or start the server and open the app in a new browser tab
34
+ npm run dev -- --open
38
35
  ```
39
36
 
40
- #### Custom handler method, using [svelte-sonner](https://github.com/wobsoriano/svelte-sonner)
41
- ```js
42
- // src/routes/+layout.svelte
43
- import { toast } from 'svelte-sonner';
44
- import { onUpdate } from 'sveltekit-cache-first';
45
-
46
- onMount(() => {
47
- onUpdate((accept) => {
48
- // Your notification logic here, eg:
49
- toast('An update is available', {
50
- description: 'Refresh to update',
51
- action: {
52
- label: 'Refresh',
53
- onClick: () => accept()
54
- }
55
- });
56
- });
57
- });
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
45
+ ```
46
+
47
+ To create a production version of your showcase app:
48
+
49
+ ```sh
50
+ npm run build
58
51
  ```
59
52
 
60
- ## Config
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
60
 
62
- ```js
63
- // default values
64
- const options = {
65
- cachePageData: false, // Cache _data.json: data returned by load functions
66
- ignoredRoutes: ['/api'] // Routes to always fetch fresh for
67
- };
61
+ To publish your library to [npm](https://www.npmjs.com):
68
62
 
69
- setupServiceWorker(self, { version, build, files, options });
63
+ ```sh
64
+ npm publish
70
65
  ```
@@ -18,8 +18,12 @@ async function checkVersionChanged(worker) {
18
18
  function handleAccept(worker) {
19
19
  worker.postMessage({ type: 'SKLOCALFIRST_SKIP_WAITING' });
20
20
  }
21
- const handlers = [];
21
+ let handlers = [];
22
22
  let listenerSetUp = false;
23
+ function sendOnce(handleAccept) {
24
+ handlers.forEach((handle) => handle(handleAccept));
25
+ handlers = [];
26
+ }
23
27
  function setupSWListener() {
24
28
  if (!('serviceWorker' in navigator)) {
25
29
  return () => { };
@@ -27,11 +31,14 @@ function setupSWListener() {
27
31
  navigator.serviceWorker.getRegistration().then(async (reg) => {
28
32
  if (!reg)
29
33
  return;
34
+ listenerSetUp = true;
30
35
  // 1️⃣ Detect if a waiting SW already exists
31
36
  if (reg.waiting) {
32
37
  const sw = reg.waiting;
33
38
  if (await checkVersionChanged(sw)) {
34
- handlers.forEach((handle) => handle(() => handleAccept(sw)));
39
+ console.log('existing waiting SW', sw);
40
+ sendOnce(() => handleAccept(sw));
41
+ return;
35
42
  }
36
43
  }
37
44
  // 2️⃣ Listen for new SW installations
@@ -41,7 +48,9 @@ function setupSWListener() {
41
48
  return;
42
49
  sw.addEventListener('statechange', () => {
43
50
  if (sw.state === 'installed' && navigator.serviceWorker.controller) {
44
- handlers.forEach((handle) => handle(() => handleAccept(sw)));
51
+ console.log('state change', sw);
52
+ sendOnce(() => handleAccept(sw));
53
+ return;
45
54
  }
46
55
  });
47
56
  });
@@ -50,12 +59,10 @@ function setupSWListener() {
50
59
  navigator.serviceWorker.addEventListener('controllerchange', () => {
51
60
  window.location.reload();
52
61
  });
53
- listenerSetUp = true;
54
62
  }
55
63
  export function onUpdate(handle) {
56
64
  handlers.push(handle);
57
65
  if (!listenerSetUp) {
58
66
  setupSWListener();
59
67
  }
60
- handlers.push(handle);
61
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.4",
3
+ "version": "1.0.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.*",
@@ -30,15 +28,11 @@
30
28
  ".": {
31
29
  "types": "./dist/index.d.ts",
32
30
  "svelte": "./dist/index.js"
33
- },
34
- "./sw": {
35
- "types": "./dist/sw.d.ts",
36
- "default": "./dist/sw.js"
37
31
  }
38
32
  },
39
33
  "peerDependencies": {
40
- "svelte": "^5.0.0",
41
- "@sveltejs/kit": "^2.63.0"
34
+ "@sveltejs/kit": "^2.63.0",
35
+ "svelte": "^5.0.0"
42
36
  },
43
37
  "devDependencies": {
44
38
  "@eslint/js": "^10.0.1",
package/LICENSE DELETED
@@ -1,21 +0,0 @@
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.
File without changes