Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alias esm next document to avoid mismatch react context #43192

Merged
merged 3 commits into from Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions packages/next/build/webpack-config.ts
Expand Up @@ -831,14 +831,15 @@ 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) => {
prev.push(path.join(pagesDir, `_app.${ext}`))
return prev
}, [] as string[])
: []),
'next/dist/pages/_app.js',
`${nextDist}pages/_app.js`,
]
customAppAliases[`${PAGES_DIR_ALIAS}/_error`] = [
...(pagesDir
Expand All @@ -847,7 +848,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
Expand All @@ -856,7 +857,7 @@ export default async function getBaseWebpackConfig(
return prev
}, [] as string[])
: []),
'next/dist/pages/_document.js',
`${nextDist}pages/_document.js`,
]
}

Expand Down Expand Up @@ -908,6 +909,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),

Expand Down
32 changes: 30 additions & 2 deletions test/e2e/streaming-ssr/index.test.ts
Expand Up @@ -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,
Expand All @@ -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 () => {
Expand Down Expand Up @@ -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 (
<Html>
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
`
)
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
Expand Down