From 712f71a1611b95dfa49bccad9ee0277cf8253778 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Fri, 26 Apr 2024 11:36:19 +0200 Subject: [PATCH 1/4] =?UTF-8?q?Revert=20"fix(fetch-cache):=20fix=20additio?= =?UTF-8?q?nal=20typo,=20add=20type=20&=20data=20validation=20(#6=E2=80=A6?= =?UTF-8?q?"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 3d1f3cc1b4c2fcda5bbe1e6f368961ec33640058. --- .../lib/incremental-cache/fetch-cache.ts | 33 +++---------------- 1 file changed, 5 insertions(+), 28 deletions(-) diff --git a/packages/next/src/server/lib/incremental-cache/fetch-cache.ts b/packages/next/src/server/lib/incremental-cache/fetch-cache.ts index 9e8467f3176e1..134586d46fa05 100644 --- a/packages/next/src/server/lib/incremental-cache/fetch-cache.ts +++ b/packages/next/src/server/lib/incremental-cache/fetch-cache.ts @@ -1,14 +1,6 @@ import type { CacheHandler, CacheHandlerContext, CacheHandlerValue } from './' -import type { - CachedFetchValue, - IncrementalCacheValue, -} from '../../response-cache' import LRUCache from 'next/dist/compiled/lru-cache' - -import { z } from 'next/dist/compiled/zod' -import type zod from 'next/dist/compiled/zod' - import { CACHE_ONE_YEAR, NEXT_CACHE_SOFT_TAGS_HEADER, @@ -31,18 +23,6 @@ const CACHE_REVALIDATE_HEADER = 'x-vercel-revalidate' as const const CACHE_FETCH_URL_HEADER = 'x-vercel-cache-item-name' as const const CACHE_CONTROL_VALUE_HEADER = 'x-vercel-cache-control' as const -const zCachedFetchValue: zod.ZodType = z.object({ - kind: z.literal('FETCH'), - data: z.object({ - headers: z.record(z.string()), - body: z.string(), - url: z.string(), - status: z.number().optional(), - }), - tags: z.array(z.string()).optional(), - revalidate: z.number(), -}) - export default class FetchCache implements CacheHandler { private headers: Record private cacheEndpoint?: string @@ -253,22 +233,19 @@ export default class FetchCache implements CacheHandler { throw new Error(`invalid response from cache ${res.status}`) } - const json: IncrementalCacheValue = await res.json() - const parsed = zCachedFetchValue.safeParse(json) + const cached = await res.json() - if (!parsed.success) { - this.debug && console.log({ json }) - throw new Error('invalid cache value') + if (!cached || cached.kind !== 'FETCH') { + this.debug && console.log({ cached }) + throw new Error(`invalid cache value`) } - const { data: cached } = parsed - // if new tags were specified, merge those tags to the existing tags if (cached.kind === 'FETCH') { cached.tags ??= [] for (const tag of tags ?? []) { if (!cached.tags.includes(tag)) { - cached.tags.push(tag) + cached.tag.push(tag) } } } From 70f306794cabd4e2ac20264e112a6f007cb610df Mon Sep 17 00:00:00 2001 From: Sam Ko Date: Fri, 26 Apr 2024 07:43:26 -0700 Subject: [PATCH 2/4] chore: update to cached.tags --- packages/next/src/server/lib/incremental-cache/fetch-cache.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/next/src/server/lib/incremental-cache/fetch-cache.ts b/packages/next/src/server/lib/incremental-cache/fetch-cache.ts index 134586d46fa05..83876964f1987 100644 --- a/packages/next/src/server/lib/incremental-cache/fetch-cache.ts +++ b/packages/next/src/server/lib/incremental-cache/fetch-cache.ts @@ -245,7 +245,7 @@ export default class FetchCache implements CacheHandler { cached.tags ??= [] for (const tag of tags ?? []) { if (!cached.tags.includes(tag)) { - cached.tag.push(tag) + cached.tags.push(tag) } } } From 5c2c401f1b38c8f298ce02b89ce8a8497a9d8451 Mon Sep 17 00:00:00 2001 From: samcx Date: Fri, 26 Apr 2024 11:03:37 -0400 Subject: [PATCH 3/4] chore(fetch-cache): re-add correct type to json --- packages/next/src/server/lib/incremental-cache/fetch-cache.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/next/src/server/lib/incremental-cache/fetch-cache.ts b/packages/next/src/server/lib/incremental-cache/fetch-cache.ts index 83876964f1987..4dc83b518b2d7 100644 --- a/packages/next/src/server/lib/incremental-cache/fetch-cache.ts +++ b/packages/next/src/server/lib/incremental-cache/fetch-cache.ts @@ -1,4 +1,5 @@ import type { CacheHandler, CacheHandlerContext, CacheHandlerValue } from './' +import type { IncrementalCacheValue } from '../../response-cache' import LRUCache from 'next/dist/compiled/lru-cache' import { @@ -233,7 +234,7 @@ export default class FetchCache implements CacheHandler { throw new Error(`invalid response from cache ${res.status}`) } - const cached = await res.json() + const cached: IncrementalCacheValue = await res.json() if (!cached || cached.kind !== 'FETCH') { this.debug && console.log({ cached }) From ba3ef6b981c650d8d6ad10dd73c8b5e6241a3a13 Mon Sep 17 00:00:00 2001 From: samcx Date: Fri, 26 Apr 2024 11:15:18 -0400 Subject: [PATCH 4/4] chore(fetch-cache): switch from template string to regular string --- packages/next/src/server/lib/incremental-cache/fetch-cache.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/next/src/server/lib/incremental-cache/fetch-cache.ts b/packages/next/src/server/lib/incremental-cache/fetch-cache.ts index 4dc83b518b2d7..6b89c97ed2f8a 100644 --- a/packages/next/src/server/lib/incremental-cache/fetch-cache.ts +++ b/packages/next/src/server/lib/incremental-cache/fetch-cache.ts @@ -238,7 +238,7 @@ export default class FetchCache implements CacheHandler { if (!cached || cached.kind !== 'FETCH') { this.debug && console.log({ cached }) - throw new Error(`invalid cache value`) + throw new Error('invalid cache value') } // if new tags were specified, merge those tags to the existing tags