Unverified Commit 86bf30a4 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix POST fetch request cache heuristic in POST route handler (#47333)

This ensures we properly bail from caching POST requests when a route is
dynamic e.g. a POST route handler
parent 93152db9
...@@ -14,8 +14,11 @@ export function staticGenerationBailout(reason: string): boolean | never { ...@@ -14,8 +14,11 @@ export function staticGenerationBailout(reason: string): boolean | never {
) )
} }
if (staticGenerationStore?.isStaticGeneration) { if (staticGenerationStore) {
staticGenerationStore.revalidate = 0 staticGenerationStore.revalidate = 0
}
if (staticGenerationStore?.isStaticGeneration) {
const err = new DynamicServerError(reason) const err = new DynamicServerError(reason)
staticGenerationStore.dynamicUsageDescription = reason staticGenerationStore.dynamicUsageDescription = reason
......
...@@ -50,6 +50,26 @@ createNextDescribe( ...@@ -50,6 +50,26 @@ createNextDescribe(
}) })
} }
it('should correctly skip caching POST fetch for POST handler', async () => {
const res = await next.fetch('/route-handler/post', {
method: 'POST',
})
expect(res.status).toBe(200)
const data = await res.json()
expect(data).toBeTruthy()
for (let i = 0; i < 5; i++) {
const res2 = await next.fetch('/route-handler/post', {
method: 'POST',
})
expect(res2.status).toBe(200)
const newData = await res2.json()
expect(newData).toBeTruthy()
expect(newData).not.toEqual(data)
}
})
if (!process.env.CUSTOM_CACHE_HANDLER) { if (!process.env.CUSTOM_CACHE_HANDLER) {
it('should revalidate correctly with config and fetch revalidate', async () => { it('should revalidate correctly with config and fetch revalidate', async () => {
const initial$ = await next.render$( const initial$ = await next.render$(
...@@ -192,6 +212,7 @@ createNextDescribe( ...@@ -192,6 +212,7 @@ createNextDescribe(
'partial-gen-params-no-additional-slug/fr/second.html', 'partial-gen-params-no-additional-slug/fr/second.html',
'partial-gen-params-no-additional-slug/fr/second.rsc', 'partial-gen-params-no-additional-slug/fr/second.rsc',
'partial-gen-params/[lang]/[slug]/page.js', 'partial-gen-params/[lang]/[slug]/page.js',
'route-handler/post/route.js',
'ssg-preview.html', 'ssg-preview.html',
'ssg-preview.rsc', 'ssg-preview.rsc',
'ssg-preview/[[...route]]/page.js', 'ssg-preview/[[...route]]/page.js',
......
import { NextResponse } from 'next/server'
export async function POST() {
const data = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random',
{
method: 'POST',
}
).then((res) => res.text())
return NextResponse.json({ data })
}
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