@vite-pwa/vitepress 0.2.1 → 0.2.3

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/dist/index.cjs CHANGED
@@ -80,12 +80,44 @@ function withUserConfig(config) {
80
80
  }
81
81
  return head;
82
82
  };
83
+ let allowlist;
84
+ if (pwa.strategies !== "injectManifest" && pwa.experimental?.includeAllowlist === true) {
85
+ pwa.workbox = pwa.workbox ?? {};
86
+ allowlist = pwa.workbox.navigateFallbackAllowlist = pwa.workbox.navigateFallbackAllowlist ?? [];
87
+ pwa.workbox.runtimeCaching = pwa.workbox.runtimeCaching ?? [];
88
+ pwa.workbox.runtimeCaching.push({
89
+ urlPattern: ({ request, sameOrigin }) => {
90
+ return sameOrigin && request.mode === "navigate";
91
+ },
92
+ handler: "NetworkOnly",
93
+ options: {
94
+ plugins: [{
95
+ /* this callback will be called when the fetch call fails */
96
+ handlerDidError: async () => Response.redirect("404", 302),
97
+ /* this callback will prevent caching the response */
98
+ cacheWillUpdate: async () => null
99
+ }]
100
+ }
101
+ });
102
+ }
83
103
  vitePressConfig.buildEnd = async (siteConfig) => {
84
104
  await userBuildEnd?.(siteConfig);
85
- api && !api.disabled && await api.generateSW();
105
+ if (api && !api.disabled) {
106
+ if (typeof allowlist !== "undefined") {
107
+ const base = siteConfig.site.base ?? "/";
108
+ for (const page of siteConfig.pages) {
109
+ const regex = page === "index.md" ? escapeStringRegexp(base) : escapeStringRegexp(`${base}${page.replace(/\.md$/, "")}`);
110
+ allowlist.push(new RegExp(`^${regex}(\\.html)?$`));
111
+ }
112
+ }
113
+ await api.generateSW();
114
+ }
86
115
  };
87
116
  return vitePressConfig;
88
117
  }
118
+ function escapeStringRegexp(value) {
119
+ return value.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&").replace(/-/g, "\\x2d");
120
+ }
89
121
 
90
122
  async function withPwa(config) {
91
123
  if (typeof config === "function") {
package/dist/index.d.cts CHANGED
@@ -4,6 +4,16 @@ import { DefaultTheme, UserConfigExport } from 'vitepress';
4
4
  import { VitePWAOptions } from 'vite-plugin-pwa';
5
5
 
6
6
  interface PwaOptions extends Partial<VitePWAOptions> {
7
+ experimental?: {
8
+ /**
9
+ * When using `generateSW` strategy, include the logic to handle the `workbox.navigateFallbackAllowlist` option.
10
+ *
11
+ * @see https://github.com/vite-pwa/vitepress/issues/22
12
+ *
13
+ * @default false
14
+ */
15
+ includeAllowlist?: boolean;
16
+ };
7
17
  }
8
18
  declare module 'vitepress' {
9
19
  interface UserConfig {
package/dist/index.d.mts CHANGED
@@ -4,6 +4,16 @@ import { DefaultTheme, UserConfigExport } from 'vitepress';
4
4
  import { VitePWAOptions } from 'vite-plugin-pwa';
5
5
 
6
6
  interface PwaOptions extends Partial<VitePWAOptions> {
7
+ experimental?: {
8
+ /**
9
+ * When using `generateSW` strategy, include the logic to handle the `workbox.navigateFallbackAllowlist` option.
10
+ *
11
+ * @see https://github.com/vite-pwa/vitepress/issues/22
12
+ *
13
+ * @default false
14
+ */
15
+ includeAllowlist?: boolean;
16
+ };
7
17
  }
8
18
  declare module 'vitepress' {
9
19
  interface UserConfig {
package/dist/index.d.ts CHANGED
@@ -4,6 +4,16 @@ import { DefaultTheme, UserConfigExport } from 'vitepress';
4
4
  import { VitePWAOptions } from 'vite-plugin-pwa';
5
5
 
6
6
  interface PwaOptions extends Partial<VitePWAOptions> {
7
+ experimental?: {
8
+ /**
9
+ * When using `generateSW` strategy, include the logic to handle the `workbox.navigateFallbackAllowlist` option.
10
+ *
11
+ * @see https://github.com/vite-pwa/vitepress/issues/22
12
+ *
13
+ * @default false
14
+ */
15
+ includeAllowlist?: boolean;
16
+ };
7
17
  }
8
18
  declare module 'vitepress' {
9
19
  interface UserConfig {
package/dist/index.mjs CHANGED
@@ -78,12 +78,44 @@ function withUserConfig(config) {
78
78
  }
79
79
  return head;
80
80
  };
81
+ let allowlist;
82
+ if (pwa.strategies !== "injectManifest" && pwa.experimental?.includeAllowlist === true) {
83
+ pwa.workbox = pwa.workbox ?? {};
84
+ allowlist = pwa.workbox.navigateFallbackAllowlist = pwa.workbox.navigateFallbackAllowlist ?? [];
85
+ pwa.workbox.runtimeCaching = pwa.workbox.runtimeCaching ?? [];
86
+ pwa.workbox.runtimeCaching.push({
87
+ urlPattern: ({ request, sameOrigin }) => {
88
+ return sameOrigin && request.mode === "navigate";
89
+ },
90
+ handler: "NetworkOnly",
91
+ options: {
92
+ plugins: [{
93
+ /* this callback will be called when the fetch call fails */
94
+ handlerDidError: async () => Response.redirect("404", 302),
95
+ /* this callback will prevent caching the response */
96
+ cacheWillUpdate: async () => null
97
+ }]
98
+ }
99
+ });
100
+ }
81
101
  vitePressConfig.buildEnd = async (siteConfig) => {
82
102
  await userBuildEnd?.(siteConfig);
83
- api && !api.disabled && await api.generateSW();
103
+ if (api && !api.disabled) {
104
+ if (typeof allowlist !== "undefined") {
105
+ const base = siteConfig.site.base ?? "/";
106
+ for (const page of siteConfig.pages) {
107
+ const regex = page === "index.md" ? escapeStringRegexp(base) : escapeStringRegexp(`${base}${page.replace(/\.md$/, "")}`);
108
+ allowlist.push(new RegExp(`^${regex}(\\.html)?$`));
109
+ }
110
+ }
111
+ await api.generateSW();
112
+ }
84
113
  };
85
114
  return vitePressConfig;
86
115
  }
116
+ function escapeStringRegexp(value) {
117
+ return value.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&").replace(/-/g, "\\x2d");
118
+ }
87
119
 
88
120
  async function withPwa(config) {
89
121
  if (typeof config === "function") {
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@vite-pwa/vitepress",
3
3
  "type": "module",
4
- "version": "0.2.1",
5
- "packageManager": "pnpm@8.7.4",
4
+ "version": "0.2.3",
5
+ "packageManager": "pnpm@8.9.0",
6
6
  "description": "Zero-config PWA for VitePress",
7
7
  "author": "antfu <anthonyfu117@hotmail.com>",
8
8
  "license": "MIT",
@@ -66,4 +66,4 @@
66
66
  ]
67
67
  }
68
68
  }
69
- }
69
+ }