Skip to content

Commit

Permalink
Add test to verify which pages are prerendered in use-cache test su…
Browse files Browse the repository at this point in the history
…ite (vercel#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.
  • Loading branch information
unstubbable authored Feb 5, 2025
1 parent eb789ec commit 72611f3
Show file tree
Hide file tree
Showing 37 changed files with 56 additions and 16 deletions.
11 changes: 0 additions & 11 deletions test/e2e/app-dir/use-cache/app/(suspense)/layout.tsx

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ async function getCachedRandom(n: number) {
}

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() {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client'

import { foo, bar, baz } from './cached'
import { Form } from '../../form'
import { Form } from '../form'

export default function Page() {
return <Form foo={foo} bar={bar} baz={baz} />
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Foo } from '../client'
import { Foo } from './client'

async function getCachedRandom(x: number, children: React.ReactNode) {
'use cache'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Form } from '../../form'
import { Form } from '../form'

function getRandomValue() {
const v = Math.random()
Expand Down
1 change: 1 addition & 0 deletions test/e2e/app-dir/use-cache/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
const nextConfig = {
experimental: {
ppr: process.env.__NEXT_EXPERIMENTAL_PPR === 'true',
useCache: true,
cacheLife: {
frequent: {
Expand Down
43 changes: 43 additions & 0 deletions test/e2e/app-dir/use-cache/use-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,49 @@ describe('use-cache', () => {
}

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 () => {
const prerenderManifest = JSON.parse(
await next.readFile('.next/prerender-manifest.json')
Expand Down
6 changes: 5 additions & 1 deletion test/lib/next-modes/base.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os from 'os'
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 type { NextConfig } from 'next'
import { FileRef, isNextDeploy } from '../e2e-utils'
Expand Down Expand Up @@ -521,6 +521,10 @@ export class NextInstance {
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) {
return JSON.parse(
await fs.readFile(path.join(this.testDir, filename), 'utf-8')
Expand Down

0 comments on commit 72611f3

Please sign in to comment.