Skip to content

Commit

Permalink
add test for app-tree
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Apr 16, 2022
1 parent 95d7b5a commit 77b56b3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
9 changes: 5 additions & 4 deletions packages/next/server/render.tsx
Expand Up @@ -197,20 +197,21 @@ function enhanceComponents(

function renderFlight(AppMod: any, ComponentMod: any, props: any) {
const isServerComponent = !!ComponentMod.__next_rsc__
const isServerApp = !!AppMod.__next_rsc__
const App = interopDefault(AppMod)
const Component = interopDefault(ComponentMod)
const AppServer = isServerComponent
? (App as React.ComponentType)
: React.Fragment
const { router: _, ...rest } = props

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

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

Expand Down Expand Up @@ -738,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

0 comments on commit 77b56b3

Please sign in to comment.