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 missing _app component of AppTree in gIP context #36206

Merged
merged 2 commits into from Apr 16, 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
17 changes: 11 additions & 6 deletions packages/next/server/render.tsx
Expand Up @@ -202,12 +202,17 @@ function renderFlight(AppMod: any, ComponentMod: any, props: any) {
const AppServer = isServerComponent
? (App as React.ComponentType)
: React.Fragment
const { router: _, ...rest } = props

return (
<AppServer>
<Component {...props} />
</AppServer>
)
if (isServerComponent) {
return (
<AppServer>
<Component {...rest} />
</AppServer>
)
}

return <App Component={Component} {...props} />
}

export type RenderOptsPartial = {
Expand Down Expand Up @@ -734,7 +739,7 @@ export async function renderToHTML(
AppTree: (props: any) => {
return (
<AppContainerWithIsomorphicFiberStructure>
{renderFlight(AppMod, ComponentMod, props)}
{renderFlight(AppMod, ComponentMod, { ...props, router })}
</AppContainerWithIsomorphicFiberStructure>
)
},
Expand Down
26 changes: 17 additions & 9 deletions test/integration/app-tree/pages/_app.tsx
@@ -1,9 +1,12 @@
import React from 'react'
import Link from 'next/link'
import { createContext } from 'react'
import { render } from 'react-dom'
import App, { AppContext } from 'next/app'
import { renderToString } from 'react-dom/server'

export const DummyContext = createContext(null)

class MyApp<P = {}> extends App<P & { html: string }> {
static async getInitialProps({ Component, AppTree, ctx }: AppContext) {
let pageProps = {}
Expand Down Expand Up @@ -32,15 +35,20 @@ class MyApp<P = {}> extends App<P & { html: string }> {
const { Component, pageProps, html, router } = this.props
const href = router.pathname === '/' ? '/another' : '/'

return html && router.pathname !== '/hello' ? (
<>
<div dangerouslySetInnerHTML={{ __html: html }} />
<Link href={href}>
<a id={href === '/' ? 'home' : 'another'}>to {href}</a>
</Link>
</>
) : (
<Component {...pageProps} />
const child =
html && router.pathname !== '/hello' ? (
<>
<div dangerouslySetInnerHTML={{ __html: html }} />
<Link href={href}>
<a id={href === '/' ? 'home' : 'another'}>to {href}</a>
</Link>
</>
) : (
<Component {...pageProps} />
)

return (
<DummyContext.Provider value={'::ctx::'}>{child}</DummyContext.Provider>
)
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/integration/app-tree/pages/index.js
@@ -1,7 +1,12 @@
import { useContext } from 'react'
import { DummyContext } from './_app'
import { useRouter } from 'next/router'

const Page = () => {
const { pathname } = useRouter()
const ctx = useContext(DummyContext)
if (ctx == null) throw new Error('context consumes failed')

return (
<>
<h3>page: {pathname}</h3>
Expand Down