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
This commit is contained in:
Jiachi Liu 2022-04-21 11:07:03 +02:00 committed by GitHub
parent 9fe2f2637c
commit 2bdf1bc023
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 33 additions and 7 deletions

View file

@ -1,4 +1,7 @@
import { join } from 'path'
import path from '../shared/lib/isomorphic/path'
const { join } = path
export const NEXT_PROJECT_ROOT = join(__dirname, '..', '..')
export const NEXT_PROJECT_ROOT_DIST = join(NEXT_PROJECT_ROOT, 'dist')
export const NEXT_PROJECT_ROOT_NODE_MODULES = join(

View file

@ -1,7 +1,7 @@
import type { CacheFs } from '../shared/lib/utils'
import LRUCache from 'next/dist/compiled/lru-cache'
import path from 'path'
import path from '../shared/lib/isomorphic/path'
import { PrerenderManifest } from '../build'
import { normalizePagePath } from './normalize-page-path'
import { IncrementalCacheValue, IncrementalCacheEntry } from './response-cache'

View file

@ -1,6 +1,8 @@
import { posix } from 'path'
import path from '../shared/lib/isomorphic/path'
import { isDynamicRoute } from '../shared/lib/router/utils'
const { posix } = path
export { normalizePathSep, denormalizePagePath } from './denormalize-page-path'
export function normalizePagePath(page: string): string {

View file

@ -1,4 +1,5 @@
import type { IncomingMessage, ServerResponse } from 'http'
import type { ParsedUrlQuery } from 'querystring'
import type { NextRouter } from '../shared/lib/router/router'
import type { HtmlProps } from '../shared/lib/html-context'
import type { DomainLocale } from './config'
@ -20,7 +21,6 @@ import type { GetServerSideProps, GetStaticProps, PreviewData } from '../types'
import type { UnwrapPromise } from '../lib/coalesced-function'
import React from 'react'
import { ParsedUrlQuery, stringify as stringifyQuery } from 'querystring'
import { createFromReadableStream } from 'next/dist/compiled/react-server-dom-webpack'
import { renderToReadableStream } from 'next/dist/compiled/react-server-dom-webpack/writer.browser.server'
import { StyleRegistry, createStyleRegistry } from 'styled-jsx'
@ -79,6 +79,7 @@ import { ImageConfigContext } from '../shared/lib/image-config-context'
import { FlushEffectsContext } from '../shared/lib/flush-effects'
import { interopDefault } from '../lib/interop-default'
import stripAnsi from 'next/dist/compiled/strip-ansi'
import { urlQueryToSearchParams } from '../shared/lib/router/utils/querystring'
let optimizeAmp: typeof import('./optimize-amp').default
let getFontDefinitionFromManifest: typeof import('./font-utils').getFontDefinitionFromManifest
@ -518,7 +519,7 @@ export async function renderToHTML(
if (isServerComponent) {
serverComponentsInlinedTransformStream = new TransformStream()
const search = stringifyQuery(query)
const search = urlQueryToSearchParams(query).toString()
Component = createServerComponentRenderer(AppMod, ComponentMod, {
cachePrefix: pathname + (search ? `?${search}` : ''),
inlinedTransformStream: serverComponentsInlinedTransformStream,

View file

@ -5,8 +5,9 @@ import type { NextParsedUrlQuery } from './request-meta'
import type { Params } from './router'
import type { PayloadOptions } from './send-payload'
import type { LoadComponentsReturnType } from './load-components'
import type { Options } from './base-server'
import BaseServer, { Options } from './base-server'
import BaseServer from './base-server'
import { renderToHTML } from './render'
import { byteLength, generateETag } from './api-utils/web'
@ -20,9 +21,11 @@ export default class NextWebServer extends BaseServer {
constructor(options: Options & { webServerConfig: WebServerConfig }) {
super(options)
this.webServerConfig = options.webServerConfig
Object.assign(this.renderOpts, options.webServerConfig.extendRenderOpts)
}
protected generateRewrites() {
// @TODO: assuming minimal mode right now
return {

View file

@ -0,0 +1,5 @@
const path = process.browser
? require('next/dist/compiled/path-browserify')
: require('path')
export default path

View file

@ -1,4 +1,4 @@
import { ParsedUrlQuery } from 'querystring'
import type { ParsedUrlQuery } from 'querystring'
export function searchParamsToUrlQuery(
searchParams: URLSearchParams

View file

@ -340,6 +340,11 @@ declare module 'next/dist/compiled/process' {
export = m
}
declare module 'next/dist/compiled/path-browserify' {
import m from 'path'
export = m
}
declare module 'pnp-webpack-plugin' {
import webpack from 'webpack4'

View file

@ -19,3 +19,7 @@ export default function Index() {
</div>
)
}
export const config = {
// runtime: 'edge'
}

View file

@ -21,6 +21,7 @@ import webdriver from 'next-webdriver'
const appDir = join(__dirname, '../app')
const nextConfig = new File(join(appDir, 'next.config.js'))
const invalidPage = new File(join(appDir, 'pages/invalid.js'))
const indexPage = new File(join(appDir, 'pages/index.js'))
describe('Basics', () => {
runTests('default setting with react 18', basics)
@ -67,12 +68,14 @@ function runTestsAgainstRuntime(runtime) {
invalidPage.write(`export const value = 1`)
}
nextConfig.replace("// runtime: 'edge'", `runtime: '${runtime}'`)
indexPage.replace("// runtime: 'edge'", `runtime: '${runtime}'`)
},
afterAll: (env) => {
if (env === 'dev') {
invalidPage.delete()
}
nextConfig.restore()
indexPage.restore()
},
}
)