Skip to content

Commit 77c4546

Browse files
ijjkhuozhi
andauthored
Ensure custom relative distDir resolves properly (#80569)
This ensures we properly load our manifest inside the handler interface when a custom relative `distDir` is being used which is the case with NX specifically. --------- Co-authored-by: Jiachi Liu <[email protected]>
1 parent bc385a0 commit 77c4546

26 files changed

+1362
-1
lines changed

packages/next/src/server/base-server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2642,6 +2642,7 @@ export default abstract class Server<
26422642
// propagate the request context for dev
26432643
setRequestMeta(request, getRequestMeta(req))
26442644
addRequestMeta(request, 'projectDir', this.dir)
2645+
addRequestMeta(request, 'distDir', this.distDir)
26452646
addRequestMeta(request, 'isIsrFallback', pagesFallback)
26462647
addRequestMeta(request, 'query', query)
26472648
addRequestMeta(request, 'params', opts.params)

packages/next/src/server/next-server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,7 @@ export default class NextNodeServer extends BaseServer<
667667
) => Promise<void>
668668
}
669669
addRequestMeta(req.originalRequest, 'projectDir', this.dir)
670+
addRequestMeta(req.originalRequest, 'distDir', this.distDir)
670671
await module.handler(req.originalRequest, res.originalResponse, {
671672
waitUntil: this.getWaitUntil(),
672673
})

packages/next/src/server/request-meta.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ export interface RequestMeta {
201201
*/
202202
projectDir?: string
203203

204+
/**
205+
* The dist directory the server is currently using
206+
*/
207+
distDir?: string
208+
204209
/**
205210
* Whether we are generating the fallback version of the page in dev mode
206211
*/

packages/next/src/server/route-modules/route-module.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,11 +425,17 @@ export abstract class RouteModule<
425425
// if we want to share the normalizing logic here
426426
// we will need to allow passing in the i18n and similar info
427427
if (process.env.NEXT_RUNTIME !== 'edge') {
428-
const { join } = require('node:path') as typeof import('node:path')
428+
const { join, relative } =
429+
require('node:path') as typeof import('node:path')
429430
const projectDir =
430431
getRequestMeta(req, 'projectDir') ||
431432
join(process.cwd(), this.projectDir)
432433

434+
const absoluteDistDir = getRequestMeta(req, 'distDir')
435+
436+
if (absoluteDistDir) {
437+
this.distDir = relative(projectDir, absoluteDistDir)
438+
}
433439
const { ensureInstrumentationRegistered } = await import(
434440
'../lib/router-utils/instrumentation-globals.external'
435441
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Editor configuration, see http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
indent_style = space
7+
indent_size = 2
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
max_line_length = off
13+
trim_trailing_whitespace = false
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# See https://docs.github.com/en/get-started/getting-started-with-git/ignoring-files for more about ignoring files.
2+
3+
# compiled output
4+
dist
5+
tmp
6+
out-tsc
7+
8+
# dependencies
9+
node_modules
10+
11+
# IDEs and editors
12+
/.idea
13+
.project
14+
.classpath
15+
.c9/
16+
*.launch
17+
.settings/
18+
*.sublime-workspace
19+
20+
# IDE - VSCode
21+
.vscode/*
22+
!.vscode/settings.json
23+
!.vscode/tasks.json
24+
!.vscode/launch.json
25+
!.vscode/extensions.json
26+
27+
# misc
28+
/.sass-cache
29+
/connect.lock
30+
/coverage
31+
/libpeerconnection.log
32+
npm-debug.log
33+
yarn-error.log
34+
testem.log
35+
/typings
36+
37+
# System Files
38+
.DS_Store
39+
Thumbs.db
40+
41+
.nx/cache
42+
.nx/workspace-data
43+
.cursor/rules/nx-rules.mdc
44+
.github/instructions/nx.instructions.md
45+
46+
# Next.js
47+
.next
48+
out
49+
.nx
50+
.vscode

test/e2e/app-dir/nx-handling/.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
legacy-peer-deps=true
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"jsc": {
3+
"target": "es2017",
4+
"parser": {
5+
"syntax": "typescript",
6+
"decorators": true,
7+
"dynamicImport": true
8+
},
9+
"transform": {
10+
"decoratorMetadata": true,
11+
"legacyDecorator": true
12+
},
13+
"keepClassNames": true,
14+
"externalHelpers": true,
15+
"loose": true
16+
},
17+
"module": {
18+
"type": "commonjs"
19+
},
20+
"sourceMaps": true,
21+
"exclude": [
22+
"jest.config.ts",
23+
".*\\.spec.tsx?$",
24+
".*\\.test.tsx?$",
25+
"./src/jest-setup.ts$",
26+
"./**/jest-setup.ts$",
27+
".*.js$",
28+
".*.d.ts$"
29+
]
30+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export async function GET(request: Request) {
2+
return new Response('Hello, from API!')
3+
}

0 commit comments

Comments
 (0)