From 6596e83b605584ea20a6d7e657646e1507d44ba1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=B3=96=E9=A5=BC?= Date: Wed, 5 Jun 2024 17:56:17 +0800 Subject: [PATCH] feat: reduce the frequency of cache revalidation (#27) --- .changeset/little-parrots-roll.md | 5 ++ package.json | 2 +- pnpm-lock.yaml | 78 +++++++++++++++---------------- src/cache.ts | 11 ++++- 4 files changed, 54 insertions(+), 42 deletions(-) create mode 100644 .changeset/little-parrots-roll.md diff --git a/.changeset/little-parrots-roll.md b/.changeset/little-parrots-roll.md new file mode 100644 index 0000000..dea2dad --- /dev/null +++ b/.changeset/little-parrots-roll.md @@ -0,0 +1,5 @@ +--- +"@web-widget/shared-cache": patch +--- + +Reduce the frequency of cache revalidation. diff --git a/package.json b/package.json index 61fbed5..0a1bf84 100644 --- a/package.json +++ b/package.json @@ -70,7 +70,7 @@ "lru-cache": "^10.2.0" }, "dependencies": { - "@web-widget/http-cache-semantics": "^1.0.0", + "@web-widget/http-cache-semantics": "^1.1.0", "@web-widget/helpers": "^1.10.37" }, "packageManager": "pnpm@8.4.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4763303..1bcfcf8 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5,8 +5,8 @@ dependencies: specifier: ^1.10.37 version: 1.10.37 '@web-widget/http-cache-semantics': - specifier: ^1.0.0 - version: 1.0.0 + specifier: ^1.1.0 + version: 1.1.0 devDependencies: '@changesets/cli': @@ -20,10 +20,10 @@ devDependencies: version: 29.5.3 '@typescript-eslint/eslint-plugin': specifier: ^5.5.0 - version: 5.5.0(@typescript-eslint/parser@5.5.0)(eslint@8.3.0)(typescript@5.4.4) + version: 5.5.0(@typescript-eslint/parser@5.5.0)(eslint@8.3.0)(typescript@5.4.5) '@typescript-eslint/parser': specifier: ^5.5.0 - version: 5.5.0(eslint@8.3.0)(typescript@5.4.4) + version: 5.5.0(eslint@8.3.0)(typescript@5.4.5) c8: specifier: latest version: 9.1.0 @@ -59,22 +59,22 @@ devDependencies: version: 3.2.5 ts-jest: specifier: ^29.1.1 - version: 29.1.1(@babel/core@7.24.4)(esbuild@0.19.12)(jest@29.6.2)(typescript@5.4.4) + version: 29.1.1(@babel/core@7.24.4)(esbuild@0.19.12)(jest@29.6.2)(typescript@5.4.5) ts-node: specifier: ^10.4.0 - version: 10.4.0(@types/node@20.12.7)(typescript@5.4.4) + version: 10.4.0(@types/node@20.12.7)(typescript@5.4.5) tsup: specifier: ^8.0.1 - version: 8.0.1(ts-node@10.4.0)(typescript@5.4.4) + version: 8.0.1(ts-node@10.4.0)(typescript@5.4.5) typedoc: specifier: ^0.25.13 - version: 0.25.13(typescript@5.4.4) + version: 0.25.13(typescript@5.4.5) typedoc-plugin-markdown: specifier: ^3.17.1 version: 3.17.1(typedoc@0.25.13) typescript: specifier: latest - version: 5.4.4 + version: 5.4.5 packages: @@ -1744,7 +1744,7 @@ packages: '@types/yargs-parser': 21.0.3 dev: true - /@typescript-eslint/eslint-plugin@5.5.0(@typescript-eslint/parser@5.5.0)(eslint@8.3.0)(typescript@5.4.4): + /@typescript-eslint/eslint-plugin@5.5.0(@typescript-eslint/parser@5.5.0)(eslint@8.3.0)(typescript@5.4.5): resolution: {integrity: sha512-4bV6fulqbuaO9UMXU0Ia0o6z6if+kmMRW8rMRyfqXj/eGrZZRGedS4n0adeGNnjr8LKAM495hrQ7Tea52UWmQA==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} peerDependencies: @@ -1755,8 +1755,8 @@ packages: typescript: optional: true dependencies: - '@typescript-eslint/experimental-utils': 5.5.0(eslint@8.3.0)(typescript@5.4.4) - '@typescript-eslint/parser': 5.5.0(eslint@8.3.0)(typescript@5.4.4) + '@typescript-eslint/experimental-utils': 5.5.0(eslint@8.3.0)(typescript@5.4.5) + '@typescript-eslint/parser': 5.5.0(eslint@8.3.0)(typescript@5.4.5) '@typescript-eslint/scope-manager': 5.5.0 debug: 4.3.4 eslint: 8.3.0 @@ -1764,13 +1764,13 @@ packages: ignore: 5.3.1 regexpp: 3.2.0 semver: 7.6.0 - tsutils: 3.21.0(typescript@5.4.4) - typescript: 5.4.4 + tsutils: 3.21.0(typescript@5.4.5) + typescript: 5.4.5 transitivePeerDependencies: - supports-color dev: true - /@typescript-eslint/experimental-utils@5.5.0(eslint@8.3.0)(typescript@5.4.4): + /@typescript-eslint/experimental-utils@5.5.0(eslint@8.3.0)(typescript@5.4.5): resolution: {integrity: sha512-kjWeeVU+4lQ1SLYErRKV5yDXbWDPkpbzTUUlfAUifPYvpX0qZlrcCZ96/6oWxt3QxtK5WVhXz+KsnwW9cIW+3A==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} peerDependencies: @@ -1779,7 +1779,7 @@ packages: '@types/json-schema': 7.0.15 '@typescript-eslint/scope-manager': 5.5.0 '@typescript-eslint/types': 5.5.0 - '@typescript-eslint/typescript-estree': 5.5.0(typescript@5.4.4) + '@typescript-eslint/typescript-estree': 5.5.0(typescript@5.4.5) eslint: 8.3.0 eslint-scope: 5.1.1 eslint-utils: 3.0.0(eslint@8.3.0) @@ -1788,7 +1788,7 @@ packages: - typescript dev: true - /@typescript-eslint/parser@5.5.0(eslint@8.3.0)(typescript@5.4.4): + /@typescript-eslint/parser@5.5.0(eslint@8.3.0)(typescript@5.4.5): resolution: {integrity: sha512-JsXBU+kgQOAgzUn2jPrLA+Rd0Y1dswOlX3hp8MuRO1hQDs6xgHtbCXEiAu7bz5hyVURxbXcA2draasMbNqrhmg==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} peerDependencies: @@ -1800,10 +1800,10 @@ packages: dependencies: '@typescript-eslint/scope-manager': 5.5.0 '@typescript-eslint/types': 5.5.0 - '@typescript-eslint/typescript-estree': 5.5.0(typescript@5.4.4) + '@typescript-eslint/typescript-estree': 5.5.0(typescript@5.4.5) debug: 4.3.4 eslint: 8.3.0 - typescript: 5.4.4 + typescript: 5.4.5 transitivePeerDependencies: - supports-color dev: true @@ -1821,7 +1821,7 @@ packages: engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} dev: true - /@typescript-eslint/typescript-estree@5.5.0(typescript@5.4.4): + /@typescript-eslint/typescript-estree@5.5.0(typescript@5.4.5): resolution: {integrity: sha512-pVn8btYUiYrjonhMAO0yG8lm7RApzy2L4RC7Td/mC/qFkyf6vRbGyZozoA94+w6D2Y2GRqpMoCWcwx/EUOzyoQ==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} peerDependencies: @@ -1836,8 +1836,8 @@ packages: globby: 11.1.0 is-glob: 4.0.3 semver: 7.6.0 - tsutils: 3.21.0(typescript@5.4.4) - typescript: 5.4.4 + tsutils: 3.21.0(typescript@5.4.5) + typescript: 5.4.5 transitivePeerDependencies: - supports-color dev: true @@ -1865,8 +1865,8 @@ packages: '@web-widget/schema': 1.10.37 dev: false - /@web-widget/http-cache-semantics@1.0.0: - resolution: {integrity: sha512-VWFtljVjjWgyR0STdU9dMVBNRyteJ7CjK+XU9uxYOS5IdSSrfcJzp/fdH27h1rgXSW30QK82HN251vF86oGpqw==} + /@web-widget/http-cache-semantics@1.1.0: + resolution: {integrity: sha512-1iSfVDtYB/VWWhThEf7Bs15fhLG3JLMPcgX2xghkuohyp+Ml++8GMOcgucKPBTK/AYOy3Y+f1Wz+Ai2pnhjFAQ==} dev: false /@web-widget/schema@1.10.37: @@ -4007,7 +4007,7 @@ packages: pretty-format: 29.7.0 slash: 3.0.0 strip-json-comments: 3.1.1 - ts-node: 10.4.0(@types/node@20.12.7)(typescript@5.4.4) + ts-node: 10.4.0(@types/node@20.12.7)(typescript@5.4.5) transitivePeerDependencies: - babel-plugin-macros - supports-color @@ -5181,7 +5181,7 @@ packages: optional: true dependencies: lilconfig: 3.1.1 - ts-node: 10.4.0(@types/node@20.12.7)(typescript@5.4.4) + ts-node: 10.4.0(@types/node@20.12.7)(typescript@5.4.5) yaml: 2.4.1 dev: true @@ -5983,7 +5983,7 @@ packages: resolution: {integrity: sha512-Y/arvbn+rrz3JCKl9C4kVNfTfSm2/mEp5FSz5EsZSANGPSlQrpRI5M4PKF+mJnE52jOO90PnPSc3Ur3bTQw0gA==} dev: true - /ts-jest@29.1.1(@babel/core@7.24.4)(esbuild@0.19.12)(jest@29.6.2)(typescript@5.4.4): + /ts-jest@29.1.1(@babel/core@7.24.4)(esbuild@0.19.12)(jest@29.6.2)(typescript@5.4.5): resolution: {integrity: sha512-D6xjnnbP17cC85nliwGiL+tpoKN0StpgE0TeOjXQTU6MVCfsB4v7aW05CgQ/1OywGb0x/oy9hHFnN+sczTiRaA==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} hasBin: true @@ -6014,11 +6014,11 @@ packages: lodash.memoize: 4.1.2 make-error: 1.3.6 semver: 7.6.0 - typescript: 5.4.4 + typescript: 5.4.5 yargs-parser: 21.1.1 dev: true - /ts-node@10.4.0(@types/node@20.12.7)(typescript@5.4.4): + /ts-node@10.4.0(@types/node@20.12.7)(typescript@5.4.5): resolution: {integrity: sha512-g0FlPvvCXSIO1JDF6S232P5jPYqBkRL9qly81ZgAOSU7rwI0stphCgd2kLiCrU9DjQCrJMWEqcNSjQL02s6d8A==} hasBin: true peerDependencies: @@ -6044,7 +6044,7 @@ packages: create-require: 1.1.1 diff: 4.0.2 make-error: 1.3.6 - typescript: 5.4.4 + typescript: 5.4.5 yn: 3.1.1 dev: true @@ -6061,7 +6061,7 @@ packages: deprecated: no longer maintained dev: true - /tsup@8.0.1(ts-node@10.4.0)(typescript@5.4.4): + /tsup@8.0.1(ts-node@10.4.0)(typescript@5.4.5): resolution: {integrity: sha512-hvW7gUSG96j53ZTSlT4j/KL0q1Q2l6TqGBFc6/mu/L46IoNWqLLUzLRLP1R8Q7xrJTmkDxxDoojV5uCVs1sVOg==} engines: {node: '>=18'} hasBin: true @@ -6094,20 +6094,20 @@ packages: source-map: 0.8.0-beta.0 sucrase: 3.35.0 tree-kill: 1.2.2 - typescript: 5.4.4 + typescript: 5.4.5 transitivePeerDependencies: - supports-color - ts-node dev: true - /tsutils@3.21.0(typescript@5.4.4): + /tsutils@3.21.0(typescript@5.4.5): resolution: {integrity: sha512-mHKK3iUXL+3UF6xL5k0PEhKRUBKPBCv/+RkEOpjRWxxx27KKRBmmA60A9pgOUvMi8GKhRMPEmjBRPzs2W7O1OA==} engines: {node: '>= 6'} peerDependencies: typescript: '>=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta' dependencies: tslib: 1.14.1 - typescript: 5.4.4 + typescript: 5.4.5 dev: true /tty-table@4.2.3: @@ -6217,10 +6217,10 @@ packages: typedoc: '>=0.24.0' dependencies: handlebars: 4.7.8 - typedoc: 0.25.13(typescript@5.4.4) + typedoc: 0.25.13(typescript@5.4.5) dev: true - /typedoc@0.25.13(typescript@5.4.4): + /typedoc@0.25.13(typescript@5.4.5): resolution: {integrity: sha512-pQqiwiJ+Z4pigfOnnysObszLiU3mVLWAExSPf+Mu06G/qsc3wzbuM56SZQvONhHLncLUhYzOVkjFFpFfL5AzhQ==} engines: {node: '>= 16'} hasBin: true @@ -6231,11 +6231,11 @@ packages: marked: 4.3.0 minimatch: 9.0.4 shiki: 0.14.7 - typescript: 5.4.4 + typescript: 5.4.5 dev: true - /typescript@5.4.4: - resolution: {integrity: sha512-dGE2Vv8cpVvw28v8HCPqyb08EzbBURxDpuhJvTrusShUfGnhHBafDsLdS1EhhxyL6BJQE+2cT3dDPAv+MQ6oLw==} + /typescript@5.4.5: + resolution: {integrity: sha512-vcI4UpRgg81oIRUFwR0WSIHKt11nJ7SAVlYNIu+QpqeyXP+gpQJy/Z4+F0aGxSE4MqwjyXvW/TzgkLAx2AGHwQ==} engines: {node: '>=14.17'} hasBin: true dev: true diff --git a/src/cache.ts b/src/cache.ts index 64f728d..c35f1ca 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -170,7 +170,12 @@ export class SharedCache implements Cache { headers, }); - if (!forceCache && !policy.satisfiesWithoutRevalidation(r)) { + if ( + !forceCache && + !policy.satisfiesWithoutRevalidation(r, { + ignoreSearch: true, + }) + ) { if (policy.stale() && policy.useStaleWhileRevalidate()) { // Well actually, in this case it's fine to return the stale response. // But we'll update the cache in the background. @@ -334,7 +339,9 @@ export class SharedCache implements Cache { fetch: typeof globalThis.fetch ): Promise { const revalidationRequest = new Request(request, { - headers: resolveCacheItem.policy.revalidationHeaders(request), + headers: resolveCacheItem.policy.revalidationHeaders(request, { + ignoreSearch: true, + }), }); let revalidationResponse: Response;