Skip to content

Commit

Permalink
Add experimental flag for providing entry paths (#67134)
Browse files Browse the repository at this point in the history
Adds a flag for experimentally building on specifically provided entry
paths. This should not be relied on as it is NOT a public facing API.
  • Loading branch information
ijjk committed Jun 23, 2024
1 parent b68caaf commit 16cf88e
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 21 deletions.
53 changes: 33 additions & 20 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -866,14 +866,20 @@ export default async function build(
appDir
)

const pagesPaths =
!appDirOnly && pagesDir
? await nextBuildSpan.traceChild('collect-pages').traceAsyncFn(() =>
recursiveReadDir(pagesDir, {
pathnameFilter: validFileMatcher.isPageFile,
})
)
: []
const providedPagePaths: string[] = JSON.parse(
process.env.NEXT_PROVIDED_PAGE_PATHS || '[]'
)

let pagesPaths =
providedPagePaths.length > 0
? providedPagePaths
: !appDirOnly && pagesDir
? await nextBuildSpan.traceChild('collect-pages').traceAsyncFn(() =>
recursiveReadDir(pagesDir, {
pathnameFilter: validFileMatcher.isPageFile,
})
)
: []

const middlewareDetectionRegExp = new RegExp(
`^${MIDDLEWARE_FILENAME}\\.(?:${config.pageExtensions.join('|')})$`
Expand Down Expand Up @@ -936,18 +942,25 @@ export default async function build(
let denormalizedAppPages: string[] | undefined

if (appDir) {
const appPaths = await nextBuildSpan
.traceChild('collect-app-paths')
.traceAsyncFn(() =>
recursiveReadDir(appDir, {
pathnameFilter: (absolutePath) =>
validFileMatcher.isAppRouterPage(absolutePath) ||
// For now we only collect the root /not-found page in the app
// directory as the 404 fallback
validFileMatcher.isRootNotFound(absolutePath),
ignorePartFilter: (part) => part.startsWith('_'),
})
)
const providedAppPaths: string[] = JSON.parse(
process.env.NEXT_PROVIDED_APP_PATHS || '[]'
)

let appPaths =
providedAppPaths.length > 0
? providedAppPaths
: await nextBuildSpan
.traceChild('collect-app-paths')
.traceAsyncFn(() =>
recursiveReadDir(appDir, {
pathnameFilter: (absolutePath) =>
validFileMatcher.isAppRouterPage(absolutePath) ||
// For now we only collect the root /not-found page in the app
// directory as the 404 fallback
validFileMatcher.isRootNotFound(absolutePath),
ignorePartFilter: (part) => part.startsWith('_'),
})
)

mappedAppPages = await nextBuildSpan
.traceChild('create-app-mapping')
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ export const defaultConfig: NextConfig = {
output: !!process.env.NEXT_PRIVATE_STANDALONE ? 'standalone' : undefined,
modularizeImports: undefined,
experimental: {
flyingShuttle: false,
flyingShuttle: Boolean(process.env.NEXT_PRIVATE_FLYING_SHUTTLE),
prerenderEarlyExit: true,
serverMinification: true,
serverSourceMaps: false,
Expand Down
53 changes: 53 additions & 0 deletions test/e2e/app-dir/app/provide-paths.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { nextTestSetup } from 'e2e-utils'
import glob from 'glob'
import path from 'path'

describe('Provided page/app paths', () => {
const { next, isNextDev } = nextTestSetup({
files: __dirname,
dependencies: {
nanoid: '4.0.1',
},
env: {
NEXT_PROVIDED_PAGE_PATHS: JSON.stringify(['/index.js', '/ssg.js']),
NEXT_PROVIDED_APP_PATHS: JSON.stringify([
'/dashboard/page.js',
'/(newroot)/dashboard/another/page.js',
]),
},
})

if (isNextDev) {
it('should skip dev', () => {})
return
}

it('should only build the provided paths', async () => {
const appPaths = await glob.sync('**/*.js', {
cwd: path.join(next.testDir, '.next/server/app'),
})
const pagePaths = await glob.sync('**/*.js', {
cwd: path.join(next.testDir, '.next/server/pages'),
})

expect(appPaths).toEqual([
'_not-found/page_client-reference-manifest.js',
'_not-found/page.js',
'(newroot)/dashboard/another/page_client-reference-manifest.js',
'(newroot)/dashboard/another/page.js',
'dashboard/page_client-reference-manifest.js',
'dashboard/page.js',
])
expect(pagePaths).toEqual([
'_app.js',
'_document.js',
'_error.js',
'ssg.js',
])

for (const pathname of ['/', '/ssg', '/dashboard', '/dashboard/another']) {
const res = await next.fetch(pathname)
expect(res.status).toBe(200)
}
})
})
5 changes: 5 additions & 0 deletions test/turbopack-build-tests-manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
{
"version": 2,
"suites": {
"test/e2e/app-dir/app/provide-paths.test.ts": {
"passed": [],
"pending": [],
"failed": ["Provided page/app paths should only build the provided paths"]
},
"test/e2e/404-page-router/index.test.ts": {
"passed": [
"404-page-router 404-page-router with basePath of false and i18n of false and middleware false for /error should have the correct router parameters after it is ready",
Expand Down

0 comments on commit 16cf88e

Please sign in to comment.