Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(fetch-cache): remove zod from fetch cache #65079

Merged
merged 5 commits into from
Apr 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 4 additions & 26 deletions packages/next/src/server/lib/incremental-cache/fetch-cache.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import type { CacheHandler, CacheHandlerContext, CacheHandlerValue } from './'
import type {
CachedFetchValue,
IncrementalCacheValue,
} from '../../response-cache'
import type { 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,
Expand All @@ -31,18 +24,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<CachedFetchValue> = 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<string, string>
private cacheEndpoint?: string
Expand Down Expand Up @@ -253,16 +234,13 @@ 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: IncrementalCacheValue = await res.json()

if (!parsed.success) {
this.debug && console.log({ json })
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 ??= []
Expand Down
Loading