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,17 +150,20 @@ export class IncrementalCache { ...@@ -148,17 +150,20 @@ 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 [origBody, newBody] = readableBody.tee() const reader = readableBody.getReader()
init.body = origBody let arrayBuffer = new Uint8Array()
const reader = newBody.getReader()
function processValue({ function processValue({
done, done,
value, value,
...@@ -171,19 +176,30 @@ export class IncrementalCache { ...@@ -171,19 +176,30 @@ export class IncrementalCache {
} }
if (value) { if (value) {
try { try {
bodyChunks.push( bodyChunks.push(typeof value === 'string' ? value : encode(value))
typeof value === 'string' ? value : encode(value) const curBuffer: Uint8Array =
typeof value === 'string'
? encodeText(value)
: new Uint8Array(value)
const prevBuffer = arrayBuffer
arrayBuffer = new Uint8Array(
prevBuffer.byteLength + curBuffer.byteLength
) )
arrayBuffer.set(prevBuffer)
arrayBuffer.set(curBuffer, prevBuffer.byteLength)
} catch (err) { } catch (err) {
console.error(err) console.error(err)
} }
} }
return reader.read().then(processValue) reader.read().then(processValue)
} }
await reader.read().then(processValue) await reader.read().then(processValue)
;(init as any)._ogBody = arrayBuffer
} // handle FormData or URLSearchParams bodies } // handle FormData or URLSearchParams bodies
else if (typeof (init.body as any).keys === 'function') { else if (typeof (init.body as any).keys === 'function') {
const formData = init.body as FormData const formData = init.body as FormData
;(init as any)._ogBody = init.body
for (const key of new Set([...formData.keys()])) { for (const key of new Set([...formData.keys()])) {
const values = formData.getAll(key) const values = formData.getAll(key)
bodyChunks.push( bodyChunks.push(
...@@ -205,18 +221,16 @@ export class IncrementalCache { ...@@ -205,18 +221,16 @@ export class IncrementalCache {
const blob = init.body as Blob const blob = init.body as Blob
const arrayBuffer = await blob.arrayBuffer() const arrayBuffer = await blob.arrayBuffer()
bodyChunks.push(encode(await (init.body as Blob).arrayBuffer())) bodyChunks.push(encode(await (init.body as Blob).arrayBuffer()))
init.body = new Blob([arrayBuffer], { type: blob.type }) ;(init as any)._ogBody = new Blob([arrayBuffer], { type: blob.type })
} else if (typeof init.body === 'string') { } else if (typeof init.body === 'string') {
bodyChunks.push(init.body) bodyChunks.push(init.body)
;(init as any)._ogBody = init.body
} }
// TODO: handle ReadableStream 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
) { ) {
try {
cacheKey = await staticGenerationStore.incrementalCache.fetchCacheKey( cacheKey = await staticGenerationStore.incrementalCache.fetchCacheKey(
isRequestInput ? (input as Request).url : input.toString(), isRequestInput ? (input as Request).url : input.toString(),
isRequestInput ? (input as RequestInit) : init 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