From 4bb213260033fe4452b1835440d9aeaf19b638fb Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Mon, 21 Nov 2022 16:34:11 +0100 Subject: [PATCH] Alias esm next document to avoid mismatch react context --- packages/next/build/webpack-config.ts | 11 ++++++--- test/e2e/streaming-ssr/index.test.ts | 32 +++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/packages/next/build/webpack-config.ts b/packages/next/build/webpack-config.ts index 2e7bebaaf90df..6cf00c36877ea 100644 --- a/packages/next/build/webpack-config.ts +++ b/packages/next/build/webpack-config.ts @@ -828,6 +828,7 @@ export default async function getBaseWebpackConfig( const customRootAliases: { [key: string]: string[] } = {} if (dev) { + const nextDist = 'next/dist/' + (isEdgeServer ? 'esm/' : '') customAppAliases[`${PAGES_DIR_ALIAS}/_app`] = [ ...(pagesDir ? pageExtensions.reduce((prev, ext) => { @@ -835,7 +836,7 @@ export default async function getBaseWebpackConfig( return prev }, [] as string[]) : []), - 'next/dist/pages/_app.js', + `${nextDist}pages/_app.js`, ] customAppAliases[`${PAGES_DIR_ALIAS}/_error`] = [ ...(pagesDir @@ -844,7 +845,7 @@ export default async function getBaseWebpackConfig( return prev }, [] as string[]) : []), - 'next/dist/pages/_error.js', + `${nextDist}pages/_error.js`, ] customDocumentAliases[`${PAGES_DIR_ALIAS}/_document`] = [ ...(pagesDir @@ -853,7 +854,7 @@ export default async function getBaseWebpackConfig( return prev }, [] as string[]) : []), - 'next/dist/pages/_document.js', + `${nextDist}pages/_document.js`, ] } @@ -905,6 +906,10 @@ export default async function getBaseWebpackConfig( 'next/dist/esm/shared/lib/head', [require.resolve('next/dist/shared/lib/dynamic')]: 'next/dist/esm/shared/lib/dynamic', + [require.resolve('next/dist/pages/_document')]: + 'next/dist/esm/pages/_document', + [require.resolve('next/dist/pages/_app')]: + 'next/dist/esm/pages/_app', } : undefined), diff --git a/test/e2e/streaming-ssr/index.test.ts b/test/e2e/streaming-ssr/index.test.ts index 958bec7e0a5c9..9f500fa2a17c5 100644 --- a/test/e2e/streaming-ssr/index.test.ts +++ b/test/e2e/streaming-ssr/index.test.ts @@ -2,6 +2,7 @@ import { join } from 'path' import { createNext, FileRef } from 'e2e-utils' import { NextInstance } from 'test/lib/next-modes/base' import { + check, fetchViaHTTP, findPort, initNextServerScript, @@ -16,7 +17,7 @@ const react18Deps = { const isNextProd = !(global as any).isNextDev && !(global as any).isNextDeploy -describe('react 18 streaming SSR with custom next configs', () => { +describe('streaming SSR with custom next configs', () => { let next: NextInstance beforeAll(async () => { @@ -74,10 +75,37 @@ describe('react 18 streaming SSR with custom next configs', () => { const html = await renderViaHTTP(next.url, '/multi-byte') expect(html).toContain('マルチバイト'.repeat(28)) }) + + if ((global as any).isNextDev) { + it('should work with custom document', async () => { + await next.patchFile( + 'pages/_document.js', + ` + import { Html, Head, Main, NextScript } from 'next/document' + + export default function Document() { + return ( + + + +
+ + + + ) + } + ` + ) + await check(async () => { + return await renderViaHTTP(next.url, '/') + }, /index/) + await next.deleteFile('pages/_document.js') + }) + } }) if (isNextProd) { - describe('react 18 streaming SSR with custom server', () => { + describe('streaming SSR with custom server', () => { let next let server let appPort