Unverified Commit 4d14e831 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

[Breaking] Disable automatic fetch caching (#66004)

## Background

Previously we introduced automatic caching for `fetch` based on certain
heuristics that were a bit tricky to grasp all scenarios. The scenarios
we would automatically cache were no dynamic data access before the
fetch call e.g. `headers()` or `cookies()`, the fetch call is inside of
a dynamic page e.g. `POST` method or `export const revalidate = 0` page
and the fetch is a non-`GET` request or has `Authorization` or `Cookie`
headers, or the fetch had `cache: 'no-store' | 'no-cache'` or
`revalidate: 0`.

## New Behavior

By default fetches will no longer automatically be cached. Instead they
need to be opted-in to caching via `export const fetchCache =
'default-cache' | 'force-cache',` `next: { revalidate: false or value >
0 }` or `cache: 'force-cache' | 'default-cache'`.

When the fetch call is automatically skipping the cache it won't impact
the page level ISR cacheability although if a fetch call manually
specifies `cache: 'no-store'` or `revalidate: 0` it will still bail from
the page being statically generated as it was before.

To achieve the previous behavior of automatic fetch caching all that
needs to be added is `export const fetchCache = 'default-cache'` in the
root layout(s) of your project.
parent 6c1c0049
...@@ -1438,6 +1438,21 @@ export async function buildAppStaticPaths({ ...@@ -1438,6 +1438,21 @@ export async function buildAppStaticPaths({
const newParams: Params[] = [] const newParams: Params[] = []
if (curGenerate.generateStaticParams) { if (curGenerate.generateStaticParams) {
const curStore =
ComponentMod.staticGenerationAsyncStorage.getStore()
if (curStore) {
if (typeof curGenerate?.config?.fetchCache !== 'undefined') {
curStore.fetchCache = curGenerate.config.fetchCache
}
if (typeof curGenerate?.config?.revalidate !== 'undefined') {
curStore.revalidate = curGenerate.config.revalidate
}
if (curGenerate?.config?.dynamic === 'force-dynamic') {
curStore.forceDynamic = true
}
}
for (const params of paramsItems) { for (const params of paramsItems) {
const result = await curGenerate.generateStaticParams({ const result = await curGenerate.generateStaticParams({
params, params,
......
This diff is collapsed.
export const fetchCache = 'default-cache'
export const metadata = { export const metadata = {
title: 'Next.js', title: 'Next.js',
description: 'Generated by Next.js', description: 'Generated by Next.js',
......
export const fetchCache = 'default-cache'
export default function Layout({ children }) { export default function Layout({ children }) {
return ( return (
<html lang="en"> <html lang="en">
......
...@@ -39,6 +39,33 @@ describe('app-dir static/dynamic handling', () => { ...@@ -39,6 +39,33 @@ describe('app-dir static/dynamic handling', () => {
} }
}) })
it('should use auto no cache when no fetch config', async () => {
const res = await next.fetch('/no-config-fetch')
expect(res.status).toBe(200)
const html = await res.text()
const $ = cheerio.load(html)
const data = $('#data').text()
expect(data).toBeTruthy()
const res2 = await next.fetch('/no-config-fetch')
const html2 = await res2.text()
const data2 = cheerio.load(html2)('#data').text()
if (isNextDev) {
expect(data).not.toBe(data2)
} else {
const pageCache = (
res.headers.get('x-vercel-cache') || res.headers.get('x-nextjs-cache')
).toLowerCase()
expect(pageCache).toBeTruthy()
expect(pageCache).not.toBe('MISS')
expect(data).toBe(data2)
}
})
it('should still cache even though the `traceparent` header was different', async () => { it('should still cache even though the `traceparent` header was different', async () => {
const res = await next.fetch('/strip-header-traceparent') const res = await next.fetch('/strip-header-traceparent')
expect(res.status).toBe(200) expect(res.status).toBe(200)
...@@ -642,6 +669,8 @@ describe('app-dir static/dynamic handling', () => { ...@@ -642,6 +669,8 @@ describe('app-dir static/dynamic handling', () => {
[ [
"(new)/custom/page.js", "(new)/custom/page.js",
"(new)/custom/page_client-reference-manifest.js", "(new)/custom/page_client-reference-manifest.js",
"(new)/no-config-fetch/page.js",
"(new)/no-config-fetch/page_client-reference-manifest.js",
"_not-found.html", "_not-found.html",
"_not-found.rsc", "_not-found.rsc",
"_not-found/page.js", "_not-found/page.js",
...@@ -744,6 +773,8 @@ describe('app-dir static/dynamic handling', () => { ...@@ -744,6 +773,8 @@ describe('app-dir static/dynamic handling', () => {
"isr-error-handling.rsc", "isr-error-handling.rsc",
"isr-error-handling/page.js", "isr-error-handling/page.js",
"isr-error-handling/page_client-reference-manifest.js", "isr-error-handling/page_client-reference-manifest.js",
"no-config-fetch.html",
"no-config-fetch.rsc",
"no-store/dynamic/page.js", "no-store/dynamic/page.js",
"no-store/dynamic/page_client-reference-manifest.js", "no-store/dynamic/page_client-reference-manifest.js",
"no-store/static.html", "no-store/static.html",
...@@ -1233,6 +1264,22 @@ describe('app-dir static/dynamic handling', () => { ...@@ -1233,6 +1264,22 @@ describe('app-dir static/dynamic handling', () => {
"initialRevalidateSeconds": 3, "initialRevalidateSeconds": 3,
"srcRoute": "/isr-error-handling", "srcRoute": "/isr-error-handling",
}, },
"/no-config-fetch": {
"dataRoute": "/no-config-fetch.rsc",
"experimentalBypassFor": [
{
"key": "Next-Action",
"type": "header",
},
{
"key": "content-type",
"type": "header",
"value": "multipart/form-data;.*",
},
],
"initialRevalidateSeconds": false,
"srcRoute": "/no-config-fetch",
},
"/no-store/static": { "/no-store/static": {
"dataRoute": "/no-store/static.rsc", "dataRoute": "/no-store/static.rsc",
"experimentalBypassFor": [ "experimentalBypassFor": [
...@@ -2465,8 +2512,12 @@ describe('app-dir static/dynamic handling', () => { ...@@ -2465,8 +2512,12 @@ describe('app-dir static/dynamic handling', () => {
const html2 = await res2.text() const html2 = await res2.text()
const $2 = cheerio.load(html2) const $2 = cheerio.load(html2)
// this relies on ISR level cache which isn't
// applied in dev
if (!isNextDev) {
expect($2('#layout-data').text()).toBe(layoutData) expect($2('#layout-data').text()).toBe(layoutData)
expect($2('#page-data').text()).toBe(pageData) expect($2('#page-data').text()).toBe(pageData)
}
return 'success' return 'success'
}, 'success') }, 'success')
}) })
...@@ -2610,8 +2661,12 @@ describe('app-dir static/dynamic handling', () => { ...@@ -2610,8 +2661,12 @@ describe('app-dir static/dynamic handling', () => {
const html2 = await res2.text() const html2 = await res2.text()
const $2 = cheerio.load(html2) const $2 = cheerio.load(html2)
// this relies on ISR level cache which isn't
// applied in dev
if (!isNextDev) {
expect($2('#layout-data').text()).toBe(layoutData) expect($2('#layout-data').text()).toBe(layoutData)
expect($2('#page-data').text()).toBe(pageData) expect($2('#page-data').text()).toBe(pageData)
}
return 'success' return 'success'
}, 'success') }, 'success')
}) })
......
export default async function Page() {
const data = await fetch(
`https://next-data-api-endpoint.vercel.app/api/random`
).then((res) => res.text())
return (
<>
<p>/no-config-fetch</p>
<p id="data">{data}</p>
</>
)
}
export const dynamicParams = true export const dynamicParams = true
export const fetchCache = 'default-cache'
export async function generateStaticParams() { export async function generateStaticParams() {
const res = await fetch( const res = await fetch(
......
export const dynamicParams = false export const dynamicParams = false
export const fetchCache = 'default-cache'
export async function generateStaticParams() { export async function generateStaticParams() {
const res = await fetch( const res = await fetch(
......
import Link from 'next/link' import Link from 'next/link'
export const fetchCache = 'default-cache'
export default function Layout({ children }) { export default function Layout({ children }) {
return ( return (
<html> <html>
......
export const fetchCache = 'default-cache'
export default function Layout({ children }) { export default function Layout({ children }) {
return ( return (
<html lang="en"> <html lang="en">
......
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