Unverified Commit 72611f31 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Add test to verify which pages are prerendered in `use-cache` test suite (#75681)

This allows us to catch regressions and monitor progress (e.g. for NAR-85).

In addition, we're consolidating all pages in the test app to use a single root layout without a suspense boundary. Before we switched the app from using the `dynamicIO` flag to the `useCache` flag, some pages needed a suspense boundary to not fail the build.
parent eb789ecf
import { Suspense } from 'react'
export default function Root({ children }: { children: React.ReactNode }) {
return (
<html>
<body>
<Suspense fallback={null}>{children}</Suspense>
</body>
</html>
)
}
...@@ -4,7 +4,10 @@ async function getCachedRandom(n: number) { ...@@ -4,7 +4,10 @@ async function getCachedRandom(n: number) {
} }
export async function generateStaticParams() { export async function generateStaticParams() {
return [{ id: await getCachedRandom(10) }, { id: await getCachedRandom(2) }] return [
{ id: `a${await getCachedRandom(9)}` },
{ id: `b${await getCachedRandom(2)}` },
]
} }
export default async function Page() { export default async function Page() {
......
'use client' 'use client'
import { foo, bar, baz } from './cached' import { foo, bar, baz } from './cached'
import { Form } from '../../form' import { Form } from '../form'
export default function Page() { export default function Page() {
return <Form foo={foo} bar={bar} baz={baz} /> return <Form foo={foo} bar={bar} baz={baz} />
......
import { Foo } from '../client' import { Foo } from './client'
async function getCachedRandom(x: number, children: React.ReactNode) { async function getCachedRandom(x: number, children: React.ReactNode) {
'use cache' 'use cache'
......
import { Form } from '../../form' import { Form } from '../form'
function getRandomValue() { function getRandomValue() {
const v = Math.random() const v = Math.random()
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
*/ */
const nextConfig = { const nextConfig = {
experimental: { experimental: {
ppr: process.env.__NEXT_EXPERIMENTAL_PPR === 'true',
useCache: true, useCache: true,
cacheLife: { cacheLife: {
frequent: { frequent: {
......
...@@ -277,6 +277,49 @@ describe('use-cache', () => { ...@@ -277,6 +277,49 @@ describe('use-cache', () => {
} }
if (isNextStart) { if (isNextStart) {
it('should prerender fully cacheable pages as static HTML', async () => {
const prerenderManifest = JSON.parse(
await next.readFile('.next/prerender-manifest.json')
)
let prerenderedRoutes = Object.keys(prerenderManifest.routes).sort()
if (process.env.__NEXT_EXPERIMENTAL_PPR === 'true') {
// For the purpose of this test we don't consider an incomplete shell.
prerenderedRoutes = prerenderedRoutes.filter((route) => {
const filename = route.replace(/^\//, '').replace(/^$/, 'index')
return next
.readFileSync(`.next/server/app/${filename}.html`)
.endsWith('</html>')
})
}
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',
// TODO(useCache): Should be prerendered when NAR-85 is fixed.
// '/form',
'/imported-from-client',
'/logs',
'/method-props',
// TODO(useCache): Should be prerendered when NAR-85 is fixed.
// '/not-found',
'/passed-to-client',
'/react-cache',
'/static-class-method',
'/use-action-state',
// TODO(useCache): Should be prerendered when NAR-85 is fixed.
// '/with-server-action',
])
})
it('should match the expected revalidate config on the prerender manifest', async () => { it('should match the expected revalidate config on the prerender manifest', async () => {
const prerenderManifest = JSON.parse( const prerenderManifest = JSON.parse(
await next.readFile('.next/prerender-manifest.json') await next.readFile('.next/prerender-manifest.json')
......
import os from 'os' import os from 'os'
import path from 'path' import path from 'path'
import { existsSync, promises as fs, rmSync } from 'fs' import { existsSync, promises as fs, rmSync, readFileSync } from 'fs'
import treeKill from 'tree-kill' import treeKill from 'tree-kill'
import type { NextConfig } from 'next' import type { NextConfig } from 'next'
import { FileRef, isNextDeploy } from '../e2e-utils' import { FileRef, isNextDeploy } from '../e2e-utils'
...@@ -521,6 +521,10 @@ export class NextInstance { ...@@ -521,6 +521,10 @@ export class NextInstance {
return fs.readFile(path.join(this.testDir, filename), 'utf8') return fs.readFile(path.join(this.testDir, filename), 'utf8')
} }
public readFileSync(filename: string) {
return readFileSync(path.join(this.testDir, filename), 'utf8')
}
public async readJSON(filename: string) { public async readJSON(filename: string) {
return JSON.parse( return JSON.parse(
await fs.readFile(path.join(this.testDir, filename), 'utf-8') await fs.readFile(path.join(this.testDir, filename), 'utf-8')
......
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