Skip to content

Commit

Permalink
[unstable_cache] Don't track dynamic fetches in an unstable_cache cal…
Browse files Browse the repository at this point in the history
…lback (#65010)

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->

### What?

When pages are rendered using partial prerendering (PPR), they can
easily trigger cases that attempt to interrupt static rendering. If a
user calls `fetch` from within a `unstable_cache` callback with `cache:
"no-store"`, we currently break out, as that fetch operation is called
within a cached callback.

### Why?

Without this change, using `unstable_cache` would have limited affect
when dealing with external code that explicitly sets `cache: "no-store"`
preventing it from utilizing the user-specified-cache.

### How?

This modifies the fetch patching so it doesn't bail static rendering due
to dynamic fetches within an `unstable_cache` callback.

Closes NEXT-3220
  • Loading branch information
wyattjoh authored Apr 26, 2024
1 parent 5449196 commit f64db55
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 29 deletions.
8 changes: 5 additions & 3 deletions packages/next/src/server/app-render/dynamic-rendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,11 @@ export function trackDynamicFetch(
store: StaticGenerationStore,
expression: string
) {
if (store.prerenderState) {
postponeWithTracking(store.prerenderState, expression, store.urlPathname)
}
// If we aren't in a prerender, or we're in an unstable cache callback, we
// don't need to postpone.
if (!store.prerenderState || store.isUnstableCacheCallback) return

postponeWithTracking(store.prerenderState, expression, store.urlPathname)
}

function postponeWithTracking(
Expand Down
43 changes: 30 additions & 13 deletions packages/next/src/server/lib/patch-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,22 @@ function createPatchedFetcher(
},
async () => {
// If this is an internal fetch, we should not do any special treatment.
if (isInternal) return originFetch(input, init)
if (isInternal) {
return originFetch(input, init)
}

const staticGenerationStore = staticGenerationAsyncStorage.getStore()

// If the staticGenerationStore is not available, we can't do any
// special treatment of fetch, therefore fallback to the original
// fetch implementation.
if (!staticGenerationStore || staticGenerationStore.isDraftMode) {
if (!staticGenerationStore) {
return originFetch(input, init)
}

// We should also fallback to the original fetch implementation if we
// are in draft mode, it does not constitute a static generation.
if (staticGenerationStore.isDraftMode) {
return originFetch(input, init)
}

Expand Down Expand Up @@ -686,14 +694,18 @@ function createPatchedFetcher(
// If enabled, we should bail out of static generation.
trackDynamicFetch(staticGenerationStore, dynamicUsageReason)

// PPR is not enabled, or React postpone is not available, we
// should set the revalidate to 0.
staticGenerationStore.revalidate = 0
// If partial prerendering is not enabled, then we should throw an
// error to indicate that this fetch is dynamic.
if (!staticGenerationStore.prerenderState) {
// PPR is not enabled, or React postpone is not available, we
// should set the revalidate to 0.
staticGenerationStore.revalidate = 0

const err = new DynamicServerError(dynamicUsageReason)
staticGenerationStore.dynamicUsageErr = err
staticGenerationStore.dynamicUsageDescription = dynamicUsageReason
throw err
const err = new DynamicServerError(dynamicUsageReason)
staticGenerationStore.dynamicUsageErr = err
staticGenerationStore.dynamicUsageDescription = dynamicUsageReason
throw err
}
}

const hasNextConfig = 'next' in init
Expand All @@ -718,10 +730,15 @@ function createPatchedFetcher(
// If enabled, we should bail out of static generation.
trackDynamicFetch(staticGenerationStore, dynamicUsageReason)

const err = new DynamicServerError(dynamicUsageReason)
staticGenerationStore.dynamicUsageErr = err
staticGenerationStore.dynamicUsageDescription = dynamicUsageReason
throw err
// If partial prerendering is not enabled, then we should throw an
// error to indicate that this fetch is dynamic.
if (!staticGenerationStore.prerenderState) {
const err = new DynamicServerError(dynamicUsageReason)
staticGenerationStore.dynamicUsageErr = err
staticGenerationStore.dynamicUsageDescription =
dynamicUsageReason
throw err
}
}

if (!staticGenerationStore.forceStatic || next.revalidate !== 0) {
Expand Down
23 changes: 10 additions & 13 deletions test/e2e/app-dir/app-static/app-static.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import globOrig from 'glob'
import cheerio from 'cheerio'
import { promisify } from 'util'
import { join } from 'path'
import { promisify } from 'node:util'
import { join } from 'node:path'
import { nextTestSetup } from 'e2e-utils'
import {
check,
Expand Down Expand Up @@ -211,24 +211,22 @@ describe('app-dir static/dynamic handling', () => {
if (isApi) {
prevData = await res.json()
} else {
const initialHtml = await res.text()
const initial$ = isApi ? undefined : cheerio.load(initialHtml)
prevData = JSON.parse(initial$('#props').text())
const $ = isApi ? undefined : cheerio.load(await res.text())
prevData = JSON.parse($('#props').text())
}

expect(prevData.data.random).toBeTruthy()

await check(async () => {
await retry(async () => {
res = await next.fetch(pathname)
expect(res.status).toBe(200)
let curData

let curData
if (isApi) {
curData = await res.json()
} else {
const curHtml = await res.text()
const cur$ = cheerio.load(curHtml)
curData = JSON.parse(cur$('#props').text())
const $ = cheerio.load(await res.text())
curData = JSON.parse($('#props').text())
}

try {
Expand All @@ -237,8 +235,7 @@ describe('app-dir static/dynamic handling', () => {
} finally {
prevData = curData
}
return 'success'
}, 'success')
})
})

it('should not have cache tags header for non-minimal mode', async () => {
Expand Down Expand Up @@ -2323,7 +2320,7 @@ describe('app-dir static/dynamic handling', () => {
/partial-gen-params fetch ([\d]{1,})/
)

if (matches[1]) {
if (matches?.[1]) {
langFetchSlug = matches[1]
slugFetchSlug = langFetchSlug
}
Expand Down
7 changes: 7 additions & 0 deletions test/e2e/app-dir/ppr-unstable-cache/app/layout.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Layout({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
37 changes: 37 additions & 0 deletions test/e2e/app-dir/ppr-unstable-cache/app/page.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { unstable_cache } from 'next/cache'

const getData = unstable_cache(
async () => {
const noStore = await fetch(
process.env.TEST_DATA_SERVER + '?cache=no-store',
{ method: 'GET', cache: 'no-store' }
).then((res) => res.text())

const forceCache = await fetch(
process.env.TEST_DATA_SERVER + '?cache=force-cache',
{ method: 'GET', cache: 'force-cache' }
).then((res) => res.text())

return JSON.stringify(
{
random: Math.floor(Math.random() * 1000).toString(),
data: {
forceCache,
noStore,
},
},
null,
2
)
},
undefined,
{
tags: ['unstable-cache-fetch'],
}
)

export default async function Page() {
const data = await getData()

return <pre id="data">{data}</pre>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { revalidateTag } from 'next/cache'

export const POST = async () => {
revalidateTag('unstable-cache-fetch')
return new Response('OK', { status: 200 })
}
5 changes: 5 additions & 0 deletions test/e2e/app-dir/ppr-unstable-cache/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
experimental: {
ppr: true,
},
}
103 changes: 103 additions & 0 deletions test/e2e/app-dir/ppr-unstable-cache/ppr-unstable-cache.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { NextInstance, createNext, isNextDeploy, isNextDev } from 'e2e-utils'
import { findPort } from 'next-test-utils'
import http from 'node:http'

describe('ppr-unstable-cache', () => {
if (isNextDeploy) {
it.skip('should not run in deploy mode', () => {})
return
}

if (isNextDev) {
it.skip('should not run in dev mode', () => {})
return
}

let next: NextInstance | null = null
let server: http.Server | null = null
afterEach(async () => {
if (next) {
await next.destroy()
next = null
}

if (server) {
await server.close()
server = null
}
})

it('should not cache inner fetch calls', async () => {
let generations: string[] = []
server = http.createServer(async (req, res) => {
try {
if (!req.url) throw new Error('No URL')

const cache = new URL(req.url, 'http://n').searchParams.get('cache')
if (!cache) throw new Error('No cache key')

const random = Math.floor(Math.random() * 1000).toString()
const data = cache + ':' + random
generations.push(data)
res.end(data)
} catch (err) {
res.statusCode = 500
res.end(err.message)
}
})
const port = await findPort()
server.listen(port)

next = await createNext({
files: __dirname,
env: { TEST_DATA_SERVER: `http://localhost:${port}/` },
})

expect(generations).toHaveLength(3)

const first = await next
.render$('/')
.then(($) => JSON.parse($('#data').text()))

expect(generations).toHaveLength(3)

expect(first.data.forceCache).toBeOneOf(generations)
expect(first.data.noStore).toBeOneOf(generations)

// Try a few more times, we should always get the same result.
for (let i = 0; i < 3; i++) {
const again = await next
.render$('/')
.then(($) => JSON.parse($('#data').text()))

expect(generations).toHaveLength(3)
expect(first).toEqual(again)
}

// Revalidate the tag associated with the `unstable_cache` call.
const revalidate = await next.fetch('/revalidate-tag', { method: 'POST' })
expect(revalidate.status).toBe(200)
await revalidate.text()

const revalidated = await next
.render$('/')
.then(($) => JSON.parse($('#data').text()))

// Expect that the `cache: no-store` value has been updated, but not
// the `cache: force-cache` value.
expect(generations).toHaveLength(5)

// We know now that the generations have been updated, so let's try to
// validate the value. We don't need to do this within the retry.
expect(revalidated.random).not.toEqual(first.random)
expect(revalidated.data.forceCache).toBeOneOf(generations.slice(0, 3))
expect(revalidated.data.noStore).toBeOneOf(generations.slice(3))
expect(revalidated).not.toEqual(first)

// Ensure that the `force-cache` value has not been updated, and only called
// once.
expect(generations.filter((g) => g.startsWith('force-cache'))).toHaveLength(
1
)
})
})

0 comments on commit f64db55

Please sign in to comment.