Unverified Commit 4475de58 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update client router for tests (#36822)



## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
Co-authored-by: 's avatarTim Neutkens <6324199+timneutkens@users.noreply.github.com>
parent 1ccf368f
...@@ -554,7 +554,7 @@ export default async function getBaseWebpackConfig( ...@@ -554,7 +554,7 @@ export default async function getBaseWebpackConfig(
path path
.relative( .relative(
dir, dir,
path.join(NEXT_PROJECT_ROOT_DIST_CLIENT, 'root-next.js') path.join(NEXT_PROJECT_ROOT_DIST_CLIENT, 'views-next.js')
) )
.replace(/\\/g, '/'), .replace(/\\/g, '/'),
} }
......
...@@ -194,11 +194,58 @@ const RSCComponent = (props: any) => { ...@@ -194,11 +194,58 @@ const RSCComponent = (props: any) => {
) )
} }
const ViewRouterContext = React.createContext({})
// TODO: move to client component when handling is implemented
function ViewRouter({ initialUrl, children }: any) {
const initialState = {
url: initialUrl,
}
const previousUrlRef = React.useRef(initialState)
const [current, setCurrent] = React.useState(initialState)
const viewRouter = React.useMemo(() => {
return {
push: (url: string) => {
previousUrlRef.current = current
setCurrent({ ...current, url })
// TODO: update url eagerly or not?
window.history.pushState(current, '', url)
},
url: current.url,
}
}, [current])
// @ts-ignore TODO: for testing
window.viewRouter = viewRouter
console.log({
viewRouter,
previous: previousUrlRef.current,
current,
})
let root
if (current.url !== previousUrlRef.current?.url) {
// eslint-disable-next-line
const data = useServerResponse(current.url)
root = data.readRoot()
}
return (
<ViewRouterContext.Provider value={viewRouter}>
{root ? root : children}
</ViewRouterContext.Provider>
)
}
export function hydrate() { export function hydrate() {
renderReactElement(appElement!, () => ( renderReactElement(appElement!, () => (
<React.StrictMode> <React.StrictMode>
<Root> <Root>
<RSCComponent /> <ViewRouter initialUrl={location.pathname}>
<RSCComponent />
</ViewRouter>
</Root> </Root>
</React.StrictMode> </React.StrictMode>
)) ))
......
import { hydrate, version } from './root-index' import { hydrate, version } from './views-index'
window.next = { window.next = {
version, version,
......
import React from 'react' import React from 'react'
export type LayoutProps = { export type LayoutProps = {
headChildren: any children: any
bodyChildren: any
} }
export default function ViewsLayout({ export default function ViewsLayout({ children }: LayoutProps) {
headChildren,
bodyChildren,
}: LayoutProps) {
return ( return (
<html> <html>
<head> <head>
{headChildren} {/* TODO: Remove <title> */}
<title>Test</title> <title>Test</title>
</head> </head>
<body>{bodyChildren}</body> <body>{children}</body>
</html> </html>
) )
} }
...@@ -139,11 +139,13 @@ export function createFlushEffectStream( ...@@ -139,11 +139,13 @@ export function createFlushEffectStream(
export function renderToInitialStream({ export function renderToInitialStream({
ReactDOMServer, ReactDOMServer,
element, element,
streamOptions,
}: { }: {
ReactDOMServer: any ReactDOMServer: any
element: React.ReactElement element: React.ReactElement
streamOptions?: any
}): Promise<ReactReadableStream> { }): Promise<ReactReadableStream> {
return ReactDOMServer.renderToReadableStream(element) return ReactDOMServer.renderToReadableStream(element, streamOptions)
} }
export async function continueFromInitialStream( export async function continueFromInitialStream(
...@@ -181,30 +183,6 @@ export async function continueFromInitialStream( ...@@ -181,30 +183,6 @@ export async function continueFromInitialStream(
) )
} }
export async function renderToStream({
ReactDOMServer,
element,
suffix,
dataStream,
generateStaticHTML,
flushEffectHandler,
}: {
ReactDOMServer: typeof import('react-dom/server')
element: React.ReactElement
suffix?: string
dataStream?: ReadableStream<Uint8Array>
generateStaticHTML: boolean
flushEffectHandler?: () => string
}): Promise<ReadableStream<Uint8Array>> {
const renderStream = await renderToInitialStream({ ReactDOMServer, element })
return continueFromInitialStream(renderStream, {
suffix,
dataStream,
generateStaticHTML,
flushEffectHandler,
})
}
export function createSuffixStream( export function createSuffixStream(
suffix: string suffix: string
): TransformStream<Uint8Array, Uint8Array> { ): TransformStream<Uint8Array, Uint8Array> {
......
...@@ -326,12 +326,11 @@ export async function renderToHTML( ...@@ -326,12 +326,11 @@ export async function renderToHTML(
} }
} }
// if this is the root layout pass children as bodyChildren prop // if this is the root layout pass children as children prop
if (!isSubtreeRender && i === 0) { if (!isSubtreeRender && i === 0) {
return React.createElement(layout.Component, { return React.createElement(layout.Component, {
...props, ...props,
headChildren: props.headChildren, children: React.createElement(
bodyChildren: React.createElement(
lastComponent || React.Fragment, lastComponent || React.Fragment,
{}, {},
null null
...@@ -356,10 +355,8 @@ export async function renderToHTML( ...@@ -356,10 +355,8 @@ export async function renderToHTML(
// } // }
} }
const headChildren = !isSubtreeRender const bootstrapScripts = !isSubtreeRender
? buildManifest.rootMainFiles.map((src) => ( ? buildManifest.rootMainFiles.map((src) => '/_next/' + src)
<script src={'/_next/' + src} async key={src} />
))
: undefined : undefined
let serverComponentsInlinedTransformStream: TransformStream< let serverComponentsInlinedTransformStream: TransformStream<
...@@ -429,7 +426,7 @@ export async function renderToHTML( ...@@ -429,7 +426,7 @@ export async function renderToHTML(
if (renderServerComponentData) { if (renderServerComponentData) {
return new RenderResult( return new RenderResult(
renderToReadableStream( renderToReadableStream(
<WrappedComponent headChildren={headChildren} />, <WrappedComponent />,
serverComponentManifest serverComponentManifest
).pipeThrough(createBufferedTransformStream()) ).pipeThrough(createBufferedTransformStream())
) )
...@@ -452,13 +449,16 @@ export async function renderToHTML( ...@@ -452,13 +449,16 @@ export async function renderToHTML(
const bodyResult = async () => { const bodyResult = async () => {
const content = ( const content = (
<AppContainer> <AppContainer>
<Component headChildren={headChildren} /> <Component />
</AppContainer> </AppContainer>
) )
const renderStream = await renderToInitialStream({ const renderStream = await renderToInitialStream({
ReactDOMServer, ReactDOMServer,
element: content, element: content,
streamOptions: {
bootstrapScripts,
},
}) })
const flushEffectHandler = (): string => { const flushEffectHandler = (): string => {
......
...@@ -6,14 +6,13 @@ export async function getServerSideProps() { ...@@ -6,14 +6,13 @@ export async function getServerSideProps() {
} }
} }
export default function Root({ headChildren, bodyChildren, world }) { export default function Root({ children, world }) {
return ( return (
<html className="this-is-another-document-html"> <html className="this-is-another-document-html">
<head> <head>
{headChildren}
<title>{`hello ${world}`}</title> <title>{`hello ${world}`}</title>
</head> </head>
<body className="this-is-another-document-body">{bodyChildren}</body> <body className="this-is-another-document-body">{children}</body>
</html> </html>
) )
} }
...@@ -6,14 +6,13 @@ export async function getServerSideProps() { ...@@ -6,14 +6,13 @@ export async function getServerSideProps() {
} }
} }
export default function Root({ headChildren, bodyChildren, world }) { export default function Root({ children, world }) {
return ( return (
<html className="this-is-the-document-html"> <html className="this-is-the-document-html">
<head> <head>
{headChildren}
<title>{`hello ${world}`}</title> <title>{`hello ${world}`}</title>
</head> </head>
<body className="this-is-the-document-body">{bodyChildren}</body> <body className="this-is-the-document-body">{children}</body>
</html> </html>
) )
} }
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