Unverified Commit 8f5ffed7 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update fetch cache handling with POST requests (#46856)

This updates to no longer skip caching POST or authed requests with the
fetch cache and instead we bail when `cookies()` or `headers()` is used
prior which is a better heuristic to signal user specific data would be
related to the fetch request.

x-ref: [slack
thread](https://vercel.slack.com/archives/C042LHPJ1NX/p1678130069138849)

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)
parent 0f621cb1
......@@ -1205,7 +1205,14 @@ export async function renderToHTMLOrFlight(
}
if (typeof layoutOrPageMod?.revalidate === 'number') {
defaultRevalidate = layoutOrPageMod.revalidate
defaultRevalidate = layoutOrPageMod.revalidate as number
if (
typeof staticGenerationStore.revalidate === 'undefined' ||
staticGenerationStore.revalidate > defaultRevalidate
) {
staticGenerationStore.revalidate = defaultRevalidate
}
if (
staticGenerationStore.isStaticGeneration &&
......
......@@ -149,7 +149,10 @@ export class IncrementalCache {
}
// 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 | Request = {}
): Promise<string> {
// this should be bumped anytime a fix is made to cache entries
// that should bust the cache
const MAIN_KEY_PREFIX = 'v1'
......@@ -240,7 +243,6 @@ export class IncrementalCache {
init.referrer,
init.referrerPolicy,
init.integrity,
init.next,
init.cache,
bodyChunks,
])
......
......@@ -48,7 +48,13 @@ export function patchFetch({
let revalidate: number | undefined | false = undefined
// RequestInit doesn't keep extra fields e.g. next so it's
// only available if init is used separate
let curRevalidate = init?.next?.revalidate
let curRevalidate =
typeof init?.next?.revalidate !== 'undefined'
? init?.next?.revalidate
: isRequestInput
? (input as any).next?.revalidate
: undefined
const _cache = getRequestMeta('cache')
if (_cache === 'force-cache') {
......@@ -78,11 +84,14 @@ export function patchFetch({
getRequestMeta('method')?.toLowerCase() || 'get'
)
const autoNoCache = hasUnCacheableHeader || isUnCacheableMethod
// if there are authorized headers or a POST method and
// dynamic data usage was present above the tree we bail
// e.g. if cookies() is used before an authed/POST fetch
const autoNoCache =
(hasUnCacheableHeader || isUnCacheableMethod) &&
staticGenerationStore.revalidate === 0
if (typeof revalidate === 'undefined') {
// if there are uncacheable headers and the cache value
// wasn't overridden then we must bail static generation
if (autoNoCache) {
revalidate = 0
} else {
......@@ -97,7 +106,7 @@ export function patchFetch({
if (
// we don't consider autoNoCache to switch to dynamic during
// revalidate although if it occurs during build we do
(!autoNoCache || staticGenerationStore.isPrerendering) &&
!autoNoCache &&
(typeof staticGenerationStore.revalidate === 'undefined' ||
(typeof revalidate === 'number' &&
revalidate < staticGenerationStore.revalidate))
......
......@@ -88,22 +88,22 @@ createNextDescribe(
'static-to-dynamic-error/[id]/page.js',
'variable-revalidate-edge/encoding/page.js',
'variable-revalidate-edge/no-store/page.js',
'variable-revalidate-edge/post-method-cached/page.js',
'variable-revalidate-edge/post-method-request/page.js',
'variable-revalidate-edge/post-method/page.js',
'variable-revalidate-edge/revalidate-3/page.js',
'variable-revalidate/authorization-cached.html',
'variable-revalidate/authorization-cached.rsc',
'variable-revalidate/authorization-cached/page.js',
'variable-revalidate/authorization.html',
'variable-revalidate/authorization.rsc',
'variable-revalidate/authorization/page.js',
'variable-revalidate/cookie-cached.html',
'variable-revalidate/cookie-cached.rsc',
'variable-revalidate/cookie-cached/page.js',
'variable-revalidate/cookie.html',
'variable-revalidate/cookie.rsc',
'variable-revalidate/cookie/page.js',
'variable-revalidate/encoding.html',
'variable-revalidate/encoding.rsc',
'variable-revalidate/encoding/page.js',
'variable-revalidate/no-store/page.js',
'variable-revalidate/post-method-cached/page.js',
'variable-revalidate/post-method-request/page.js',
'variable-revalidate/post-method.html',
'variable-revalidate/post-method.rsc',
'variable-revalidate/post-method/page.js',
'variable-revalidate/revalidate-3.html',
'variable-revalidate/revalidate-3.rsc',
......@@ -214,21 +214,26 @@ createNextDescribe(
initialRevalidateSeconds: false,
srcRoute: '/ssg-preview/[[...route]]',
},
'/variable-revalidate/authorization-cached': {
dataRoute: '/variable-revalidate/authorization-cached.rsc',
initialRevalidateSeconds: 3,
srcRoute: '/variable-revalidate/authorization-cached',
'/variable-revalidate/authorization': {
dataRoute: '/variable-revalidate/authorization.rsc',
initialRevalidateSeconds: 10,
srcRoute: '/variable-revalidate/authorization',
},
'/variable-revalidate/cookie-cached': {
dataRoute: '/variable-revalidate/cookie-cached.rsc',
'/variable-revalidate/cookie': {
dataRoute: '/variable-revalidate/cookie.rsc',
initialRevalidateSeconds: 3,
srcRoute: '/variable-revalidate/cookie-cached',
srcRoute: '/variable-revalidate/cookie',
},
'/variable-revalidate/encoding': {
dataRoute: '/variable-revalidate/encoding.rsc',
initialRevalidateSeconds: 3,
srcRoute: '/variable-revalidate/encoding',
},
'/variable-revalidate/post-method': {
dataRoute: '/variable-revalidate/post-method.rsc',
initialRevalidateSeconds: 10,
srcRoute: '/variable-revalidate/post-method',
},
'/variable-revalidate/revalidate-3': {
dataRoute: '/variable-revalidate/revalidate-3.rsc',
initialRevalidateSeconds: 3,
......@@ -441,35 +446,11 @@ createNextDescribe(
}, 'success')
})
it('should not cache correctly with authorization header', async () => {
const res = await fetchViaHTTP(
next.url,
'/variable-revalidate/authorization'
)
expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
const pageData = $('#page-data').text()
for (let i = 0; i < 3; i++) {
const res2 = await fetchViaHTTP(
next.url,
'/variable-revalidate/authorization'
)
expect(res2.status).toBe(200)
const html2 = await res2.text()
const $2 = cheerio.load(html2)
expect($2('#page-data').text()).not.toBe(pageData)
}
})
it('should cache correctly with authorization header and revalidate', async () => {
await check(async () => {
const res = await fetchViaHTTP(
next.url,
'/variable-revalidate/authorization-cached'
'/variable-revalidate/authorization'
)
expect(res.status).toBe(200)
const html = await res.text()
......@@ -480,7 +461,7 @@ createNextDescribe(
const res2 = await fetchViaHTTP(
next.url,
'/variable-revalidate/authorization-cached'
'/variable-revalidate/authorization'
)
expect(res2.status).toBe(200)
const html2 = await res2.text()
......@@ -492,30 +473,6 @@ createNextDescribe(
}, 'success')
})
it('should not cache correctly with POST method', async () => {
const res = await fetchViaHTTP(
next.url,
'/variable-revalidate/post-method'
)
expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
const pageData = $('#page-data').text()
for (let i = 0; i < 3; i++) {
const res2 = await fetchViaHTTP(
next.url,
'/variable-revalidate/post-method'
)
expect(res2.status).toBe(200)
const html2 = await res2.text()
const $2 = cheerio.load(html2)
expect($2('#page-data').text()).not.toBe(pageData)
}
})
it('should not cache correctly with POST method request init', async () => {
const res = await fetchViaHTTP(
next.url,
......@@ -544,7 +501,7 @@ createNextDescribe(
await check(async () => {
const res = await fetchViaHTTP(
next.url,
'/variable-revalidate/post-method-cached'
'/variable-revalidate/post-method'
)
expect(res.status).toBe(200)
const html = await res.text()
......@@ -563,7 +520,7 @@ createNextDescribe(
const res2 = await fetchViaHTTP(
next.url,
'/variable-revalidate/post-method-cached'
'/variable-revalidate/post-method'
)
expect(res2.status).toBe(200)
const html2 = await res2.text()
......@@ -582,7 +539,7 @@ createNextDescribe(
await check(async () => {
const res = await fetchViaHTTP(
next.url,
'/variable-revalidate-edge/post-method-cached'
'/variable-revalidate-edge/post-method'
)
expect(res.status).toBe(200)
const html = await res.text()
......@@ -597,7 +554,7 @@ createNextDescribe(
const res2 = await fetchViaHTTP(
next.url,
'/variable-revalidate-edge/post-method-cached'
'/variable-revalidate-edge/post-method'
)
expect(res2.status).toBe(200)
const html2 = await res2.text()
......@@ -617,7 +574,7 @@ createNextDescribe(
await check(async () => {
const res = await fetchViaHTTP(
next.url,
'/variable-revalidate/post-method-cached'
'/variable-revalidate/post-method'
)
expect(res.status).toBe(200)
const html = await res.text()
......@@ -628,7 +585,7 @@ createNextDescribe(
const res2 = await fetchViaHTTP(
next.url,
'/variable-revalidate/post-method-cached'
'/variable-revalidate/post-method'
)
expect(res2.status).toBe(200)
const html2 = await res2.text()
......@@ -640,30 +597,9 @@ createNextDescribe(
}, 'success')
})
it('should not cache correctly with cookie header', async () => {
const res = await fetchViaHTTP(next.url, '/variable-revalidate/cookie')
expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
const pageData = $('#page-data').text()
for (let i = 0; i < 3; i++) {
const res2 = await fetchViaHTTP(next.url, '/variable-revalidate/cookie')
expect(res2.status).toBe(200)
const html2 = await res2.text()
const $2 = cheerio.load(html2)
expect($2('#page-data').text()).not.toBe(pageData)
}
})
it('should cache correctly with cookie header and revalidate', async () => {
await check(async () => {
const res = await fetchViaHTTP(
next.url,
'/variable-revalidate/cookie-cached'
)
const res = await fetchViaHTTP(next.url, '/variable-revalidate/cookie')
expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
......@@ -671,10 +607,7 @@ createNextDescribe(
const layoutData = $('#layout-data').text()
const pageData = $('#page-data').text()
const res2 = await fetchViaHTTP(
next.url,
'/variable-revalidate/cookie-cached'
)
const res2 = await fetchViaHTTP(next.url, '/variable-revalidate/cookie')
expect(res2.status).toBe(200)
const html2 = await res2.text()
const $2 = cheerio.load(html2)
......
......@@ -22,6 +22,9 @@ export default async function page({ params }) {
{
method: 'POST',
body: JSON.stringify({ hello: 'world' }),
next: {
revalidate: 0,
},
}
).then((res) => res.text())
......
......@@ -4,6 +4,9 @@ export default async function Page() {
const data = await fetch(
new Request('https://next-data-api-endpoint.vercel.app/api/random', {
method: 'POST',
next: {
revalidate: 0,
},
})
).then((res) => res.text())
......@@ -11,12 +14,15 @@ export default async function Page() {
new URL('https://next-data-api-endpoint.vercel.app/api/random'),
{
method: 'POST',
next: {
revalidate: 0,
},
}
).then((res) => res.text())
return (
<>
<p id="page">/variable-revalidate/post-method</p>
<p id="page">/variable-revalidate-edge/post-method</p>
<p id="page-data">{data}</p>
<p id="page-data2">{data2}</p>
</>
......
export default async function Page() {
const data = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random',
{
headers: {
// revalidate 3 is overridden by cookie header here
cookie: 'authorized=true',
},
next: {
revalidate: 3,
},
}
).then((res) => res.text())
return (
<>
<p id="page">/variable-revalidate/cookie-cached</p>
<p id="page-data">{data}</p>
</>
)
}
......@@ -5,7 +5,6 @@ export default async function Page() {
'https://next-data-api-endpoint.vercel.app/api/random',
{
headers: {
// revalidate 3 is overridden by cookie header here
cookie: 'authorized=true',
},
}
......
const fetchRetry = async (url, init) => {
for (let i = 0; i < 5; i++) {
try {
return await fetch(url, init)
} catch (err) {
if (i === 4) {
throw err
}
console.log(`Failed to fetch`, err, `retrying...`)
}
}
}
export default async function Page() {
const data = await fetchRetry(
'https://next-data-api-endpoint.vercel.app/api/random',
{
method: 'POST',
next: {
revalidate: 10,
},
}
).then((res) => res.text())
const dataWithBody1 = await fetchRetry(
'https://next-data-api-endpoint.vercel.app/api/random',
{
method: 'POST',
body: JSON.stringify({ hello: 'world' }),
next: {
revalidate: 10,
},
}
).then((res) => res.text())
const dataWithBody2 = await fetchRetry(
'https://next-data-api-endpoint.vercel.app/api/random',
{
method: 'POST',
body: new ReadableStream({
start(controller) {
controller.enqueue(JSON.stringify({ another: 'one' }))
controller.close()
},
}),
duplex: 'half',
next: {
revalidate: 10,
},
}
).then((res) => res.text())
const dataWithBody3 = await fetchRetry(
'https://next-data-api-endpoint.vercel.app/api/random',
{
method: 'POST',
body: new URLSearchParams('myParam=myValue&myParam=anotherValue'),
next: {
revalidate: 30,
},
}
).then((res) => res.text())
const dataWithBody4 = 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 (
<>
<p id="page">/variable-revalidate/post-method-cached</p>
<p id="page-data">{data}</p>
<p id="data-body1">{dataWithBody1}</p>
<p id="data-body2">{dataWithBody2}</p>
<p id="data-body3">{dataWithBody3}</p>
<p id="data-body4">{dataWithBody4}</p>
</>
)
}
export default async function Page() {
const data = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random',
{
headers: {
Authorization: 'Bearer token',
new Request('https://next-data-api-endpoint.vercel.app/api/random', {
method: 'POST',
next: {
revalidate: 0,
},
})
).then((res) => res.text())
const data2 = await fetch(
new URL('https://next-data-api-endpoint.vercel.app/api/random'),
{
method: 'POST',
next: {
revalidate: 3,
revalidate: 0,
},
}
).then((res) => res.text())
return (
<>
<p id="page">/variable-revalidate/authorization-cached</p>
<p id="page">/variable-revalidate/post-method</p>
<p id="page-data">{data}</p>
<p id="page-data2">{data2}</p>
</>
)
}
const fetchRetry = async (url, init) => {
for (let i = 0; i < 5; i++) {
try {
return await fetch(url, init)
} catch (err) {
if (i === 4) {
throw err
}
console.log(`Failed to fetch`, err, `retrying...`)
}
}
}
export default async function Page() {
const data = await fetch(
const data = await fetchRetry(
'https://next-data-api-endpoint.vercel.app/api/random',
{
method: 'POST',
next: {
revalidate: 10,
},
}
).then((res) => res.text())
const dataWithBody1 = await fetchRetry(
'https://next-data-api-endpoint.vercel.app/api/random',
{
method: 'POST',
body: JSON.stringify({ hello: 'world' }),
next: {
revalidate: 10,
},
}
).then((res) => res.text())
const dataWithBody2 = await fetchRetry(
'https://next-data-api-endpoint.vercel.app/api/random',
{
method: 'POST',
body: new ReadableStream({
start(controller) {
controller.enqueue(JSON.stringify({ another: 'one' }))
controller.close()
},
}),
duplex: 'half',
next: {
revalidate: 10,
},
}
).then((res) => res.text())
const dataWithBody3 = await fetchRetry(
'https://next-data-api-endpoint.vercel.app/api/random',
{
method: 'POST',
body: new URLSearchParams('myParam=myValue&myParam=anotherValue'),
next: {
revalidate: 30,
},
}
).then((res) => res.text())
const dataWithBody4 = await fetchRetry(
'https://next-data-api-endpoint.vercel.app/api/random',
{
method: 'POST',
body: new URLSearchParams('myParam=myValue&myParam=diffValue'),
}
).then((res) => res.text())
return (
<>
<p id="page">/variable-revalidate/post-method</p>
<p id="page">/variable-revalidate/post-method-cached</p>
<p id="page-data">{data}</p>
<p id="data-body1">{dataWithBody1}</p>
<p id="data-body2">{dataWithBody2}</p>
<p id="data-body3">{dataWithBody3}</p>
<p id="data-body4">{dataWithBody4}</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