Unverified Commit e20f127a authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Fix missing non-deferred turbopack build items (#89616)

This expands test suite for deferred entries experimental handling and
fixes missing handling for metadata routes and route handlers when
building the non-deferred entries.
parent 355d4484
......@@ -2,6 +2,11 @@
import { saveCpuProfile } from '../../server/lib/cpu-profile'
import path from 'path'
import { validateTurboNextConfig } from '../../lib/turbopack-warning'
import { isMetadataRouteFile } from '../../lib/metadata/is-metadata-route'
import {
normalizeMetadataPageToRoute,
normalizeMetadataRoute,
} from '../../lib/metadata/get-metadata-route'
import { isFileSystemCacheEnabledForBuild } from '../../shared/lib/turbopack/utils'
import { NextBuildContext } from '../build-context'
import { createDefineEnv, getBindingsSync } from '../swc'
......@@ -12,7 +17,6 @@ import {
} from '../handle-entrypoints'
import { TurbopackManifestLoader } from '../../shared/lib/turbopack/manifest-loader'
import { promises as fs } from 'fs'
import type { Stats } from 'fs'
import { PHASE_PRODUCTION_BUILD } from '../../shared/lib/constants'
import loadConfig from '../../server/config'
import { hasCustomExportOutput } from '../../export/utils'
......@@ -22,7 +26,11 @@ import { isCI } from '../../server/ci-info'
import { backgroundLogCompilationEvents } from '../../shared/lib/turbopack/compilation-events'
import { getSupportedBrowsers, printBuildErrors } from '../utils'
import { normalizePath } from '../../lib/normalize-path'
import { collectPagesFiles } from '../route-discovery'
import {
collectAppFiles,
collectPagesFiles,
getPageFromPath,
} from '../route-discovery'
import { createValidFileMatcher } from '../../server/lib/find-page-file'
import type {
ProjectOptions,
......@@ -30,6 +38,15 @@ import type {
TurbopackResult,
} from '../swc/types'
/**
* Convert an app route path to debugBuildPaths format.
* e.g. '/' -> '/page', '/blog' -> '/blog/page'
*/
function toAppDebugPath(route: string, leaf: 'page' | 'route'): string {
const routePath = route.startsWith('/') ? route : `/${route}`
return routePath === '/' ? `/${leaf}` : `${routePath}/${leaf}`
}
/**
* Convert deferred entry routes to debugBuildPaths format.
* Routes like '/deferred' become '/deferred/page' for the filter.
......@@ -37,86 +54,49 @@ import type {
*/
function getDeferredBuildPaths(
deferredEntries: string[],
pagesPaths: string[]
pagesPaths: string[],
metadataAppPaths: string[]
): {
app: string[]
pages: string[]
} {
return {
app: deferredEntries.map((route) => {
const routePath = route.startsWith('/') ? route : `/${route}`
return `${routePath}/page`
}),
app: [
...new Set([
...deferredEntries.map((route) => toAppDebugPath(route, 'page')),
...metadataAppPaths,
]),
],
// Include all pages routes so they are not filtered out
pages: pagesPaths,
}
}
/**
* Scan the app directory to find all page routes, then filter out deferred entries.
* Returns debugBuildPaths for non-deferred routes only.
* Collect app entries and filter out deferred app pages only.
* This keeps non-page app entries (e.g. route handlers) in the non-deferred build.
* @param pagesPaths - All pages routes to include (deferred entries only affects app routes)
*/
async function getNonDeferredBuildPaths(
appDir: string | undefined,
function getNonDeferredBuildPaths(
appPaths: string[],
deferredEntries: string[],
pageExtensions: string[],
pagesPaths: string[]
): Promise<{ app: string[]; pages: string[] } | null> {
if (!appDir || deferredEntries.length === 0) {
): { app: string[]; pages: string[] } | null {
if (deferredEntries.length === 0) {
return null
}
const deferredSet = new Set(
deferredEntries.map((route) =>
route.startsWith('/') ? route : `/${route}`
)
const deferredPagePaths = new Set(
deferredEntries.map((route) => toAppDebugPath(route, 'page'))
)
const nonDeferredAppPaths = appPaths.filter(
(appPath) =>
!deferredPagePaths.has(getPageFromPath(appPath, pageExtensions))
)
const appRoutes: string[] = []
// Recursively scan for page files
async function scanDir(dir: string, routePath: string): Promise<void> {
let entries: string[]
try {
entries = await fs.readdir(dir)
} catch {
return
}
for (const entry of entries) {
const fullPath = path.join(dir, entry)
let stat: Stats
try {
stat = await fs.stat(fullPath)
} catch {
continue
}
if (stat.isDirectory()) {
// Recurse into subdirectory
const subRoute =
routePath === '/' ? `/${entry}` : `${routePath}/${entry}`
await scanDir(fullPath, subRoute)
} else if (stat.isFile()) {
// Check if this is a page file
for (const ext of pageExtensions) {
if (entry === `page.${ext}`) {
// Found a page file, add to routes if not deferred
if (!deferredSet.has(routePath)) {
appRoutes.push(`${routePath}/page`)
}
break
}
}
}
}
}
await scanDir(appDir, '/')
return {
app: appRoutes,
app: nonDeferredAppPaths,
// Include all pages routes so they are not filtered out
pages: pagesPaths,
}
......@@ -157,6 +137,8 @@ export async function turbopackBuild(): Promise<{
// Collect all pages paths when using deferred entries to ensure pages routes
// are not filtered out (deferred entries only affects app routes)
let pagesPaths: string[] = []
let appPaths: string[] = []
let metadataAppPaths: string[] = []
if (hasDeferredEntries && NextBuildContext.pagesDir) {
const validFileMatcher = createValidFileMatcher(
config.pageExtensions!,
......@@ -167,20 +149,50 @@ export async function turbopackBuild(): Promise<{
validFileMatcher
)
}
if (hasDeferredEntries && NextBuildContext.appDir) {
const validFileMatcher = createValidFileMatcher(
config.pageExtensions!,
NextBuildContext.appDir
)
;({ appPaths } = await collectAppFiles(
NextBuildContext.appDir,
validFileMatcher
))
metadataAppPaths = Array.from(
new Set(
appPaths.flatMap((appPath) => {
if (!isMetadataRouteFile(appPath, config.pageExtensions!, true)) {
return []
}
const page = getPageFromPath(appPath, config.pageExtensions!)
const route = normalizeMetadataRoute(page)
return [
appPath,
page,
route,
normalizeMetadataPageToRoute(route, false),
normalizeMetadataPageToRoute(route, true),
]
})
)
)
}
// For deferred entries, we use debugBuildPaths to control which routes are built
// First build excludes deferred entries, second build includes only deferred entries
const nonDeferredBuildPaths = hasDeferredEntries
? await getNonDeferredBuildPaths(
NextBuildContext.appDir,
deferredEntries,
config.pageExtensions!,
pagesPaths
)
: null
const deferredBuildPaths = hasDeferredEntries
? getDeferredBuildPaths(deferredEntries, pagesPaths)
: null
const nonDeferredBuildPaths =
hasDeferredEntries && NextBuildContext.appDir
? getNonDeferredBuildPaths(
appPaths,
deferredEntries,
config.pageExtensions!,
pagesPaths
)
: null
const deferredBuildPaths =
hasDeferredEntries && NextBuildContext.appDir
? getDeferredBuildPaths(deferredEntries, pagesPaths, metadataAppPaths)
: null
const persistentCaching = isFileSystemCacheEnabledForBuild(config)
const rootPath = config.turbopack?.root || config.outputFileTracingRoot || dir
......
import { connection } from 'next/server'
import { Suspense } from 'react'
async function DynamicCurrentTime() {
await connection()
return <p id="current-time">{Date.now()}</p>
}
export default function CurrentTimePage() {
return (
<div>
<h1>Current Time</h1>
<Suspense fallback={null}>
<DynamicCurrentTime />
</Suspense>
</div>
)
}
const CALLBACK_TIMESTAMP = 0
export default function DeferredPage() {
return <h1>Deferred Page</h1>
return (
<div>
<h1>Deferred Page</h1>
<p id="deferred-callback-timestamp">{CALLBACK_TIMESTAMP}</p>
</div>
)
}
{
"name": "Deferred Entries Metadata",
"short_name": "DeferredMeta",
"description": "Metadata manifest for deferred entries e2e",
"start_url": "/"
}
User-Agent: *
Allow: /
Disallow: /private/
Sitemap: https://example.com/sitemap.xml
import { NextResponse } from 'next/server'
export function GET() {
return NextResponse.json({ message: 'Hello from app route handler' })
}
import type { MetadataRoute } from 'next'
export default function sitemap(): MetadataRoute.Sitemap {
return [
{
url: 'https://example.com/deferred-entries',
lastModified: new Date('2024-01-01T00:00:00.000Z'),
},
]
}
export function generateStaticParams() {
return [{ slug: 'alpha' }, { slug: 'beta' }]
}
type StaticParamsPageProps = {
params: Promise<{ slug: string }>
}
export default async function StaticParamsPage({
params,
}: StaticParamsPageProps) {
const { slug } = await Promise.resolve(params)
return <h1>Generated Static Param: {slug}</h1>
}
......@@ -3,6 +3,10 @@ const path = require('path')
const logFile = path.join(__dirname, '.entry-log')
const callbackLogFile = path.join(__dirname, '.callback-log')
const deferredPageFile = path.join(__dirname, 'app', 'deferred', 'page.tsx')
const homePageFile = path.join(__dirname, 'app', 'page.tsx')
let lastHomePageContent = null
/** @type {import('next').NextConfig} */
module.exports = {
......@@ -10,10 +14,34 @@ module.exports = {
deferredEntries: ['/deferred'],
onBeforeDeferredEntries: async () => {
const timestamp = Date.now()
// Write the callback log file - this file existing proves callback was called
fs.writeFileSync(callbackLogFile, `callback:${timestamp}\n`)
const homePageContent = fs.readFileSync(homePageFile, 'utf-8')
const shouldWriteDeferredTimestamp =
lastHomePageContent === null || lastHomePageContent !== homePageContent
// Mutate the deferred entry source directly so the deferred build picks
// up callback-time content.
if (shouldWriteDeferredTimestamp) {
const deferredPageContent = fs.readFileSync(deferredPageFile, 'utf-8')
const nextDeferredPageContent = deferredPageContent.replace(
/const CALLBACK_TIMESTAMP = \d+/,
`const CALLBACK_TIMESTAMP = ${timestamp}`
)
if (nextDeferredPageContent === deferredPageContent) {
throw new Error(
'Failed to update CALLBACK_TIMESTAMP in deferred page entry'
)
}
fs.writeFileSync(deferredPageFile, nextDeferredPageContent)
// Persist only write-triggering callback timestamps.
// This avoids deferred self-rebuild callback loops in webpack dev.
fs.writeFileSync(callbackLogFile, `callback:${timestamp}\n`)
}
lastHomePageContent = homePageContent
console.log(
`[TEST] onBeforeDeferredEntries callback executed at ${timestamp}`
`[TEST] onBeforeDeferredEntries callback executed at ${timestamp} (write=${shouldWriteDeferredTimestamp})`
)
// Small delay to ensure we can verify timing
......
export default function NoDataPage() {
return <h1>Pages No Data Fetching</h1>
}
type ServerSidePropsPageProps = {
timestamp: number
}
export function getServerSideProps() {
return {
props: {
timestamp: Date.now(),
},
}
}
export default function ServerSidePropsPage({
timestamp,
}: ServerSidePropsPageProps) {
return (
<div>
<h1>Pages getServerSideProps</h1>
<p id="server-side-timestamp">{timestamp}</p>
</div>
)
}
type StaticPathPageProps = {
slug: string
}
export function getStaticPaths() {
return {
paths: [{ params: { slug: 'alpha' } }, { params: { slug: 'beta' } }],
fallback: false,
}
}
export function getStaticProps({ params }: { params: { slug: string } }) {
return {
props: {
slug: params.slug,
},
}
}
export default function StaticPathPage({ slug }: StaticPathPageProps) {
return <h1>Pages getStaticPaths + getStaticProps: {slug}</h1>
}
type StaticPropsSecondaryPageProps = {
message: string
}
export function getStaticProps() {
return {
props: {
message: 'Pages getStaticProps Secondary',
},
}
}
export default function StaticPropsSecondaryPage({
message,
}: StaticPropsSecondaryPageProps) {
return <h1>{message}</h1>
}
type StaticPropsPageProps = {
message: string
}
export function getStaticProps() {
return {
props: {
message: 'Pages getStaticProps Primary',
},
}
}
export default function StaticPropsPage({ message }: StaticPropsPageProps) {
return <h1>{message}</h1>
}
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