Unverified Commit af74b4a3 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Include `use-cache` test suite in the Cache Components tests (#81610)

We're splitting the test app routes between two root layouts, one for (partially) static pages that does not wrap its children in a Suspense boundary, and one for dynamic pages that adds a Suspense boundary above `body`, which opts those pages into fully dynamic rendering.

Two tests are expected to fail for now, because they revealed bugs related to revalidation, which will be fixed in follow-up PRs:
- https://github.com/vercel/next.js/pull/81611
- https://github.com/vercel/next.js/pull/81621

closes NAR-171
parent d8fdb6ff
......@@ -27,6 +27,12 @@
"searchparams-reuse-loading should re-use loading from \"full\" prefetch for param-less URL when navigating to param-full route"
]
},
"test/e2e/app-dir/use-cache/use-cache.test.ts": {
"failed": [
"use-cache should revalidate before redirecting in a route handler",
"use-cache should update after unstable_expireTag correctly"
]
},
"test/production/app-dir/browser-chunks/browser-chunks.test.ts": {
"failed": [
"browser-chunks must not bundle any server modules into browser chunks"
......@@ -278,7 +284,6 @@
"test/e2e/app-dir/use-cache-segment-configs/use-cache-segment-configs.test.ts",
"test/e2e/app-dir/use-cache-standalone-search-params/use-cache-standalone-search-params.test.ts",
"test/e2e/app-dir/use-cache-without-experimental-flag/use-cache-without-experimental-flag.test.ts",
"test/e2e/app-dir/use-cache/use-cache.test.ts",
"test/e2e/app-dir/use-params/use-params.test.ts",
"test/e2e/app-dir/use-selected-layout-segment-s/use-selected-layout-segment-s.test.ts",
"test/e2e/app-dir/use-server-inserted-html/use-server-inserted-html.test.ts",
......
import { Suspense } from 'react'
export default function Root({ children }: { children: React.ReactNode }) {
return (
<Suspense>
<html>
<body>{children}</body>
</html>
</Suspense>
)
}
import { Foo } from './client'
import { Foo } from '../client'
async function getCachedRandom(x: number, children: React.ReactNode) {
'use cache'
......
......@@ -12,7 +12,7 @@ export default async function Page() {
return (
<form action={refresh}>
<button id="refresh">Refresh</button>
<p id="t">{Date.now()}</p>
<p id="t">{new Date().toISOString()}</p>
</form>
)
}
'use client'
import { foo, bar, baz } from './cached'
import { Form } from '../form'
import { Form } from '../../form'
export default function Page() {
export function ClientComponent() {
return <Form foo={foo} bar={bar} baz={baz} />
}
import { ClientComponent } from './client-component'
export default function Page() {
return <ClientComponent />
}
import { Form } from '../form'
import { Form } from '../../form'
function getRandomValue() {
const v = Math.random()
......
......@@ -3,7 +3,7 @@
import { useActionState } from 'react'
import { getRandomValue } from './cached'
export default function Page() {
export function Form() {
const [result, formAction, isPending] = useActionState(getRandomValue, -1)
return (
......
import { Form } from './form'
export default function Page() {
return <Form />
}
......@@ -8,7 +8,7 @@ const nextConfig = {
frequent: {
stale: 19,
revalidate: 100,
expire: 250,
expire: 300,
},
},
cacheHandlers: {
......
import { nextTestSetup } from 'e2e-utils'
import { retry, waitFor } from 'next-test-utils'
import { retry } from 'next-test-utils'
import stripAnsi from 'strip-ansi'
import { format } from 'util'
import { Playwright } from 'next-webdriver'
......@@ -172,7 +172,7 @@ describe('use-cache', () => {
expect(rand1).toEqual(rand2)
})
it('should revalidate before redirecting in a route handlers', async () => {
it('should revalidate before redirecting in a route handler', async () => {
const initialValues = await next.fetch('/api').then((res) => res.json())
const values = await next
......@@ -473,14 +473,24 @@ describe('use-cache', () => {
it('should prerender fully cacheable pages as static HTML', async () => {
const prerenderManifest = JSON.parse(
await next.readFile('.next/prerender-manifest.json')
)
) as PrerenderManifest
let prerenderedRoutes = Object.keys(prerenderManifest.routes).sort()
const withPPR = process.env.__NEXT_EXPERIMENTAL_PPR === 'true'
if (process.env.__NEXT_EXPERIMENTAL_PPR === 'true') {
const withCacheComponents =
process.env.__NEXT_EXPERIMENTAL_CACHE_COMPONENTS === 'true'
let prerenderedRoutes = Object.entries(prerenderManifest.routes)
if (withPPR || withCacheComponents) {
// For the purpose of this test we don't consider an incomplete shell.
prerenderedRoutes = prerenderedRoutes.filter((route) => {
const filename = route.replace(/^\//, '').replace(/^$/, 'index')
prerenderedRoutes = prerenderedRoutes.filter(([pathname, route]) => {
const filename = pathname.replace(/^\//, '').replace(/^$/, 'index')
// A prerendered route handler does not have a dataRoute (i.e. RSC).
if (!route.dataRoute) {
return true
}
return next
.readFileSync(`.next/server/app/${filename}.html`)
......@@ -488,34 +498,42 @@ describe('use-cache', () => {
})
}
expect(prerenderedRoutes).toEqual([
// [id] route, first entry in generateStaticParams
expect.stringMatching(/\/a\d/),
// [id] route, second entry in generateStaticParams
expect.stringMatching(/\/b\d/),
'/cache-fetch',
'/cache-fetch-no-store',
'/cache-life',
'/cache-tag',
'/directive-in-node-modules/with-handler',
'/directive-in-node-modules/without-handler',
'/draft-mode',
'/form',
'/imported-from-client',
'/logs',
'/method-props',
'/nested-in-unstable-cache',
'/not-found',
'/on-demand-revalidate',
'/passed-to-client',
'/react-cache',
'/referential-equality',
'/revalidate-and-redirect/redirect',
'/rsc-payload',
'/static-class-method',
'/use-action-state',
'/with-server-action',
])
const prerenderedRouteKeys = prerenderedRoutes
.map(([routeKey]) => routeKey)
.sort()
expect(prerenderedRouteKeys).toEqual(
[
// [id] route, first entry in generateStaticParams
expect.stringMatching(/\/a\d/),
withCacheComponents && '/api',
// [id] route, second entry in generateStaticParams
expect.stringMatching(/\/b\d/),
'/cache-fetch',
'/cache-fetch-no-store',
'/cache-life',
'/cache-tag',
'/directive-in-node-modules/with-handler',
'/directive-in-node-modules/without-handler',
'/draft-mode',
'/form',
'/imported-from-client',
'/logs',
'/method-props',
'/nested-in-unstable-cache',
'/not-found',
'/on-demand-revalidate',
'/passed-to-client',
'/react-cache',
'/referential-equality',
'/revalidate-and-redirect/redirect',
'/rsc-payload',
'/static-class-method',
withCacheComponents && '/unhandled-promise-regression',
'/use-action-state',
'/with-server-action',
].filter(Boolean)
)
})
it('should match the expected revalidate and expire configs on the prerender manifest', async () => {
......@@ -527,7 +545,7 @@ describe('use-cache', () => {
// custom cache life profile "frequent"
expect(routes['/cache-life'].initialRevalidateSeconds).toBe(100)
expect(routes['/cache-life'].initialExpireSeconds).toBe(250)
expect(routes['/cache-life'].initialExpireSeconds).toBe(300)
// default expireTime
expect(routes['/cache-fetch'].initialExpireSeconds).toBe(31536000)
......@@ -554,8 +572,8 @@ describe('use-cache', () => {
let response = await next.fetch('/cache-life')
expect(response.headers.get('cache-control')).toBe(
// revalidate is set to 100, expire is set to 250 => SWR 150
's-maxage=100, stale-while-revalidate=150'
// revalidate is set to 100, expire is set to 300 => SWR 200
's-maxage=100, stale-while-revalidate=200'
)
response = await next.fetch('/cache-fetch')
......@@ -597,11 +615,10 @@ describe('use-cache', () => {
await browser.elementByCss('#refresh').click()
await waitFor(500)
const time3 = await browser.waitForElementByCss('#t').text()
expect(time3).not.toBe(time2)
await retry(async () => {
const time3 = await browser.waitForElementByCss('#t').text()
expect(time3).not.toBe(time2)
})
// Reloading again should ideally be the same value but because the Action seeds
// the cache with real params as the argument it has a different cache key.
......@@ -878,26 +895,33 @@ describe('use-cache', () => {
})
if (isNextDev) {
it('should not have unhandled rejection of Request data promises when use cache is enabled without dynamicIO', async () => {
await next.render('/unhandled-promise-regression')
// We assert both to better defend against changes in error messaging invalidating this test silently.
// They are today asserting the same thing
expect(next.cliOutput).not.toContain(
'During prerendering, `cookies()` rejects when the prerender is complete.'
)
expect(next.cliOutput).not.toContain(
'During prerendering, `headers()` rejects when the prerender is complete.'
)
expect(next.cliOutput).not.toContain(
'During prerendering, `connection()` rejects when the prerender is complete.'
)
expect(next.cliOutput).not.toContain('HANGING_PROMISE_REJECTION')
})
if (process.env.__NEXT_EXPERIMENTAL_CACHE_COMPONENTS !== 'true') {
it('should not have unhandled rejection of Request data promises when use cache is enabled without dynamicIO', async () => {
await next.render('/unhandled-promise-regression')
// We assert both to better defend against changes in error messaging invalidating this test silently.
// They are today asserting the same thing
expect(next.cliOutput).not.toContain(
'During prerendering, `cookies()` rejects when the prerender is complete.'
)
expect(next.cliOutput).not.toContain(
'During prerendering, `headers()` rejects when the prerender is complete.'
)
expect(next.cliOutput).not.toContain(
'During prerendering, `connection()` rejects when the prerender is complete.'
)
expect(next.cliOutput).not.toContain('HANGING_PROMISE_REJECTION')
})
}
it('replays logs from "use cache" functions', async () => {
const browser = await next.browser('/logs')
const initialLogs = await getSanitizedLogs(browser)
const expectedOutsideBadge =
process.env.__NEXT_EXPERIMENTAL_CACHE_COMPONENTS === 'true'
? 'Prerender'
: 'Server'
// We ignore the logged time string at the end of this message:
const logMessageWithDateRegexp = /^ Cache {2}deep inside /
......@@ -906,7 +930,7 @@ describe('use-cache', () => {
await retry(async () => {
expect(initialLogs).toMatchObject(
expect.arrayContaining([
' Server outside',
` ${expectedOutsideBadge} outside`,
' Cache inside',
expect.stringMatching(logMessageWithDateRegexp),
])
......@@ -929,7 +953,7 @@ describe('use-cache', () => {
expect(newLogs).toMatchObject(
expect.arrayContaining([
' Server outside',
` ${expectedOutsideBadge} outside`,
' Cache inside',
logMessageWithCachedDate,
])
......
......@@ -6236,7 +6236,7 @@
"use-cache should prerender fully cacheable pages as static HTML",
"use-cache should propagate unstable_cache tags correctly",
"use-cache should return the same object reference for multiple invocations",
"use-cache should revalidate before redirecting in a route handlers",
"use-cache should revalidate before redirecting in a route handler",
"use-cache should revalidate caches after redirect",
"use-cache should revalidate caches during on-demand revalidation",
"use-cache should revalidate caches nested in unstable_cache",
......@@ -9927,7 +9927,7 @@
"use-cache should override fetch with cookies/auth in use cache properly",
"use-cache should override fetch with no-store in use cache properly",
"use-cache should return the same object reference for multiple invocations",
"use-cache should revalidate before redirecting in a route handlers",
"use-cache should revalidate before redirecting in a route handler",
"use-cache should revalidate caches after redirect",
"use-cache should revalidate caches during on-demand revalidation",
"use-cache should revalidate caches nested in unstable_cache",
......@@ -6096,7 +6096,7 @@
"use-cache should prerender fully cacheable pages as static HTML",
"use-cache should propagate unstable_cache tags correctly",
"use-cache should return the same object reference for multiple invocations",
"use-cache should revalidate before redirecting in a route handlers",
"use-cache should revalidate before redirecting in a route handler",
"use-cache should revalidate caches after redirect",
"use-cache should revalidate caches during on-demand revalidation",
"use-cache should revalidate caches nested in unstable_cache",
......@@ -9673,7 +9673,7 @@
"use-cache should override fetch with cookies/auth in use cache properly",
"use-cache should override fetch with no-store in use cache properly",
"use-cache should return the same object reference for multiple invocations",
"use-cache should revalidate before redirecting in a route handlers",
"use-cache should revalidate before redirecting in a route handler",
"use-cache should revalidate caches after redirect",
"use-cache should revalidate caches during on-demand revalidation",
"use-cache should revalidate caches nested in unstable_cache",
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