Unverified Commit 07410160 authored by Jimmy Lai's avatar Jimmy Lai Committed by GitHub

parallel routes: fix next-provided default.js not being resolved if the config…

parallel routes: fix next-provided default.js not being resolved if the config doesn't specify .js (#48446)

This PR fixes an edge case when the next config has `pageExtensions`
specified. When resolving the default parallel route, we use that
config. If it does not specify `.js` then it wouldn't be able to resolve
the default from Next.js

## For Contributors

### Improving Documentation or adding/fixing Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md



## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->

fix #48431
link NEXT-1021
parent 2eb821ad
...@@ -51,7 +51,8 @@ const PAGE_SEGMENT = 'page$' ...@@ -51,7 +51,8 @@ const PAGE_SEGMENT = 'page$'
type PathResolver = ( type PathResolver = (
pathname: string, pathname: string,
resolveDir?: boolean resolveDir?: boolean,
internal?: boolean
) => Promise<string | undefined> ) => Promise<string | undefined>
export type ComponentsType = { export type ComponentsType = {
...@@ -171,10 +172,7 @@ async function createTreeCodeFromPath( ...@@ -171,10 +172,7 @@ async function createTreeCodeFromPath(
loaderContext, loaderContext,
pageExtensions, pageExtensions,
}: { }: {
resolver: ( resolver: PathResolver
pathname: string,
resolveDir?: boolean
) => Promise<string | undefined>
resolvePath: (pathname: string) => Promise<string> resolvePath: (pathname: string) => Promise<string>
resolveParallelSegments: ( resolveParallelSegments: (
pathname: string pathname: string
...@@ -365,7 +363,11 @@ async function createTreeCodeFromPath( ...@@ -365,7 +363,11 @@ async function createTreeCodeFromPath(
(await resolver( (await resolver(
`${appDirPrefix}${segmentPath}/${actualSegment}/default` `${appDirPrefix}${segmentPath}/${actualSegment}/default`
)) ?? )) ??
(await resolver(`next/dist/client/components/parallel-route-default`)) (await resolver(
`next/dist/client/components/parallel-route-default`,
false,
true
))
props[normalizeParallelKey(adjacentParallelSegment)] = `[ props[normalizeParallelKey(adjacentParallelSegment)] = `[
'__DEFAULT__', '__DEFAULT__',
...@@ -437,6 +439,13 @@ const nextAppLoader: AppLoader = async function nextAppLoader() { ...@@ -437,6 +439,13 @@ const nextAppLoader: AppLoader = async function nextAppLoader() {
const resolve = this.getResolve(resolveOptions) const resolve = this.getResolve(resolveOptions)
// a resolver for internal next files. We need to override the extensions, in case
// a project doesn't have the same ones as used by next.
const internalResolve = this.getResolve({
...resolveOptions,
extensions: [...extensions, '.js', '.jsx', '.ts', '.tsx'],
})
const normalizedAppPaths = const normalizedAppPaths =
typeof appPaths === 'string' ? [appPaths] : appPaths || [] typeof appPaths === 'string' ? [appPaths] : appPaths || []
...@@ -471,13 +480,16 @@ const nextAppLoader: AppLoader = async function nextAppLoader() { ...@@ -471,13 +480,16 @@ const nextAppLoader: AppLoader = async function nextAppLoader() {
return Object.entries(matched) return Object.entries(matched)
} }
const resolver: PathResolver = async (pathname, resolveDir) => { const resolver: PathResolver = async (pathname, resolveDir, internal) => {
if (resolveDir) { if (resolveDir) {
return createAbsolutePath(appDir, pathname) return createAbsolutePath(appDir, pathname)
} }
try { try {
const resolved = await resolve(this.rootContext, pathname) const resolved = await (internal ? internalResolve : resolve)(
this.rootContext,
pathname
)
this.addDependency(resolved) this.addDependency(resolved)
return resolved return resolved
} catch (err: any) { } catch (err: any) {
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
* @type {import('next').NextConfig} * @type {import('next').NextConfig}
*/ */
const nextConfig = { const nextConfig = {
pageExtensions: ['tsx', 'ts'],
experimental: { appDir: true }, experimental: { appDir: true },
} }
......
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