Unverified Commit f602b297 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

default fetchCache to no-store when force-dynamic is set (#64145)

`fetchCache` is a more fine-grained segment level cache-control
configuration that most people shouldn't have to use. Current semantics
of `dynamic = "force-dynamic"` will still treat the fetch as cacheable
unless explicitly overriding it in the fetch, or setting a segment level
`fetchCache`.

The `dynamic` cache configuration should be seen as a "top-level"
configuration, while more fine-grained controls should inherit logical
defaults from the top-level. Otherwise this forces people to opt-into
the `fetchCache` configuration, or manually override each `fetch` call,
which isn't what you'd expect when forcing a segment to be dynamic.

This will default to not attempting to cache the fetch when
`force-dynamic` is used. As a result, I had to update one of the
`app-static` tests to use `revalidate: 0` rather than `force-dynamic`,
as the revalidate behavior is slightly different in that it won't modify
the revalidation time on a fetch if it's non-zero.

Closes NEXT-2067
parent 14ed612d
...@@ -62,7 +62,11 @@ export const dynamic = 'auto' ...@@ -62,7 +62,11 @@ export const dynamic = 'auto'
> **Good to know**: The new model in the `app` directory favors granular caching control at the `fetch` request level over the binary all-or-nothing model of `getServerSideProps` and `getStaticProps` at the page-level in the `pages` directory. The `dynamic` option is a way to opt back in to the previous model as a convenience and provides a simpler migration path. > **Good to know**: The new model in the `app` directory favors granular caching control at the `fetch` request level over the binary all-or-nothing model of `getServerSideProps` and `getStaticProps` at the page-level in the `pages` directory. The `dynamic` option is a way to opt back in to the previous model as a convenience and provides a simpler migration path.
- **`'auto'`** (default): The default option to cache as much as possible without preventing any components from opting into dynamic behavior. - **`'auto'`** (default): The default option to cache as much as possible without preventing any components from opting into dynamic behavior.
- **`'force-dynamic'`**: Force [dynamic rendering](/docs/app/building-your-application/rendering/server-components#dynamic-rendering), which will result in routes being rendered for each user at request time. This option is equivalent to `getServerSideProps()` in the `pages` directory. - **`'force-dynamic'`**: Force [dynamic rendering](/docs/app/building-your-application/rendering/server-components#dynamic-rendering), which will result in routes being rendered for each user at request time. This option is equivalent to:
- `getServerSideProps()` in the `pages` directory.
- Setting the option of every `fetch()` request in a layout or page to `{ cache: 'no-store', next: { revalidate: 0 } }`.
- Setting the segment config to `export const fetchCache = 'force-no-store'`
- **`'error'`**: Force static rendering and cache the data of a layout or page by causing an error if any components use [dynamic functions](/docs/app/building-your-application/rendering/server-components#dynamic-functions) or uncached data. This option is equivalent to: - **`'error'`**: Force static rendering and cache the data of a layout or page by causing an error if any components use [dynamic functions](/docs/app/building-your-application/rendering/server-components#dynamic-functions) or uncached data. This option is equivalent to:
- `getStaticProps()` in the `pages` directory. - `getStaticProps()` in the `pages` directory.
......
...@@ -310,7 +310,13 @@ function createPatchedFetcher( ...@@ -310,7 +310,13 @@ function createPatchedFetcher(
_cache === 'no-cache' || _cache === 'no-cache' ||
_cache === 'no-store' || _cache === 'no-store' ||
fetchCacheMode === 'force-no-store' || fetchCacheMode === 'force-no-store' ||
fetchCacheMode === 'only-no-store' fetchCacheMode === 'only-no-store' ||
// If no explicit fetch cache mode is set, but dynamic = `force-dynamic` is set,
// we shouldn't consider caching the fetch. This is because the `dynamic` cache
// is considered a "top-level" cache mode, whereas something like `fetchCache` is more
// fine-grained. Top-level modes are responsible for setting reasonable defaults for the
// other configurations.
(!fetchCacheMode && staticGenerationStore.forceDynamic)
) { ) {
curRevalidate = 0 curRevalidate = 0
} }
......
...@@ -104,6 +104,34 @@ createNextDescribe( ...@@ -104,6 +104,34 @@ createNextDescribe(
) )
}) })
it('should infer a fetchCache of force-no-store when force-dynamic is used', async () => {
const $ = await next.render$(
'/force-dynamic-fetch-cache/no-fetch-cache'
)
const initData = $('#data').text()
await retry(async () => {
const $2 = await next.render$(
'/force-dynamic-fetch-cache/no-fetch-cache'
)
expect($2('#data').text()).toBeTruthy()
expect($2('#data').text()).not.toBe(initData)
})
})
it('fetchCache config should supercede dynamic config when force-dynamic is used', async () => {
const $ = await next.render$(
'/force-dynamic-fetch-cache/with-fetch-cache'
)
const initData = $('#data').text()
await retry(async () => {
const $2 = await next.render$(
'/force-dynamic-fetch-cache/with-fetch-cache'
)
expect($2('#data').text()).toBeTruthy()
expect($2('#data').text()).toBe(initData)
})
})
if (!process.env.CUSTOM_CACHE_HANDLER) { if (!process.env.CUSTOM_CACHE_HANDLER) {
it('should honor force-static with fetch cache: no-store correctly', async () => { it('should honor force-static with fetch cache: no-store correctly', async () => {
const res = await next.fetch('/force-static-fetch-no-store') const res = await next.fetch('/force-static-fetch-no-store')
...@@ -667,6 +695,10 @@ createNextDescribe( ...@@ -667,6 +695,10 @@ createNextDescribe(
"force-cache/page_client-reference-manifest.js", "force-cache/page_client-reference-manifest.js",
"force-dynamic-catch-all/[slug]/[[...id]]/page.js", "force-dynamic-catch-all/[slug]/[[...id]]/page.js",
"force-dynamic-catch-all/[slug]/[[...id]]/page_client-reference-manifest.js", "force-dynamic-catch-all/[slug]/[[...id]]/page_client-reference-manifest.js",
"force-dynamic-fetch-cache/no-fetch-cache/page.js",
"force-dynamic-fetch-cache/no-fetch-cache/page_client-reference-manifest.js",
"force-dynamic-fetch-cache/with-fetch-cache/page.js",
"force-dynamic-fetch-cache/with-fetch-cache/page_client-reference-manifest.js",
"force-dynamic-no-prerender/[id]/page.js", "force-dynamic-no-prerender/[id]/page.js",
"force-dynamic-no-prerender/[id]/page_client-reference-manifest.js", "force-dynamic-no-prerender/[id]/page_client-reference-manifest.js",
"force-dynamic-prerender/[slug]/page.js", "force-dynamic-prerender/[slug]/page.js",
......
export const dynamic = 'force-dynamic'
export default async function Page() {
const data = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random'
).then((res) => res.text())
return (
<>
<p id="page">/force-dynamic-fetch-cache/no-fetch-cache</p>
<p id="data">{data}</p>
</>
)
}
export const dynamic = 'force-dynamic'
export const fetchCache = 'force-cache'
export default async function Page() {
const data = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random'
).then((res) => res.text())
return (
<>
<p id="page">/force-dynamic-fetch-cache/with-fetch-cache</p>
<p id="data">{data}</p>
</>
)
}
export const dynamic = 'force-dynamic' export const revalidate = 0
const delay = 3000 const delay = 3000
......
import { NextRequest, NextResponse } from 'next/server' import { NextRequest, NextResponse } from 'next/server'
export const dynamic = 'force-dynamic' export const revalidate = 0
const delay = 3000 const delay = 3000
......
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