Unverified Commit aa614635 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update fetch cache key (#46513

x-ref: [slack
thread](https://vercel.slack.com/archives/C042LHPJ1NX/p1677461963151879?thread_ts=1677360662.652689&cid=C042LHPJ1NX)

## Bug

- [x] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)
parent d3e576db
...@@ -57,6 +57,7 @@ export class IncrementalCache { ...@@ -57,6 +57,7 @@ export class IncrementalCache {
prerenderManifest: PrerenderManifest prerenderManifest: PrerenderManifest
requestHeaders: Record<string, undefined | string | string[]> requestHeaders: Record<string, undefined | string | string[]>
minimalMode?: boolean minimalMode?: boolean
fetchCacheKeyPrefix?: string
constructor({ constructor({
fs, fs,
...@@ -103,6 +104,7 @@ export class IncrementalCache { ...@@ -103,6 +104,7 @@ export class IncrementalCache {
this.minimalMode = minimalMode this.minimalMode = minimalMode
this.requestHeaders = requestHeaders this.requestHeaders = requestHeaders
this.prerenderManifest = getPrerenderManifest() this.prerenderManifest = getPrerenderManifest()
this.fetchCacheKeyPrefix = fetchCacheKeyPrefix
if (CurCacheHandler) { if (CurCacheHandler) {
this.cacheHandler = new CurCacheHandler({ this.cacheHandler = new CurCacheHandler({
...@@ -148,75 +150,87 @@ export class IncrementalCache { ...@@ -148,75 +150,87 @@ export class IncrementalCache {
// x-ref: https://github.com/facebook/react/blob/2655c9354d8e1c54ba888444220f63e836925caa/packages/react/src/ReactFetch.js#L23 // x-ref: https://github.com/facebook/react/blob/2655c9354d8e1c54ba888444220f63e836925caa/packages/react/src/ReactFetch.js#L23
async fetchCacheKey(url: string, init: RequestInit = {}): Promise<string> { async fetchCacheKey(url: string, init: RequestInit = {}): Promise<string> {
// this should be bumped anytime a fix is made to cache entries
// that should bust the cache
const MAIN_KEY_PREFIX = 'v1'
let cacheKey: string let cacheKey: string
const bodyChunks: string[] = [] const bodyChunks: string[] = []
try { if (init.body) {
if (init.body) { // handle ReadableStream body
// handle ReadableStream body if (typeof (init.body as any).getReader === 'function') {
if (typeof (init.body as any).getReader === 'function') { const readableBody = init.body as ReadableStream
const readableBody = init.body as ReadableStream const reader = readableBody.getReader()
const [origBody, newBody] = readableBody.tee() let arrayBuffer = new Uint8Array()
init.body = origBody
const reader = newBody.getReader() function processValue({
function processValue({ done,
done, value,
value, }: {
}: { done?: boolean
done?: boolean value?: ArrayBuffer | string
value?: ArrayBuffer | string }): any {
}): any { if (done) {
if (done) { return
return
}
if (value) {
try {
bodyChunks.push(
typeof value === 'string' ? value : encode(value)
)
} catch (err) {
console.error(err)
}
}
return reader.read().then(processValue)
} }
await reader.read().then(processValue) if (value) {
} // handle FormData or URLSearchParams bodies try {
else if (typeof (init.body as any).keys === 'function') { bodyChunks.push(typeof value === 'string' ? value : encode(value))
const formData = init.body as FormData const curBuffer: Uint8Array =
for (const key of new Set([...formData.keys()])) { typeof value === 'string'
const values = formData.getAll(key) ? encodeText(value)
bodyChunks.push( : new Uint8Array(value)
`${key}=${(
await Promise.all( const prevBuffer = arrayBuffer
values.map(async (val) => { arrayBuffer = new Uint8Array(
if (typeof val === 'string') { prevBuffer.byteLength + curBuffer.byteLength
return val )
} else { arrayBuffer.set(prevBuffer)
return await val.text() arrayBuffer.set(curBuffer, prevBuffer.byteLength)
} } catch (err) {
}) console.error(err)
) }
).join(',')}`
)
} }
// handle blob body reader.read().then(processValue)
} else if (typeof (init.body as any).arrayBuffer === 'function') { }
const blob = init.body as Blob await reader.read().then(processValue)
const arrayBuffer = await blob.arrayBuffer() ;(init as any)._ogBody = arrayBuffer
bodyChunks.push(encode(await (init.body as Blob).arrayBuffer())) } // handle FormData or URLSearchParams bodies
init.body = new Blob([arrayBuffer], { type: blob.type }) else if (typeof (init.body as any).keys === 'function') {
} else if (typeof init.body === 'string') { const formData = init.body as FormData
bodyChunks.push(init.body) ;(init as any)._ogBody = init.body
for (const key of new Set([...formData.keys()])) {
const values = formData.getAll(key)
bodyChunks.push(
`${key}=${(
await Promise.all(
values.map(async (val) => {
if (typeof val === 'string') {
return val
} else {
return await val.text()
}
})
)
).join(',')}`
)
} }
// TODO: handle ReadableStream body? // handle blob body
} else if (typeof (init.body as any).arrayBuffer === 'function') {
const blob = init.body as Blob
const arrayBuffer = await blob.arrayBuffer()
bodyChunks.push(encode(await (init.body as Blob).arrayBuffer()))
;(init as any)._ogBody = new Blob([arrayBuffer], { type: blob.type })
} else if (typeof init.body === 'string') {
bodyChunks.push(init.body)
;(init as any)._ogBody = init.body
} }
} catch (err) {
console.error(err)
} }
const cacheString = JSON.stringify([ const cacheString = JSON.stringify([
this.fetchCacheKey || '', MAIN_KEY_PREFIX,
this.fetchCacheKeyPrefix || '',
url, url,
init.method, init.method,
init.headers, init.headers,
......
...@@ -103,10 +103,51 @@ export function patchFetch({ ...@@ -103,10 +103,51 @@ export function patchFetch({
typeof revalidate === 'number' && typeof revalidate === 'number' &&
revalidate > 0 revalidate > 0
) { ) {
cacheKey = await staticGenerationStore.incrementalCache.fetchCacheKey( try {
isRequestInput ? (input as Request).url : input.toString(), cacheKey = await staticGenerationStore.incrementalCache.fetchCacheKey(
isRequestInput ? (input as RequestInit) : init isRequestInput ? (input as Request).url : input.toString(),
) isRequestInput ? (input as RequestInit) : init
)
} catch (err) {
console.error(`Failed to generate cache key for`, input)
}
}
const requestInputFields = [
'cache',
'credentials',
'headers',
'integrity',
'keepalive',
'method',
'mode',
'redirect',
'referrer',
'referrerPolicy',
'signal',
'window',
'duplex',
]
if (isRequestInput) {
const reqInput: Request = input as any
const reqOptions: RequestInit = {
body: (reqInput as any)._ogBody || reqInput.body,
}
for (const field of requestInputFields) {
// @ts-expect-error custom fields
reqOptions[field] = reqInput[field]
}
input = new Request(reqInput.url, reqOptions)
} else if (init) {
const initialInit = init
init = {
body: (init as any)._ogBody || init.body,
}
for (const field of requestInputFields) {
// @ts-expect-error custom fields
init[field] = initialInit[field]
}
} }
const doOriginalFetch = async () => { const doOriginalFetch = async () => {
......
...@@ -99,8 +99,6 @@ createNextDescribe( ...@@ -99,8 +99,6 @@ createNextDescribe(
'variable-revalidate/encoding.rsc', 'variable-revalidate/encoding.rsc',
'variable-revalidate/encoding/page.js', 'variable-revalidate/encoding/page.js',
'variable-revalidate/no-store/page.js', 'variable-revalidate/no-store/page.js',
'variable-revalidate/post-method-cached.html',
'variable-revalidate/post-method-cached.rsc',
'variable-revalidate/post-method-cached/page.js', 'variable-revalidate/post-method-cached/page.js',
'variable-revalidate/post-method/page.js', 'variable-revalidate/post-method/page.js',
'variable-revalidate/revalidate-3.html', 'variable-revalidate/revalidate-3.html',
...@@ -222,11 +220,6 @@ createNextDescribe( ...@@ -222,11 +220,6 @@ createNextDescribe(
initialRevalidateSeconds: 3, initialRevalidateSeconds: 3,
srcRoute: '/variable-revalidate/encoding', srcRoute: '/variable-revalidate/encoding',
}, },
'/variable-revalidate/post-method-cached': {
dataRoute: '/variable-revalidate/post-method-cached.rsc',
initialRevalidateSeconds: 10,
srcRoute: '/variable-revalidate/post-method-cached',
},
'/variable-revalidate/revalidate-3': { '/variable-revalidate/revalidate-3': {
dataRoute: '/variable-revalidate/revalidate-3.rsc', dataRoute: '/variable-revalidate/revalidate-3.rsc',
initialRevalidateSeconds: 3, initialRevalidateSeconds: 3,
......
...@@ -79,6 +79,14 @@ export default async function Page() { ...@@ -79,6 +79,14 @@ export default async function Page() {
} }
).then((res) => res.text()) ).then((res) => res.text())
const dataWithBody5 = await fetchRetry(
'https://next-data-api-endpoint.vercel.app/api/random',
{
method: 'POST',
body: new URLSearchParams('myParam=myValue&myParam=anotherValue'),
}
).then((res) => res.text())
return ( return (
<> <>
<p id="page">/variable-revalidate/post-method-cached</p> <p id="page">/variable-revalidate/post-method-cached</p>
...@@ -87,6 +95,7 @@ export default async function Page() { ...@@ -87,6 +95,7 @@ export default async function Page() {
<p id="data-body2">{dataWithBody2}</p> <p id="data-body2">{dataWithBody2}</p>
<p id="data-body3">{dataWithBody3}</p> <p id="data-body3">{dataWithBody3}</p>
<p id="data-body4">{dataWithBody4}</p> <p id="data-body4">{dataWithBody4}</p>
<p id="data-body5">{dataWithBody5}</p>
</> </>
) )
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment