@serwist/svelte 9.0.0-preview.10

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Google LLC, 2019 ShadowWalker w@weiw.io https://weiw.io, 2023 Serwist
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 ADDED
@@ -0,0 +1 @@
1
+ This module's documentation can be found at https://serwist.pages.dev/docs/svelte
@@ -0,0 +1,52 @@
1
+ /// <reference types="@sveltejs/kit" />
2
+ import type { PrecacheEntry } from "@serwist/precaching";
3
+ import type { RuntimeCaching } from "@serwist/sw";
4
+ import { build as immutableAssets, files as staticAssets, prerendered as prerenderedRoutes, version as serviceWorkerVersion } from "$service-worker";
5
+ export { immutableAssets, staticAssets, prerenderedRoutes, serviceWorkerVersion };
6
+ export type StaticRevisions = {
7
+ [url: string]: string | null;
8
+ };
9
+ export interface GetPrecacheManifestOptions {
10
+ /**
11
+ * Whether immutable assets, as in assets that are generated by
12
+ * Vite, should be precached.
13
+ *
14
+ * @default true
15
+ */
16
+ precacheImmutable?: boolean;
17
+ /**
18
+ * Whether static assets should be precached.
19
+ *
20
+ * @default true
21
+ */
22
+ precacheStatic?: boolean;
23
+ /**
24
+ * Whether prerendered routes should be precached. This
25
+ * includes their __data.json files.
26
+ *
27
+ * @default true
28
+ */
29
+ precachePrerendered?: boolean;
30
+ /**
31
+ * An object that maps URLs of static assets to custom `revision`'s.
32
+ * If an URL doesn't exist in the object, `$service-worker.version`
33
+ * will be used instead.
34
+ *
35
+ * This helps prevent such assets, which are unlikely to change, from
36
+ * being invalidated every time the service worker is installed.
37
+ *
38
+ * Note: you should prefix these URLs with `$service-worker.base`.
39
+ */
40
+ staticRevisions?: StaticRevisions;
41
+ }
42
+ /**
43
+ * Retrieves the precache manifest generated by SvelteKit. If further
44
+ * modifications are needed, simply mutate the resulting array.
45
+ *
46
+ * @param options
47
+ * @returns
48
+ */
49
+ export declare const getPrecacheManifest: ({ precacheImmutable, precacheStatic, precachePrerendered, staticRevisions, }?: GetPrecacheManifestOptions) => PrecacheEntry[];
50
+ export declare const defaultIgnoreUrlParameters: RegExp[];
51
+ export declare const defaultCache: RuntimeCaching[];
52
+ //# sourceMappingURL=index.worker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.worker.d.ts","sourceRoot":"","sources":["../src/index.worker.ts"],"names":[],"mappings":";AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAElD,OAAO,EAAE,KAAK,IAAI,eAAe,EAAE,KAAK,IAAI,YAAY,EAAE,WAAW,IAAI,iBAAiB,EAAE,OAAO,IAAI,oBAAoB,EAAE,MAAM,iBAAiB,CAAC;AAErJ,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,CAAC;AAElF,MAAM,MAAM,eAAe,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAA;CAAE,CAAC;AAE/D,MAAM,WAAW,0BAA0B;IACzC;;;;;OAKG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B;;;;OAIG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB;;;;;OAKG;IACH,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B;;;;;;;;;OASG;IACH,eAAe,CAAC,EAAE,eAAe,CAAC;CACnC;AAED;;;;;;GAMG;AACH,eAAO,MAAM,mBAAmB,kFAK7B,0BAA0B,KAAQ,aAAa,EAajD,CAAC;AAEF,eAAO,MAAM,0BAA0B,UAAgC,CAAC;AAExE,eAAO,MAAM,YAAY,EAAE,cAAc,EAoGxC,CAAC"}
@@ -0,0 +1,131 @@
1
+ import { ExpirationPlugin } from '@serwist/expiration';
2
+ import { CacheFirst, StaleWhileRevalidate, NetworkFirst } from '@serwist/strategies';
3
+ import { build, files, prerendered, version } from '$service-worker';
4
+ export { build as immutableAssets, prerendered as prerenderedRoutes, version as serviceWorkerVersion, files as staticAssets } from '$service-worker';
5
+
6
+ const getPrecacheManifest = ({ precacheImmutable = true, precacheStatic = true, precachePrerendered = true, staticRevisions } = {})=>{
7
+ const staticMapper = (url)=>{
8
+ const revision = staticRevisions !== undefined && url in staticRevisions ? staticRevisions[url] : version;
9
+ return {
10
+ url,
11
+ revision
12
+ };
13
+ };
14
+ return [
15
+ ...precacheImmutable ? build.map((url)=>({
16
+ url,
17
+ revision: null
18
+ })) : [],
19
+ ...precacheStatic ? files.map(staticMapper) : [],
20
+ ...precachePrerendered ? prerendered.map((url)=>({
21
+ url,
22
+ revision: version
23
+ })) : []
24
+ ];
25
+ };
26
+ const defaultIgnoreUrlParameters = [
27
+ /^x-sveltekit-invalidated$/
28
+ ];
29
+ const defaultCache = [
30
+ {
31
+ matcher: /^https:\/\/fonts\.(?:googleapis|gstatic)\.com\/.*/i,
32
+ handler: new CacheFirst({
33
+ cacheName: "google-fonts",
34
+ plugins: [
35
+ new ExpirationPlugin({
36
+ maxEntries: 4,
37
+ maxAgeSeconds: 365 * 24 * 60 * 60
38
+ })
39
+ ]
40
+ })
41
+ },
42
+ {
43
+ matcher: /\.(?:eot|otf|ttc|ttf|woff|woff2|font.css)$/i,
44
+ handler: new StaleWhileRevalidate({
45
+ cacheName: "static-font-assets",
46
+ plugins: [
47
+ new ExpirationPlugin({
48
+ maxEntries: 4,
49
+ maxAgeSeconds: 7 * 24 * 60 * 60
50
+ })
51
+ ]
52
+ })
53
+ },
54
+ {
55
+ matcher: /\.(?:jpg|jpeg|gif|png|svg|ico|webp)$/i,
56
+ handler: new StaleWhileRevalidate({
57
+ cacheName: "static-image-assets",
58
+ plugins: [
59
+ new ExpirationPlugin({
60
+ maxEntries: 64,
61
+ maxAgeSeconds: 24 * 60 * 60
62
+ })
63
+ ]
64
+ })
65
+ },
66
+ {
67
+ matcher: /\.(?:js)$/i,
68
+ handler: new StaleWhileRevalidate({
69
+ cacheName: "static-js-assets",
70
+ plugins: [
71
+ new ExpirationPlugin({
72
+ maxEntries: 32,
73
+ maxAgeSeconds: 24 * 60 * 60
74
+ })
75
+ ]
76
+ })
77
+ },
78
+ {
79
+ matcher: /\.(?:css|less)$/i,
80
+ handler: new StaleWhileRevalidate({
81
+ cacheName: "static-style-assets",
82
+ plugins: [
83
+ new ExpirationPlugin({
84
+ maxEntries: 32,
85
+ maxAgeSeconds: 24 * 60 * 60
86
+ })
87
+ ]
88
+ })
89
+ },
90
+ {
91
+ matcher: /\.(?:json|xml|csv)$/i,
92
+ handler: new NetworkFirst({
93
+ cacheName: "static-data-assets",
94
+ plugins: [
95
+ new ExpirationPlugin({
96
+ maxEntries: 32,
97
+ maxAgeSeconds: 24 * 60 * 60
98
+ })
99
+ ]
100
+ })
101
+ },
102
+ {
103
+ matcher: /\/api\/.*$/i,
104
+ method: "GET",
105
+ handler: new NetworkFirst({
106
+ cacheName: "apis",
107
+ plugins: [
108
+ new ExpirationPlugin({
109
+ maxEntries: 16,
110
+ maxAgeSeconds: 24 * 60 * 60
111
+ })
112
+ ],
113
+ networkTimeoutSeconds: 10
114
+ })
115
+ },
116
+ {
117
+ matcher: /.*/i,
118
+ handler: new NetworkFirst({
119
+ cacheName: "others",
120
+ plugins: [
121
+ new ExpirationPlugin({
122
+ maxEntries: 32,
123
+ maxAgeSeconds: 24 * 60 * 60
124
+ })
125
+ ],
126
+ networkTimeoutSeconds: 10
127
+ })
128
+ }
129
+ ];
130
+
131
+ export { defaultCache, defaultIgnoreUrlParameters, getPrecacheManifest };
package/package.json ADDED
@@ -0,0 +1,72 @@
1
+ {
2
+ "name": "@serwist/svelte",
3
+ "version": "9.0.0-preview.10",
4
+ "type": "module",
5
+ "description": "A module that integrates Serwist into your Svelte application.",
6
+ "files": [
7
+ "src",
8
+ "dist"
9
+ ],
10
+ "keywords": [
11
+ "serwist",
12
+ "serwistjs",
13
+ "sw",
14
+ "service worker",
15
+ "progressive web apps",
16
+ "web",
17
+ "service-worker",
18
+ "progressive-web-apps",
19
+ "svelte",
20
+ "sveltekit",
21
+ "pwa"
22
+ ],
23
+ "author": "Serwist's Team",
24
+ "license": "MIT",
25
+ "repository": "serwist/serwist",
26
+ "bugs": "https://github.com/serwist/serwist/issues",
27
+ "homepage": "https://serwist.pages.dev",
28
+ "typesVersions": {
29
+ "*": {
30
+ "worker": [
31
+ "./dist/index.worker.d.ts"
32
+ ]
33
+ }
34
+ },
35
+ "exports": {
36
+ "./worker": {
37
+ "types": "./dist/index.worker.d.ts",
38
+ "default": "./dist/index.worker.js"
39
+ },
40
+ "./package.json": "./package.json"
41
+ },
42
+ "dependencies": {
43
+ "@serwist/expiration": "9.0.0-preview.10",
44
+ "@serwist/strategies": "9.0.0-preview.10"
45
+ },
46
+ "devDependencies": {
47
+ "@sveltejs/kit": "2.5.0",
48
+ "rollup": "4.9.6",
49
+ "@serwist/constants": "9.0.0-preview.10",
50
+ "@serwist/precaching": "9.0.0-preview.10",
51
+ "@serwist/sw": "9.0.0-preview.10"
52
+ },
53
+ "peerDependencies": {
54
+ "@sveltejs/kit": ">=2.0.0",
55
+ "typescript": ">=5.0.0",
56
+ "@serwist/sw": "9.0.0-preview.10"
57
+ },
58
+ "peerDependenciesMeta": {
59
+ "@serwist/sw": {
60
+ "optional": true
61
+ },
62
+ "typescript": {
63
+ "optional": true
64
+ }
65
+ },
66
+ "scripts": {
67
+ "build": "rimraf dist && cross-env NODE_ENV=production rollup --config rollup.config.js",
68
+ "dev": "rollup --config rollup.config.js --watch",
69
+ "lint": "biome lint ./src",
70
+ "typecheck": "tsc"
71
+ }
72
+ }
@@ -0,0 +1,175 @@
1
+ import { ExpirationPlugin } from "@serwist/expiration";
2
+ import type { PrecacheEntry } from "@serwist/precaching";
3
+ import { CacheFirst, NetworkFirst, StaleWhileRevalidate } from "@serwist/strategies";
4
+ import type { RuntimeCaching } from "@serwist/sw";
5
+
6
+ import { build as immutableAssets, files as staticAssets, prerendered as prerenderedRoutes, version as serviceWorkerVersion } from "$service-worker";
7
+
8
+ export { immutableAssets, staticAssets, prerenderedRoutes, serviceWorkerVersion };
9
+
10
+ export type StaticRevisions = { [url: string]: string | null };
11
+
12
+ export interface GetPrecacheManifestOptions {
13
+ /**
14
+ * Whether immutable assets, as in assets that are generated by
15
+ * Vite, should be precached.
16
+ *
17
+ * @default true
18
+ */
19
+ precacheImmutable?: boolean;
20
+ /**
21
+ * Whether static assets should be precached.
22
+ *
23
+ * @default true
24
+ */
25
+ precacheStatic?: boolean;
26
+ /**
27
+ * Whether prerendered routes should be precached. This
28
+ * includes their __data.json files.
29
+ *
30
+ * @default true
31
+ */
32
+ precachePrerendered?: boolean;
33
+ /**
34
+ * An object that maps URLs of static assets to custom `revision`'s.
35
+ * If an URL doesn't exist in the object, `$service-worker.version`
36
+ * will be used instead.
37
+ *
38
+ * This helps prevent such assets, which are unlikely to change, from
39
+ * being invalidated every time the service worker is installed.
40
+ *
41
+ * Note: you should prefix these URLs with `$service-worker.base`.
42
+ */
43
+ staticRevisions?: StaticRevisions;
44
+ }
45
+
46
+ /**
47
+ * Retrieves the precache manifest generated by SvelteKit. If further
48
+ * modifications are needed, simply mutate the resulting array.
49
+ *
50
+ * @param options
51
+ * @returns
52
+ */
53
+ export const getPrecacheManifest = ({
54
+ precacheImmutable = true,
55
+ precacheStatic = true,
56
+ precachePrerendered = true,
57
+ staticRevisions,
58
+ }: GetPrecacheManifestOptions = {}): PrecacheEntry[] => {
59
+ const staticMapper = (url: string): PrecacheEntry => {
60
+ const revision = staticRevisions !== undefined && url in staticRevisions ? staticRevisions[url] : serviceWorkerVersion;
61
+ return { url, revision };
62
+ };
63
+ return [
64
+ // Immutable files generated by Vite.
65
+ ...(precacheImmutable ? immutableAssets.map((url) => ({ url, revision: null }) satisfies PrecacheEntry) : []),
66
+ // Files in the static directory.
67
+ ...(precacheStatic ? staticAssets.map(staticMapper) : []),
68
+ // Prerendered routes.
69
+ ...(precachePrerendered ? prerenderedRoutes.map((url) => ({ url, revision: serviceWorkerVersion }) satisfies PrecacheEntry) : []),
70
+ ];
71
+ };
72
+
73
+ export const defaultIgnoreUrlParameters = [/^x-sveltekit-invalidated$/];
74
+
75
+ export const defaultCache: RuntimeCaching[] = [
76
+ {
77
+ matcher: /^https:\/\/fonts\.(?:googleapis|gstatic)\.com\/.*/i,
78
+ handler: new CacheFirst({
79
+ cacheName: "google-fonts",
80
+ plugins: [
81
+ new ExpirationPlugin({
82
+ maxEntries: 4,
83
+ maxAgeSeconds: 365 * 24 * 60 * 60, // 365 days
84
+ }),
85
+ ],
86
+ }),
87
+ },
88
+ {
89
+ matcher: /\.(?:eot|otf|ttc|ttf|woff|woff2|font.css)$/i,
90
+ handler: new StaleWhileRevalidate({
91
+ cacheName: "static-font-assets",
92
+ plugins: [
93
+ new ExpirationPlugin({
94
+ maxEntries: 4,
95
+ maxAgeSeconds: 7 * 24 * 60 * 60, // 7 days
96
+ }),
97
+ ],
98
+ }),
99
+ },
100
+ {
101
+ matcher: /\.(?:jpg|jpeg|gif|png|svg|ico|webp)$/i,
102
+ handler: new StaleWhileRevalidate({
103
+ cacheName: "static-image-assets",
104
+ plugins: [
105
+ new ExpirationPlugin({
106
+ maxEntries: 64,
107
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
108
+ }),
109
+ ],
110
+ }),
111
+ },
112
+ {
113
+ matcher: /\.(?:js)$/i,
114
+ handler: new StaleWhileRevalidate({
115
+ cacheName: "static-js-assets",
116
+ plugins: [
117
+ new ExpirationPlugin({
118
+ maxEntries: 32,
119
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
120
+ }),
121
+ ],
122
+ }),
123
+ },
124
+ {
125
+ matcher: /\.(?:css|less)$/i,
126
+ handler: new StaleWhileRevalidate({
127
+ cacheName: "static-style-assets",
128
+ plugins: [
129
+ new ExpirationPlugin({
130
+ maxEntries: 32,
131
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
132
+ }),
133
+ ],
134
+ }),
135
+ },
136
+ {
137
+ matcher: /\.(?:json|xml|csv)$/i,
138
+ handler: new NetworkFirst({
139
+ cacheName: "static-data-assets",
140
+ plugins: [
141
+ new ExpirationPlugin({
142
+ maxEntries: 32,
143
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
144
+ }),
145
+ ],
146
+ }),
147
+ },
148
+ {
149
+ matcher: /\/api\/.*$/i,
150
+ method: "GET",
151
+ handler: new NetworkFirst({
152
+ cacheName: "apis",
153
+ plugins: [
154
+ new ExpirationPlugin({
155
+ maxEntries: 16,
156
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
157
+ }),
158
+ ],
159
+ networkTimeoutSeconds: 10, // fallback to cache if API does not response within 10 seconds
160
+ }),
161
+ },
162
+ {
163
+ matcher: /.*/i,
164
+ handler: new NetworkFirst({
165
+ cacheName: "others",
166
+ plugins: [
167
+ new ExpirationPlugin({
168
+ maxEntries: 32,
169
+ maxAgeSeconds: 24 * 60 * 60, // 24 hours
170
+ }),
171
+ ],
172
+ networkTimeoutSeconds: 10,
173
+ }),
174
+ },
175
+ ];