Unverified Commit 891ca7a0 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

ensure accessing search params mark dynamic usage (#67105)

When accessing search params, we only track dynamic access during static
generation. This has implications on fetch caching, because the fetch
cache relies on hints set during render that it's in a dynamic scope. In
15, this would signal that it should not attempt to cache the fetch at
all. In 14, this could impact the heuristic that bails from fetch cache
if dynamic access came before certain requests types, e.g. POSTs.
parent a7930c3c
......@@ -41,17 +41,17 @@ export function createDynamicallyTrackedSearchParams(
// If we forced static we omit searchParams entirely. This is true both during SSR
// and browser render because we need there to be parity between these environments
return {}
} else if (!store.isStaticGeneration && !store.dynamicShouldError) {
// during dynamic renders we don't actually have to track anything so we just return
// the searchParams directly. However if dynamic data access should error then we
// still want to track access. This covers the case in Dev where all renders are dynamic
// but we still want to error if you use a dynamic data source because it will fail the build
// or revalidate if you do.
return searchParams
} else {
// We need to track dynamic access with a Proxy. We implement get, has, and ownKeys because
// We need to track dynamic access with a Proxy. If `dynamic = "error"`, we use this information
// to fail the build. This also signals to the patched fetch that it's inside
// of a dynamic render and should bail from data cache. We implement get, has, and ownKeys because
// these can all be used to exfiltrate information about searchParams.
return new Proxy({} as ParsedUrlQuery, {
const trackedSearchParams: ParsedUrlQuery = store.isStaticGeneration
? {}
: searchParams
return new Proxy(trackedSearchParams, {
get(target, prop, receiver) {
if (typeof prop === 'string') {
trackDynamicDataAccessed(store, `searchParams.${prop}`)
......
......@@ -780,6 +780,8 @@ describe('app-dir static/dynamic handling', () => {
"blog/tim.rsc",
"blog/tim/first-post.html",
"blog/tim/first-post.rsc",
"default-cache-search-params/page.js",
"default-cache-search-params/page_client-reference-manifest.js",
"default-cache/page.js",
"default-cache/page_client-reference-manifest.js",
"dynamic-error/[id]/page.js",
......@@ -2203,6 +2205,32 @@ describe('app-dir static/dynamic handling', () => {
}, 'success')
})
it('should cache correctly when accessing search params opts into dynamic rendering', async () => {
const res = await next.fetch('/default-cache-search-params')
expect(res.status).toBe(200)
let prevHtml = await res.text()
let prev$ = cheerio.load(prevHtml)
await retry(async () => {
const curRes = await next.fetch('/default-cache-search-params')
expect(curRes.status).toBe(200)
const curHtml = await curRes.text()
const cur$ = cheerio.load(curHtml)
expect(cur$('#data-default-cache').text()).not.toBe(
prev$('#data-default-cache').text()
)
expect(cur$('#data-request-cache').text()).not.toBe(
prev$('#data-request-cache').text()
)
expect(cur$('#data-cache-auto').text()).not.toBe(
prev$('#data-cache-auto').text()
)
})
})
it('should cache correctly for fetchCache = force-cache', async () => {
const res = await next.fetch('/force-cache')
expect(res.status).toBe(200)
......
export default async function Page({ searchParams }) {
// this page is using searchParams to opt into dynamic rendering
// meaning the page will bail from ISR cache and hint to patch-fetch
// that it's in a dynamic scope and shouldn't cache the fetch.
console.log(searchParams.q)
const data1 = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random?1',
{ cache: 'default' }
).then((res) => res.text())
const data2 = await fetch(
new Request('https://next-data-api-endpoint.vercel.app/api/random?2')
).then((res) => res.text())
const data3 = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random?3'
).then((res) => res.text())
return (
<>
<p>/default-cache-fetch</p>
<p id="data-default-cache">"cache: default" {data1}</p>
<p id="data-request-cache">
"cache: default" (Request constructor) {data2}
</p>
<p id="data-cache-auto">"cache: auto" {data3}</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