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

fix: duplicate app server #36710

Merged
merged 4 commits into from May 5, 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
22 changes: 11 additions & 11 deletions packages/next/server/render.tsx
Expand Up @@ -208,7 +208,6 @@ function renderPageTree(
isServerComponent: boolean
) {
const { router: _, ...rest } = props

if (isServerComponent) {
return (
<App>
Expand Down Expand Up @@ -400,7 +399,7 @@ const useFlightResponse = createFlightHook()
// Create the wrapper component for a Flight stream.
function createServerComponentRenderer(
App: any,
ComponentMod: any,
Component: any,
{
cachePrefix,
inlinedTransformStream,
Expand All @@ -413,12 +412,6 @@ function createServerComponentRenderer(
serverComponentManifest: NonNullable<RenderOpts['serverComponentManifest']>
}
) {
// We need to expose the `__webpack_require__` API globally for
// react-server-dom-webpack. This is a hack until we find a better way.
// @ts-ignore
globalThis.__webpack_require__ = ComponentMod.__next_rsc__.__webpack_require__
const Component = interopDefault(ComponentMod)

function ServerComponentWrapper({ router, ...props }: any) {
const id = (React as any).useId()

Expand Down Expand Up @@ -527,7 +520,13 @@ export async function renderToHTML(
if (isServerComponent) {
serverComponentsInlinedTransformStream = new TransformStream()
const search = urlQueryToSearchParams(query).toString()
Component = createServerComponentRenderer(App, ComponentMod, {
// We need to expose the `__webpack_require__` API globally for
// react-server-dom-webpack. This is a hack until we find a better way.
// @ts-ignore
globalThis.__webpack_require__ =
ComponentMod.__next_rsc__.__webpack_require__

Component = createServerComponentRenderer(App, Component, {
cachePrefix: pathname + (search ? `?${search}` : ''),
inlinedTransformStream: serverComponentsInlinedTransformStream,
staticTransformStream: serverComponentsPageDataTransformStream,
Expand Down Expand Up @@ -1257,7 +1256,7 @@ export async function renderToHTML(
...props.pageProps,
...serverComponentProps,
},
isServerComponent
true
),
serverComponentManifest
).pipeThrough(createBufferedTransformStream())
Expand Down Expand Up @@ -1425,7 +1424,8 @@ export async function renderToHTML(
<Body>
<AppContainerWithIsomorphicFiberStructure>
{renderPageTree(
EnhancedApp,
// AppServer is included in the EnhancedComponent in ServerComponentWrapper
isServerComponent ? React.Fragment : EnhancedApp,
EnhancedComponent,
{ ...(isServerComponent ? props.pageProps : props), router },
isServerComponent
Expand Down
@@ -0,0 +1,7 @@
export default function App({ Component, pageProps }) {
return (
<div className="app-client-root">
<Component {...pageProps} />
</div>
)
}
Expand Up @@ -38,12 +38,16 @@ async function testRoute(appPort, url, { isStatic, isEdge, isRSC }) {
// Should be re-rendered.
expect(renderedAt1).toBeLessThan(renderedAt2)
}
const customAppServerHtml = '<div class="app-server-root"'
if (isRSC) {
expect(html1).toContain(customAppServerHtml)
} else {
expect(html1).not.toContain(customAppServerHtml)
}
// If the page is using 1 root, it won't use the other.
// e.g. server component page won't have client app root.
const rootClasses = ['app-server-root', 'app-client-root']
const [rootClass, oppositeRootClass] = isRSC
? [rootClasses[0], rootClasses[1]]
: [rootClasses[1], rootClasses[0]]
const appServerOccurrence =
html1.match(new RegExp(`class="${rootClass}"`, 'g')) || []
expect(appServerOccurrence.length).toBe(1)
expect(html1).not.toContain(`"${oppositeRootClass}"`)
}

describe('Switchable runtime (prod)', () => {
Expand Down