diff --git a/web/build/__tests__/utils.spec.ts b/web/build/__tests__/utils.spec.ts new file mode 100644 index 00000000..39070116 --- /dev/null +++ b/web/build/__tests__/utils.spec.ts @@ -0,0 +1,14 @@ +import { workboxPluginPattern } from '../utils'; + +describe('workboxPluginPattern', () => { + test.each([ + ['/plugins/com.msgbyte.foo/bar.js', true], + ['/plugins/com.msgbyte.foo.foz/bar.js', true], + ['/plugins/com.msgbyte.foo/index.js', false], + ['/plugins/com.msgbyte.foo/index-abcde.js', false], // TODO: 这个期望是true的。但是不会写正则 + ['/plugins/com.msgbyte.foo/index.woff', false], + ['/plugins/com.msgbyte.foo/font.woff', false], + ])('%s: %p', (input, output) => { + expect(workboxPluginPattern.test(input)).toBe(output); + }); +}); diff --git a/web/build/utils.ts b/web/build/utils.ts new file mode 100644 index 00000000..15c90526 --- /dev/null +++ b/web/build/utils.ts @@ -0,0 +1,5 @@ +/** + * workbox 匹配plugin的缓存 + */ +export const workboxPluginPattern = + /plugins\/com\.msgbyte(.*?)\/((?!index).)*?\.js/; diff --git a/web/build/webpack.config.ts b/web/build/webpack.config.ts index 4e2d37a8..c656e3a4 100644 --- a/web/build/webpack.config.ts +++ b/web/build/webpack.config.ts @@ -12,6 +12,7 @@ import CopyPlugin from 'copy-webpack-plugin'; import TsconfigPathsPlugin from 'tsconfig-paths-webpack-plugin'; import fs from 'fs'; import WorkboxPlugin from 'workbox-webpack-plugin'; +import { workboxPluginPattern } from './utils'; // eslint-disable-next-line @typescript-eslint/no-var-requires require('dotenv').config(); @@ -185,7 +186,7 @@ const config: Configuration = { }, { // 匹配内置 plugins 以加速 - urlPattern: /plugins\/com\.msgbyte(.*?)\.js/, + urlPattern: workboxPluginPattern, handler: 'CacheFirst', options: { cacheName: 'builtin-plugins',