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 @@ ...@@ -2,6 +2,11 @@
import { saveCpuProfile } from '../../server/lib/cpu-profile' import { saveCpuProfile } from '../../server/lib/cpu-profile'
import path from 'path' import path from 'path'
import { validateTurboNextConfig } from '../../lib/turbopack-warning' 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 { isFileSystemCacheEnabledForBuild } from '../../shared/lib/turbopack/utils'
import { NextBuildContext } from '../build-context' import { NextBuildContext } from '../build-context'
import { createDefineEnv, getBindingsSync } from '../swc' import { createDefineEnv, getBindingsSync } from '../swc'
...@@ -12,7 +17,6 @@ import { ...@@ -12,7 +17,6 @@ import {
} from '../handle-entrypoints' } from '../handle-entrypoints'
import { TurbopackManifestLoader } from '../../shared/lib/turbopack/manifest-loader' import { TurbopackManifestLoader } from '../../shared/lib/turbopack/manifest-loader'
import { promises as fs } from 'fs' import { promises as fs } from 'fs'
import type { Stats } from 'fs'
import { PHASE_PRODUCTION_BUILD } from '../../shared/lib/constants' import { PHASE_PRODUCTION_BUILD } from '../../shared/lib/constants'
import loadConfig from '../../server/config' import loadConfig from '../../server/config'
import { hasCustomExportOutput } from '../../export/utils' import { hasCustomExportOutput } from '../../export/utils'
...@@ -22,7 +26,11 @@ import { isCI } from '../../server/ci-info' ...@@ -22,7 +26,11 @@ import { isCI } from '../../server/ci-info'
import { backgroundLogCompilationEvents } from '../../shared/lib/turbopack/compilation-events' import { backgroundLogCompilationEvents } from '../../shared/lib/turbopack/compilation-events'
import { getSupportedBrowsers, printBuildErrors } from '../utils' import { getSupportedBrowsers, printBuildErrors } from '../utils'
import { normalizePath } from '../../lib/normalize-path' 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 { createValidFileMatcher } from '../../server/lib/find-page-file'
import type { import type {
ProjectOptions, ProjectOptions,
...@@ -30,6 +38,15 @@ import type { ...@@ -30,6 +38,15 @@ import type {
TurbopackResult, TurbopackResult,
} from '../swc/types' } 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. * Convert deferred entry routes to debugBuildPaths format.
* Routes like '/deferred' become '/deferred/page' for the filter. * Routes like '/deferred' become '/deferred/page' for the filter.
...@@ -37,86 +54,49 @@ import type { ...@@ -37,86 +54,49 @@ import type {
*/ */
function getDeferredBuildPaths( function getDeferredBuildPaths(
deferredEntries: string[], deferredEntries: string[],
pagesPaths: string[] pagesPaths: string[],
metadataAppPaths: string[]
): { ): {
app: string[] app: string[]
pages: string[] pages: string[]
} { } {
return { return {
app: deferredEntries.map((route) => { app: [
const routePath = route.startsWith('/') ? route : `/${route}` ...new Set([
return `${routePath}/page` ...deferredEntries.map((route) => toAppDebugPath(route, 'page')),
}), ...metadataAppPaths,
]),
],
// Include all pages routes so they are not filtered out // Include all pages routes so they are not filtered out
pages: pagesPaths, pages: pagesPaths,
} }
} }
/** /**
* Scan the app directory to find all page routes, then filter out deferred entries. * Collect app entries and filter out deferred app pages only.
* Returns debugBuildPaths for non-deferred routes 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) * @param pagesPaths - All pages routes to include (deferred entries only affects app routes)
*/ */
async function getNonDeferredBuildPaths( function getNonDeferredBuildPaths(
appDir: string | undefined, appPaths: string[],
deferredEntries: string[], deferredEntries: string[],
pageExtensions: string[], pageExtensions: string[],
pagesPaths: string[] pagesPaths: string[]
): Promise<{ app: string[]; pages: string[] } | null> { ): { app: string[]; pages: string[] } | null {
if (!appDir || deferredEntries.length === 0) { if (deferredEntries.length === 0) {
return null return null
} }
const deferredSet = new Set( const deferredPagePaths = new Set(
deferredEntries.map((route) => deferredEntries.map((route) => toAppDebugPath(route, 'page'))
route.startsWith('/') ? route : `/${route}` )
) 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 { return {
app: appRoutes, app: nonDeferredAppPaths,
// Include all pages routes so they are not filtered out // Include all pages routes so they are not filtered out
pages: pagesPaths, pages: pagesPaths,
} }
...@@ -157,6 +137,8 @@ export async function turbopackBuild(): Promise<{ ...@@ -157,6 +137,8 @@ export async function turbopackBuild(): Promise<{
// Collect all pages paths when using deferred entries to ensure pages routes // Collect all pages paths when using deferred entries to ensure pages routes
// are not filtered out (deferred entries only affects app routes) // are not filtered out (deferred entries only affects app routes)
let pagesPaths: string[] = [] let pagesPaths: string[] = []
let appPaths: string[] = []
let metadataAppPaths: string[] = []
if (hasDeferredEntries && NextBuildContext.pagesDir) { if (hasDeferredEntries && NextBuildContext.pagesDir) {
const validFileMatcher = createValidFileMatcher( const validFileMatcher = createValidFileMatcher(
config.pageExtensions!, config.pageExtensions!,
...@@ -167,20 +149,50 @@ export async function turbopackBuild(): Promise<{ ...@@ -167,20 +149,50 @@ export async function turbopackBuild(): Promise<{
validFileMatcher 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 // For deferred entries, we use debugBuildPaths to control which routes are built
// First build excludes deferred entries, second build includes only deferred entries // First build excludes deferred entries, second build includes only deferred entries
const nonDeferredBuildPaths = hasDeferredEntries const nonDeferredBuildPaths =
? await getNonDeferredBuildPaths( hasDeferredEntries && NextBuildContext.appDir
NextBuildContext.appDir, ? getNonDeferredBuildPaths(
deferredEntries, appPaths,
config.pageExtensions!, deferredEntries,
pagesPaths config.pageExtensions!,
) pagesPaths
: null )
const deferredBuildPaths = hasDeferredEntries : null
? getDeferredBuildPaths(deferredEntries, pagesPaths) const deferredBuildPaths =
: null hasDeferredEntries && NextBuildContext.appDir
? getDeferredBuildPaths(deferredEntries, pagesPaths, metadataAppPaths)
: null
const persistentCaching = isFileSystemCacheEnabledForBuild(config) const persistentCaching = isFileSystemCacheEnabledForBuild(config)
const rootPath = config.turbopack?.root || config.outputFileTracingRoot || dir 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() { 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') ...@@ -3,6 +3,10 @@ const path = require('path')
const logFile = path.join(__dirname, '.entry-log') const logFile = path.join(__dirname, '.entry-log')
const callbackLogFile = path.join(__dirname, '.callback-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} */ /** @type {import('next').NextConfig} */
module.exports = { module.exports = {
...@@ -10,10 +14,34 @@ module.exports = { ...@@ -10,10 +14,34 @@ module.exports = {
deferredEntries: ['/deferred'], deferredEntries: ['/deferred'],
onBeforeDeferredEntries: async () => { onBeforeDeferredEntries: async () => {
const timestamp = Date.now() const timestamp = Date.now()
// Write the callback log file - this file existing proves callback was called const homePageContent = fs.readFileSync(homePageFile, 'utf-8')
fs.writeFileSync(callbackLogFile, `callback:${timestamp}\n`) 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( console.log(
`[TEST] onBeforeDeferredEntries callback executed at ${timestamp}` `[TEST] onBeforeDeferredEntries callback executed at ${timestamp} (write=${shouldWriteDeferredTimestamp})`
) )
// Small delay to ensure we can verify timing // 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