Unverified Commit 00836ad9 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

rsc: bundle legacy head as client component (#43425)

Related to the build failure from #41931 

We need to bundle `next/head` so that it can appear in the
flight-client-manifest, so we changed the behavior to not treat it as
non externals.

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)
parent b304a183
...@@ -111,7 +111,7 @@ impl<C: Comments> ReactServerComponents<C> { ...@@ -111,7 +111,7 @@ impl<C: Comments> ReactServerComponents<C> {
return false; return false;
} }
} }
// Match `ParenthesisExpression` which is some formartting tools // Match `ParenthesisExpression` which is some formatting tools
// usually do: ('use client'). In these case we need to throw // usually do: ('use client'). In these case we need to throw
// an exception because they are not valid directives. // an exception because they are not valid directives.
Expr::Paren(ParenExpr { expr, .. }) => { Expr::Paren(ParenExpr { expr, .. }) => {
......
...@@ -1151,15 +1151,16 @@ export default async function getBaseWebpackConfig( ...@@ -1151,15 +1151,16 @@ export default async function getBaseWebpackConfig(
const isLocalCallback = (localRes: string) => { const isLocalCallback = (localRes: string) => {
// Makes sure dist/shared and dist/server are not bundled // Makes sure dist/shared and dist/server are not bundled
// we need to process shared `router/router` and `dynamic`, // we need to process shared `router/router`, `head` and `dynamic`,
// so that the DefinePlugin can inject process.env values // so that the DefinePlugin can inject process.env values.
// Treat next internals as non-external for server layer
if (layer === WEBPACK_LAYERS.server) return
const isNextExternal = const isNextExternal =
// Treat next internals as non-external for server layer /next[/\\]dist[/\\](esm[\\/])?(shared|server)[/\\](?!lib[/\\](router[/\\]router|dynamic|head[^-]))/.test(
layer === WEBPACK_LAYERS.server localRes
? false )
: /next[/\\]dist[/\\](esm[\\/])?(shared|server)[/\\](?!lib[/\\](router[/\\]router|dynamic))/.test(
localRes
)
if (isNextExternal) { if (isNextExternal) {
// Generate Next.js external import // Generate Next.js external import
...@@ -1176,10 +1177,6 @@ export default async function getBaseWebpackConfig( ...@@ -1176,10 +1177,6 @@ export default async function getBaseWebpackConfig(
.replace(/\\/g, '/') .replace(/\\/g, '/')
) )
return `commonjs ${externalRequest}` return `commonjs ${externalRequest}`
} else if (layer !== WEBPACK_LAYERS.client) {
// We don't want to retry local requests
// with other preferEsm options
return
} }
} }
......
import Link from 'next/link' import Link from 'next/link'
import Head from 'next/head'
export default function Page() { export default function Page() {
return ( return (
<div> <div>
<div> <div>
{/* NOTE: next/head will not work in RSC for now but not break either */}
<Head>
<title>internal-title</title>
</Head>
<Link href="/internal/test/rewrite" id="navigate-rewrite"> <Link href="/internal/test/rewrite" id="navigate-rewrite">
Navigate Rewrite Navigate Rewrite
</Link> </Link>
......
...@@ -113,6 +113,11 @@ describe('app dir head', () => { ...@@ -113,6 +113,11 @@ describe('app dir head', () => {
.waitForElementByCss('#layout', 2000) .waitForElementByCss('#layout', 2000)
expect(await getTitle()).toBe('hello from dynamic blog page post-1') expect(await getTitle()).toBe('hello from dynamic blog page post-1')
}) })
it('should treat next/head as client components but not apply', async () => {
const html = await renderViaHTTP(next.url, '/next-head')
expect(html).not.toMatch(/<title>legacy-head<\/title>/)
})
} }
runTests() runTests()
......
import Head from 'next/head'
export default function page() {
return (
<>
<Head>
<title>legacy-head</title>
</Head>
<p>page</p>
</>
)
}
...@@ -10,9 +10,19 @@ describe('externalize next/dist/shared', () => { ...@@ -10,9 +10,19 @@ describe('externalize next/dist/shared', () => {
await nextBuild(appDir) await nextBuild(appDir)
}) })
it('Does not bundle next/dist/shared/lib/head.js in _error', async () => { it('Bundle next/dist/shared/lib/head.js but not next/dist/shared/lib/head-manager-context.js in _error', async () => {
const content = readNextBuildServerPageFile(appDir, '/_error') const content = readNextBuildServerPageFile(appDir, '/_error')
expect(content).toMatch( expect(content).toMatch(
new RegExp(
'^' +
escapeStringRegexp(
`module.exports = require("next/dist/shared/lib/head-manager-context.js");`
) +
';?$',
'm'
)
)
expect(content).not.toMatch(
new RegExp( new RegExp(
'^' + '^' +
escapeStringRegexp( escapeStringRegexp(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment