From 0e8143d60231212c590dfcd6483b765446576e8a Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Tue, 29 Mar 2022 23:25:30 +0200 Subject: [PATCH] fix file runtime build and flight server loader --- packages/next/build/entries.ts | 12 ++++++------ packages/next/build/webpack-config.ts | 14 +++++++++++--- .../loaders/next-flight-server-loader.ts | 6 ------ .../next-middleware-ssr-loader/index.ts | 19 ++++++++++++++----- packages/next/server/render.tsx | 2 +- .../app/pages/index.server.js | 3 ++- 6 files changed, 34 insertions(+), 22 deletions(-) diff --git a/packages/next/build/entries.ts b/packages/next/build/entries.ts index 629b6c24f3eed09..9945adf1cf6b776 100644 --- a/packages/next/build/entries.ts +++ b/packages/next/build/entries.ts @@ -98,20 +98,20 @@ export function createPagesMapping( // allow falling back to the correct source file so // that HMR can work properly when a file is added/removed if (isDev) { - pages['/_app'] = `${PAGES_DIR_ALIAS}/_app` - pages['/_error'] = `${PAGES_DIR_ALIAS}/_error` - pages['/_document'] = `${PAGES_DIR_ALIAS}/_document` if (hasServerComponents) { pages['/_app.server'] = `${PAGES_DIR_ALIAS}/_app.server` } + pages['/_app'] = `${PAGES_DIR_ALIAS}/_app` + pages['/_error'] = `${PAGES_DIR_ALIAS}/_error` + pages['/_document'] = `${PAGES_DIR_ALIAS}/_document` } else { - pages['/_app'] = pages['/_app'] || 'next/dist/pages/_app' - pages['/_error'] = pages['/_error'] || 'next/dist/pages/_error' - pages['/_document'] = pages['/_document'] || 'next/dist/pages/_document' if (hasServerComponents) { pages['/_app.server'] = pages['/_app.server'] || 'next/dist/pages/_app.server' } + pages['/_app'] = pages['/_app'] || 'next/dist/pages/_app' + pages['/_error'] = pages['/_error'] || 'next/dist/pages/_error' + pages['/_document'] = pages['/_document'] || 'next/dist/pages/_document' } return pages } diff --git a/packages/next/build/webpack-config.ts b/packages/next/build/webpack-config.ts index 6e77b831fb1fa16..798e90866e51e31 100644 --- a/packages/next/build/webpack-config.ts +++ b/packages/next/build/webpack-config.ts @@ -977,6 +977,14 @@ export default async function getBaseWebpackConfig( }, } + const rscCodeCondition = { + test: codeCondition.test, + include: codeCondition.include + ? [...codeCondition.include, /node_modules/] + : /node_modules/, + exclude: () => false, + } + let webpackConfig: webpack.Configuration = { parallelism: Number(process.env.NEXT_WEBPACK_PARALLELISM) || undefined, externals: targetWeb @@ -1199,7 +1207,7 @@ export default async function getBaseWebpackConfig( ? [ // RSC server compilation loaders { - ...codeCondition, + ...rscCodeCondition, use: { loader: 'next-flight-server-loader', options: { @@ -1208,7 +1216,7 @@ export default async function getBaseWebpackConfig( }, }, { - test: codeCondition.test, + ...rscCodeCondition, resourceQuery: /__sc_client__/, use: { loader: 'next-flight-client-loader', @@ -1218,7 +1226,7 @@ export default async function getBaseWebpackConfig( : [ // RSC client compilation loaders { - ...codeCondition, + ...rscCodeCondition, test: serverComponentsRegex, use: { loader: 'next-flight-server-loader', diff --git a/packages/next/build/webpack/loaders/next-flight-server-loader.ts b/packages/next/build/webpack/loaders/next-flight-server-loader.ts index f4ef0f1016ec1f8..0fa20a28f495f3e 100644 --- a/packages/next/build/webpack/loaders/next-flight-server-loader.ts +++ b/packages/next/build/webpack/loaders/next-flight-server-loader.ts @@ -166,12 +166,6 @@ export default async function transformSource( throw new Error('Expected source to have been transformed to a string.') } - // We currently assume that all components are shared components (unsuffixed) - // from node_modules. - if (resourcePath.includes('/node_modules/')) { - return source - } - const rawRawPageExtensions = getRawPageExtensions(pageExtensions) const isServerComponent = createServerComponentFilter(rawRawPageExtensions) const isClientComponent = createClientComponentFilter(rawRawPageExtensions) diff --git a/packages/next/build/webpack/loaders/next-middleware-ssr-loader/index.ts b/packages/next/build/webpack/loaders/next-middleware-ssr-loader/index.ts index a3b57409678ad89..5777445618828d4 100644 --- a/packages/next/build/webpack/loaders/next-middleware-ssr-loader/index.ts +++ b/packages/next/build/webpack/loaders/next-middleware-ssr-loader/index.ts @@ -17,13 +17,17 @@ export default async function middlewareSSRLoader(this: any) { const stringifiedPagePath = stringifyRequest(this, absolutePagePath) const stringifiedAppPath = stringifyRequest(this, absoluteAppPath) - const stringifiedAppServerPath = stringifyRequest(this, absoluteAppServerPath) + const stringifiedAppServerPath = absoluteAppServerPath + ? stringifyRequest(this, absoluteAppServerPath) + : null const stringifiedErrorPath = stringifyRequest(this, absoluteErrorPath) const stringifiedDocumentPath = stringifyRequest(this, absoluteDocumentPath) const stringified500Path = absolute500Path ? stringifyRequest(this, absolute500Path) - : 'null' + : null + + console.log('stringifiedAppServerPath', stringifiedAppServerPath) const transformed = ` import { adapter } from 'next/dist/server/web/adapter' @@ -34,11 +38,16 @@ export default async function middlewareSSRLoader(this: any) { import Document from ${stringifiedDocumentPath} const appMod = require(${stringifiedAppPath}) - const appServerMod = require(${stringifiedAppServerPath}) + const appServerMod = ${ + stringifiedAppServerPath ? `require(${stringifiedAppServerPath})` : 'null' + } const pageMod = require(${stringifiedPagePath}) const errorMod = require(${stringifiedErrorPath}) - const error500Mod = ${stringified500Path} ? require(${stringified500Path}) : null + const error500Mod = ${ + stringified500Path ? `require(${stringified500Path})` : 'null' + } + const buildManifest = self.__BUILD_MANIFEST const reactLoadableManifest = self.__REACT_LOADABLE_MANIFEST const rscManifest = self.__RSC_MANIFEST @@ -60,7 +69,7 @@ export default async function middlewareSSRLoader(this: any) { buildManifest, reactLoadableManifest, serverComponentManifest: ${isServerComponent} ? rscManifest : null, - appServerMod: appServerMod, + appServerMod, isServerComponent: ${isServerComponent}, config: ${stringifiedConfig}, buildId: ${JSON.stringify(buildId)}, diff --git a/packages/next/server/render.tsx b/packages/next/server/render.tsx index 3994efd481bb753..20702bf46076427 100644 --- a/packages/next/server/render.tsx +++ b/packages/next/server/render.tsx @@ -1376,7 +1376,7 @@ export async function renderToHTML( ) : ( - {isServerComponent && AppServerMod.__next_rsc__ ? ( + {isServerComponent && !!AppMod.__next_rsc__ ? ( // _app.server.js is used. ) : ( diff --git a/test/integration/react-streaming-and-server-components/app/pages/index.server.js b/test/integration/react-streaming-and-server-components/app/pages/index.server.js index 7ca7e5e30855df4..6e9a7bf17751200 100644 --- a/test/integration/react-streaming-and-server-components/app/pages/index.server.js +++ b/test/integration/react-streaming-and-server-components/app/pages/index.server.js @@ -14,7 +14,7 @@ export default function Index({ header, router }) { ) } - +/* export function getServerSideProps({ req }) { const { headers } = req const header = headers[headerKey] || '' @@ -25,3 +25,4 @@ export function getServerSideProps({ req }) { }, } } +*/