Unverified Commit ba3ef55f authored by mugi-uno's avatar mugi-uno Committed by GitHub

fix: fetch() behavior when "dynamic" is "force-dynamic (#58484)

### What?

I changed the behavior of fetch() when 'force-dynamic' is specified in the `dynamic` of Route Segment Config to be similar to when 'force-no-store' is specified in `fetchCache`.

### Why?

The document (https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamic) contains an explanation that when 'force-dynamic' is specified for `dynamic`, it will behave equivalently to the following:

> Setting the segment config to export const fetchCache = 'force-no-store'

I tried to correct it because it was not actually behaving this way.

### How?

When determining if `fetchCache` is 'force-no-store', I have modified the code to also check the `dynamic` setting.

Fixes #47033
Co-authored-by: 's avatarZack Tanner <1939140+ztanner@users.noreply.github.com>
parent df4c2aa8
...@@ -257,7 +257,8 @@ export function patchFetch({ ...@@ -257,7 +257,8 @@ export function patchFetch({
const isOnlyNoStore = const isOnlyNoStore =
staticGenerationStore.fetchCache === 'only-no-store' staticGenerationStore.fetchCache === 'only-no-store'
const isForceNoStore = const isForceNoStore =
staticGenerationStore.fetchCache === 'force-no-store' staticGenerationStore.fetchCache === 'force-no-store' ||
staticGenerationStore.forceDynamic
let _cache = getRequestMeta('cache') let _cache = getRequestMeta('cache')
let cacheReason = '' let cacheReason = ''
......
...@@ -598,6 +598,7 @@ createNextDescribe( ...@@ -598,6 +598,7 @@ createNextDescribe(
'variable-revalidate/post-method/page.js', 'variable-revalidate/post-method/page.js',
'variable-revalidate/status-code/page.js', 'variable-revalidate/status-code/page.js',
'dynamic-no-gen-params-ssr/[slug]/page.js', 'dynamic-no-gen-params-ssr/[slug]/page.js',
'force-dynamic-no-with-revalidate/page.js',
'hooks/use-search-params/force-static.rsc', 'hooks/use-search-params/force-static.rsc',
'partial-gen-params/[lang]/[slug]/page.js', 'partial-gen-params/[lang]/[slug]/page.js',
'variable-revalidate/headers-instance.rsc', 'variable-revalidate/headers-instance.rsc',
...@@ -631,6 +632,7 @@ createNextDescribe( ...@@ -631,6 +632,7 @@ createNextDescribe(
'variable-revalidate/headers-instance/page.js', 'variable-revalidate/headers-instance/page.js',
'variable-revalidate/status-code.prefetch.rsc', 'variable-revalidate/status-code.prefetch.rsc',
'force-cache/page_client-reference-manifest.js', 'force-cache/page_client-reference-manifest.js',
'force-dynamic-no-with-revalidate.prefetch.rsc',
'hooks/use-search-params/with-suspense/page.js', 'hooks/use-search-params/with-suspense/page.js',
'variable-revalidate-edge/revalidate-3/page.js', 'variable-revalidate-edge/revalidate-3/page.js',
'(new)/custom/page_client-reference-manifest.js', '(new)/custom/page_client-reference-manifest.js',
...@@ -702,6 +704,7 @@ createNextDescribe( ...@@ -702,6 +704,7 @@ createNextDescribe(
'variable-revalidate/post-method/page_client-reference-manifest.js', 'variable-revalidate/post-method/page_client-reference-manifest.js',
'variable-revalidate/status-code/page_client-reference-manifest.js', 'variable-revalidate/status-code/page_client-reference-manifest.js',
'dynamic-no-gen-params-ssr/[slug]/page_client-reference-manifest.js', 'dynamic-no-gen-params-ssr/[slug]/page_client-reference-manifest.js',
'force-dynamic-no-with-revalidate/page_client-reference-manifest.js',
'partial-gen-params/[lang]/[slug]/page_client-reference-manifest.js', 'partial-gen-params/[lang]/[slug]/page_client-reference-manifest.js',
'variable-revalidate/revalidate-3/page_client-reference-manifest.js', 'variable-revalidate/revalidate-3/page_client-reference-manifest.js',
'stale-cache-serving-edge/app-page/page_client-reference-manifest.js', 'stale-cache-serving-edge/app-page/page_client-reference-manifest.js',
...@@ -2529,6 +2532,24 @@ createNextDescribe( ...@@ -2529,6 +2532,24 @@ createNextDescribe(
} }
}) })
it('should force no store with force-dynamic', async () => {
const res = await next.fetch('/force-dynamic-no-with-revalidate')
const html = await res.text()
expect(res.status).toBe(200)
const initData = cheerio.load(html)('#data').text()
await check(async () => {
const res2 = await next.fetch('/force-dynamic-no-with-revalidate')
expect(res2.status).toBe(200)
const $ = cheerio.load(await res2.text())
expect($('#data').text()).toBeTruthy()
expect($('#data').text()).not.toBe(initData)
return 'success'
}, 'success')
})
it('should not error with generateStaticParams and dynamic data', async () => { it('should not error with generateStaticParams and dynamic data', async () => {
const res = await next.fetch('/gen-params-dynamic/one') const res = await next.fetch('/gen-params-dynamic/one')
const html = await res.text() const html = await res.text()
......
import React from 'react'
export const dynamic = 'force-dynamic'
export default async function Page() {
const data = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random',
{
next: {
revalidate: 100,
},
}
).then((res) => res.text())
return (
<>
<p id="page">/force-dynamic-no-with-revalidate</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