Skip to content

Commit 3654e22

Browse files
committed
Enhance Vite configuration by refining caching strategies to only apply to same-origin requests for assets and PWA paths. This improves cache management and ensures that external requests are not inadvertently cached.
1 parent c79a850 commit 3654e22

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

vite.config.mjs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ export default defineConfig({
3838
navigateFallback: 'index.html',
3939
runtimeCaching: [
4040
{
41-
urlPattern: ({ url }) => url.pathname.startsWith('/assets/'),
41+
urlPattern: ({ url, sameOrigin }) => {
42+
return sameOrigin && url.pathname.startsWith('/assets/');
43+
},
4244
handler: 'CacheFirst',
4345
options: {
4446
cacheName: 'assets-cache',
@@ -52,7 +54,9 @@ export default defineConfig({
5254
}
5355
},
5456
{
55-
urlPattern: ({ url }) => url.pathname.startsWith('/pwa/'),
57+
urlPattern: ({ url, sameOrigin }) => {
58+
return sameOrigin && url.pathname.startsWith('/pwa/');
59+
},
5660
handler: 'StaleWhileRevalidate',
5761
options: {
5862
cacheName: 'pwa-cache',
@@ -66,8 +70,9 @@ export default defineConfig({
6670
}
6771
},
6872
{
69-
// 匹配除了上述规则外的所有请求
70-
urlPattern: ({ url }) => {
73+
// 匹配当前域名下除了上述规则外的所有请求
74+
urlPattern: ({ url, sameOrigin }) => {
75+
if (!sameOrigin) return false;
7176
const path = url.pathname;
7277
// 排除已经由其他规则处理的路径
7378
return !(path.includes('/assets/') || path.includes('/pwa/'));
@@ -85,7 +90,6 @@ export default defineConfig({
8590
}
8691
}
8792
},
88-
8993
],
9094
additionalManifestEntries: [],
9195
clientsClaim: true,

0 commit comments

Comments
 (0)