Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add experimental flag for providing entry paths #67134

Merged
merged 4 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add experimental flag for providing entry paths
  • Loading branch information
ijjk committed Jun 23, 2024
commit c78ef086babd677226730f7f7829230e67cffbd8
41 changes: 26 additions & 15 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -866,14 +866,19 @@ export default async function build(
appDir
)

const pagesPaths =
!appDirOnly && pagesDir
const providedPagePaths: string[] = JSON.parse(
process.env.NEXT_PROVIDED_PAGE_PATHS || '[]'
)

let pagesPaths =
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 +941,24 @@ 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 ||
(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
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)
}
})
})
Loading