Skip to content

Commit 2bdf1bc

Browse files
authored
Use fallbackable path module for node and edge runtime (#36306)
x-ref: #36190 x-ref: #31506 * Move nodejs ptah module usage to next-server, keep base-server and web-server headless for `'path'` * Use a native module `path` for nodejs runtime and `path` polyfill for edge runtime
1 parent 9fe2f26 commit 2bdf1bc

File tree

10 files changed

+33
-7
lines changed

10 files changed

+33
-7
lines changed

packages/next/lib/constants.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { join } from 'path'
1+
import path from '../shared/lib/isomorphic/path'
2+
3+
const { join } = path
4+
25
export const NEXT_PROJECT_ROOT = join(__dirname, '..', '..')
36
export const NEXT_PROJECT_ROOT_DIST = join(NEXT_PROJECT_ROOT, 'dist')
47
export const NEXT_PROJECT_ROOT_NODE_MODULES = join(

packages/next/server/incremental-cache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { CacheFs } from '../shared/lib/utils'
22

33
import LRUCache from 'next/dist/compiled/lru-cache'
4-
import path from 'path'
4+
import path from '../shared/lib/isomorphic/path'
55
import { PrerenderManifest } from '../build'
66
import { normalizePagePath } from './normalize-page-path'
77
import { IncrementalCacheValue, IncrementalCacheEntry } from './response-cache'

packages/next/server/normalize-page-path.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { posix } from 'path'
1+
import path from '../shared/lib/isomorphic/path'
22
import { isDynamicRoute } from '../shared/lib/router/utils'
33

4+
const { posix } = path
5+
46
export { normalizePathSep, denormalizePagePath } from './denormalize-page-path'
57

68
export function normalizePagePath(page: string): string {

packages/next/server/render.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { IncomingMessage, ServerResponse } from 'http'
2+
import type { ParsedUrlQuery } from 'querystring'
23
import type { NextRouter } from '../shared/lib/router/router'
34
import type { HtmlProps } from '../shared/lib/html-context'
45
import type { DomainLocale } from './config'
@@ -20,7 +21,6 @@ import type { GetServerSideProps, GetStaticProps, PreviewData } from '../types'
2021
import type { UnwrapPromise } from '../lib/coalesced-function'
2122

2223
import React from 'react'
23-
import { ParsedUrlQuery, stringify as stringifyQuery } from 'querystring'
2424
import { createFromReadableStream } from 'next/dist/compiled/react-server-dom-webpack'
2525
import { renderToReadableStream } from 'next/dist/compiled/react-server-dom-webpack/writer.browser.server'
2626
import { StyleRegistry, createStyleRegistry } from 'styled-jsx'
@@ -79,6 +79,7 @@ import { ImageConfigContext } from '../shared/lib/image-config-context'
7979
import { FlushEffectsContext } from '../shared/lib/flush-effects'
8080
import { interopDefault } from '../lib/interop-default'
8181
import stripAnsi from 'next/dist/compiled/strip-ansi'
82+
import { urlQueryToSearchParams } from '../shared/lib/router/utils/querystring'
8283

8384
let optimizeAmp: typeof import('./optimize-amp').default
8485
let getFontDefinitionFromManifest: typeof import('./font-utils').getFontDefinitionFromManifest
@@ -518,7 +519,7 @@ export async function renderToHTML(
518519

519520
if (isServerComponent) {
520521
serverComponentsInlinedTransformStream = new TransformStream()
521-
const search = stringifyQuery(query)
522+
const search = urlQueryToSearchParams(query).toString()
522523
Component = createServerComponentRenderer(AppMod, ComponentMod, {
523524
cachePrefix: pathname + (search ? `?${search}` : ''),
524525
inlinedTransformStream: serverComponentsInlinedTransformStream,

packages/next/server/web-server.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import type { NextParsedUrlQuery } from './request-meta'
55
import type { Params } from './router'
66
import type { PayloadOptions } from './send-payload'
77
import type { LoadComponentsReturnType } from './load-components'
8+
import type { Options } from './base-server'
89

9-
import BaseServer, { Options } from './base-server'
10+
import BaseServer from './base-server'
1011
import { renderToHTML } from './render'
1112
import { byteLength, generateETag } from './api-utils/web'
1213

@@ -20,9 +21,11 @@ export default class NextWebServer extends BaseServer {
2021

2122
constructor(options: Options & { webServerConfig: WebServerConfig }) {
2223
super(options)
24+
2325
this.webServerConfig = options.webServerConfig
2426
Object.assign(this.renderOpts, options.webServerConfig.extendRenderOpts)
2527
}
28+
2629
protected generateRewrites() {
2730
// @TODO: assuming minimal mode right now
2831
return {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const path = process.browser
2+
? require('next/dist/compiled/path-browserify')
3+
: require('path')
4+
5+
export default path

packages/next/shared/lib/router/utils/querystring.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ParsedUrlQuery } from 'querystring'
1+
import type { ParsedUrlQuery } from 'querystring'
22

33
export function searchParamsToUrlQuery(
44
searchParams: URLSearchParams

packages/next/types/misc.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,11 @@ declare module 'next/dist/compiled/process' {
340340
export = m
341341
}
342342

343+
declare module 'next/dist/compiled/path-browserify' {
344+
import m from 'path'
345+
export = m
346+
}
347+
343348
declare module 'pnp-webpack-plugin' {
344349
import webpack from 'webpack4'
345350

test/integration/react-18/app/pages/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ export default function Index() {
1919
</div>
2020
)
2121
}
22+
23+
export const config = {
24+
// runtime: 'edge'
25+
}

test/integration/react-18/test/index.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import webdriver from 'next-webdriver'
2121
const appDir = join(__dirname, '../app')
2222
const nextConfig = new File(join(appDir, 'next.config.js'))
2323
const invalidPage = new File(join(appDir, 'pages/invalid.js'))
24+
const indexPage = new File(join(appDir, 'pages/index.js'))
2425

2526
describe('Basics', () => {
2627
runTests('default setting with react 18', basics)
@@ -67,12 +68,14 @@ function runTestsAgainstRuntime(runtime) {
6768
invalidPage.write(`export const value = 1`)
6869
}
6970
nextConfig.replace("// runtime: 'edge'", `runtime: '${runtime}'`)
71+
indexPage.replace("// runtime: 'edge'", `runtime: '${runtime}'`)
7072
},
7173
afterAll: (env) => {
7274
if (env === 'dev') {
7375
invalidPage.delete()
7476
}
7577
nextConfig.restore()
78+
indexPage.restore()
7679
},
7780
}
7881
)

0 commit comments

Comments
 (0)