spark-html-offline 0.1.0 → 0.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/README.md +33 -8
- package/package.json +3 -4
- package/src/bun.js +33 -0
- package/src/index.d.ts +4 -4
- package/src/index.js +3 -3
- package/src/vite.d.ts +0 -8
- package/src/vite.js +0 -35
package/README.md
CHANGED
|
@@ -17,11 +17,10 @@ offline();
|
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
```js
|
|
20
|
-
//
|
|
21
|
-
import
|
|
22
|
-
import offlineSw from 'spark-html-offline/vite';
|
|
20
|
+
// spark.config.js — writes /spark-sw.js in build, serves it in dev
|
|
21
|
+
import offlineSw from 'spark-html-offline/bun';
|
|
23
22
|
|
|
24
|
-
export default {
|
|
23
|
+
export default { pipeline: [offlineSw()] };
|
|
25
24
|
```
|
|
26
25
|
|
|
27
26
|
```html
|
|
@@ -54,9 +53,9 @@ own assets behave exactly as before.
|
|
|
54
53
|
## Options
|
|
55
54
|
|
|
56
55
|
```js
|
|
57
|
-
//
|
|
56
|
+
// spark.config.js
|
|
58
57
|
offlineSw({
|
|
59
|
-
file: 'spark-sw.js', //
|
|
58
|
+
file: 'spark-sw.js', // written worker file name
|
|
60
59
|
include: ['/components/'], // ALSO cache these same-origin paths
|
|
61
60
|
exclude: ['/api/'], // never touch these (substring match)
|
|
62
61
|
cacheName: 'spark-offline-v1',
|
|
@@ -77,7 +76,7 @@ just without the safety net.
|
|
|
77
76
|
|
|
78
77
|
## No build step?
|
|
79
78
|
|
|
80
|
-
You don't need
|
|
79
|
+
You don't need the build step. Generate the worker once and host it next to
|
|
81
80
|
`index.html`:
|
|
82
81
|
|
|
83
82
|
```js
|
|
@@ -96,7 +95,33 @@ Then call `offline()` from any `<script type="module">`.
|
|
|
96
95
|
| `swSource(options?)` | The full worker source as a string. |
|
|
97
96
|
| `shouldHandle(url, origin, config?)` | The matching rule the worker uses (exported for tests/tooling). |
|
|
98
97
|
| `CACHE_NAME` | Default cache bucket name (`'spark-offline-v1'`). |
|
|
99
|
-
| `spark-html-offline/
|
|
98
|
+
| `spark-html-offline/bun` | Build step — writes the worker in build, serves it in dev. |
|
|
99
|
+
|
|
100
|
+
## The Spark family
|
|
101
|
+
|
|
102
|
+
Small, single-purpose packages that share one philosophy: no compiler, no
|
|
103
|
+
virtual DOM, no build step required. Add only what you use.
|
|
104
|
+
|
|
105
|
+
| Package | What it does |
|
|
106
|
+
|---|---|
|
|
107
|
+
| [`spark-html`](https://www.npmjs.com/package/spark-html) | The runtime — components, reactivity, stores, forms, scoped styles. 13 kB gzip, 0 deps. |
|
|
108
|
+
| [`spark-html-router`](https://www.npmjs.com/package/spark-html-router) | `<template route>` routing — nested routes/layouts, `route.query`, active links. |
|
|
109
|
+
| [`spark-html-theme`](https://www.npmjs.com/package/spark-html-theme) | Dark/light/system theming in one line — persisted, no flash. |
|
|
110
|
+
| [`spark-html-head`](https://www.npmjs.com/package/spark-html-head) | Reactive `<title>`/`<meta>` per route + a `head` store. |
|
|
111
|
+
| [`spark-html-motion`](https://www.npmjs.com/package/spark-html-motion) | Enter/leave transitions on if/each blocks — `transition="fade|slide|scale"`. |
|
|
112
|
+
| [`spark-html-devtools`](https://www.npmjs.com/package/spark-html-devtools) | In-page devtools — live stores, component tree, patch activity. |
|
|
113
|
+
| [`spark-html-query`](https://www.npmjs.com/package/spark-html-query) | Declarative async data — a self-fetching store (`loading`/`error`/`data`/`refetch`). |
|
|
114
|
+
| [`spark-html-persist`](https://www.npmjs.com/package/spark-html-persist) | Persist stores to localStorage/sessionStorage in one line. |
|
|
115
|
+
| [`spark-html-websocket`](https://www.npmjs.com/package/spark-html-websocket) | A WebSocket as a reactive store — auto-reconnect, JSON, `send()`. |
|
|
116
|
+
| [`spark-prerender`](https://www.npmjs.com/package/spark-prerender) | Build-time SEO prerender + sitemap/robots — no SSR server. |
|
|
117
|
+
| [`spark-html-image`](https://www.npmjs.com/package/spark-html-image) | Build-time image optimization — webp/avif + responsive `srcset`, zero config. |
|
|
118
|
+
| [`spark-html-font`](https://www.npmjs.com/package/spark-html-font) | Font loading optimizer — preload + size-adjusted fallbacks, no FOUT. |
|
|
119
|
+
| [`spark-html-manifest`](https://www.npmjs.com/package/spark-html-manifest) | PWA manifest + icons + head tags (and optional service worker) from one config. |
|
|
120
|
+
| [`spark-html-offline`](https://www.npmjs.com/package/spark-html-offline) | Offline URL imports — a service worker that caches CDN components. |
|
|
121
|
+
| [`spark-html-sri`](https://www.npmjs.com/package/spark-html-sri) | Subresource Integrity — hash + verify assets and remote components. |
|
|
122
|
+
| [`create-spark-html-app`](https://www.npmjs.com/package/create-spark-html-app) | Scaffold a spark-html app in one command. |
|
|
123
|
+
| [`prettier-plugin-spark`](https://www.npmjs.com/package/prettier-plugin-spark) | Prettier for components — formats `<script>`/`<style>`, markup stays byte-for-byte. |
|
|
124
|
+
| [`spark-html-language-server`](https://www.npmjs.com/package/spark-html-language-server) | LSP — diagnostics, go-to-definition, prop autocomplete, hover docs. |
|
|
100
125
|
|
|
101
126
|
## License
|
|
102
127
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "spark-html-offline",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Offline-capable URL imports for spark-html — a tiny service worker that caches CDN-imported components on first fetch and serves them when the network is gone. Zero dependencies.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.js",
|
|
@@ -11,9 +11,8 @@
|
|
|
11
11
|
"types": "./src/index.d.ts",
|
|
12
12
|
"default": "./src/index.js"
|
|
13
13
|
},
|
|
14
|
-
"./
|
|
15
|
-
"
|
|
16
|
-
"default": "./src/vite.js"
|
|
14
|
+
"./bun": {
|
|
15
|
+
"default": "./src/bun.js"
|
|
17
16
|
}
|
|
18
17
|
},
|
|
19
18
|
"scripts": {
|
package/src/bun.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* spark-html-offline/bun — emit + serve the worker file, as a spark-html-bun
|
|
3
|
+
* pipeline step.
|
|
4
|
+
*
|
|
5
|
+
* • run({ outDir }) — write the generated service worker into the output dir
|
|
6
|
+
* (default /spark-sw.js) so offline() finds it in production.
|
|
7
|
+
* • devRoutes() — serve the same source in dev, so the worker can be
|
|
8
|
+
* exercised locally too (it only touches cross-origin URLs by default, so
|
|
9
|
+
* HMR and local files are unaffected).
|
|
10
|
+
*
|
|
11
|
+
* import offline from 'spark-html-offline/bun';
|
|
12
|
+
* export default { pipeline: [offline({ include: ['/components/'] })] };
|
|
13
|
+
*/
|
|
14
|
+
import { join, resolve } from 'node:path';
|
|
15
|
+
import { writeFile } from 'node:fs/promises';
|
|
16
|
+
import { existsSync } from 'node:fs';
|
|
17
|
+
import { swSource } from './index.js';
|
|
18
|
+
|
|
19
|
+
export default function sparkOffline(options = {}) {
|
|
20
|
+
const file = (options.file || 'spark-sw.js').replace(/^\//, '');
|
|
21
|
+
const source = swSource(options);
|
|
22
|
+
return {
|
|
23
|
+
name: 'spark-html-offline',
|
|
24
|
+
async run({ outDir }) {
|
|
25
|
+
const root = resolve(outDir);
|
|
26
|
+
if (!existsSync(root)) return;
|
|
27
|
+
await writeFile(join(root, file), source, 'utf8');
|
|
28
|
+
},
|
|
29
|
+
devRoutes() {
|
|
30
|
+
return { [`/${file}`]: { type: 'text/javascript', body: () => source } };
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
package/src/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/** Options shared by the worker generator and the
|
|
1
|
+
/** Options shared by the worker generator and the build step. */
|
|
2
2
|
export interface OfflineSwOptions {
|
|
3
3
|
/** Same-origin URL substrings to cache too (e.g. ['/components/']). */
|
|
4
4
|
include?: string[];
|
|
@@ -39,8 +39,8 @@ declare const _default: {
|
|
|
39
39
|
};
|
|
40
40
|
export default _default;
|
|
41
41
|
|
|
42
|
-
/**
|
|
43
|
-
export interface
|
|
44
|
-
/**
|
|
42
|
+
/** spark-html-bun build step: writes the worker in build, serves it in dev. */
|
|
43
|
+
export interface OfflineBuildOptions extends OfflineSwOptions {
|
|
44
|
+
/** Written file name (default 'spark-sw.js'). */
|
|
45
45
|
file?: string;
|
|
46
46
|
}
|
package/src/index.js
CHANGED
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
* import { offline } from 'spark-html-offline';
|
|
13
13
|
* offline();
|
|
14
14
|
*
|
|
15
|
-
* //
|
|
16
|
-
* import offlineSw from 'spark-html-offline/
|
|
15
|
+
* // spark.config.js — writes + serves the worker file for you
|
|
16
|
+
* import offlineSw from 'spark-html-offline/bun';
|
|
17
17
|
* plugins: [spark(), offlineSw()]
|
|
18
18
|
*
|
|
19
19
|
* Works with any CDN (esm.sh, unpkg, jsdelivr, your own). By default it
|
|
@@ -53,7 +53,7 @@ export function shouldHandle(url, origin, config) {
|
|
|
53
53
|
|
|
54
54
|
/**
|
|
55
55
|
* The service-worker source, as a string — write it to a file served from
|
|
56
|
-
* your origin (the
|
|
56
|
+
* your origin (the bun build step does this for you as /spark-sw.js).
|
|
57
57
|
*
|
|
58
58
|
* Strategy: cache-first with background revalidation (stale-while-
|
|
59
59
|
* revalidate). First visit fetches + caches; every visit after serves
|
package/src/vite.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { OfflineVitePluginOptions } from './index.js';
|
|
2
|
-
|
|
3
|
-
/** Vite plugin: emits the service worker in build, serves it in dev. */
|
|
4
|
-
export default function sparkOffline(options?: OfflineVitePluginOptions): {
|
|
5
|
-
name: string;
|
|
6
|
-
configureServer(server: unknown): void;
|
|
7
|
-
generateBundle(): void;
|
|
8
|
-
};
|
package/src/vite.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* spark-html-offline/vite — emit + serve the worker file.
|
|
3
|
-
*
|
|
4
|
-
* Build: writes the generated service worker into the output dir (default
|
|
5
|
-
* name /spark-sw.js) so `offline()` finds it in production.
|
|
6
|
-
* Dev: serves the same source from the dev server, so the worker can be
|
|
7
|
-
* exercised locally too (it only touches cross-origin URLs by default, so
|
|
8
|
-
* HMR and local files are unaffected).
|
|
9
|
-
*
|
|
10
|
-
* import offlineSw from 'spark-html-offline/vite';
|
|
11
|
-
* plugins: [spark(), offlineSw({ include: ['/components/'] })]
|
|
12
|
-
*/
|
|
13
|
-
import { swSource } from './index.js';
|
|
14
|
-
|
|
15
|
-
export default function sparkOffline(options = {}) {
|
|
16
|
-
const file = (options.file || 'spark-sw.js').replace(/^\//, '');
|
|
17
|
-
const source = swSource(options);
|
|
18
|
-
return {
|
|
19
|
-
name: 'spark-html-offline',
|
|
20
|
-
configureServer(server) {
|
|
21
|
-
server.middlewares.use((req, res, next) => {
|
|
22
|
-
if ((req.url || '').split('?')[0] === `/${file}`) {
|
|
23
|
-
res.setHeader('Content-Type', 'text/javascript');
|
|
24
|
-
res.setHeader('Cache-Control', 'no-cache');
|
|
25
|
-
res.end(source);
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
next();
|
|
29
|
-
});
|
|
30
|
-
},
|
|
31
|
-
generateBundle() {
|
|
32
|
-
this.emitFile({ type: 'asset', fileName: file, source });
|
|
33
|
-
},
|
|
34
|
-
};
|
|
35
|
-
}
|