Unverified Commit c5d2c2d9 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Move handling of navigation to pages from new router (#41001)

Ensures the static generation case is covered. Checks
application/octet-stream client-side, if it's not does a full page
navigation.

<!--
Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change that you're making:
-->

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a 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 a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 335e9180
...@@ -65,6 +65,14 @@ export async function fetchServerResponse( ...@@ -65,6 +65,14 @@ export async function fetchServerResponse(
? urlToUrlWithoutFlightMarker(res.url) ? urlToUrlWithoutFlightMarker(res.url)
: undefined : undefined
const isFlightResponse =
res.headers.get('content-type') === 'application/octet-stream'
// If fetch returns something different than flight response handle it like a mpa navigation
if (!isFlightResponse) {
return [res.url, undefined]
}
// Handle the `fetch` readable stream that can be unwrapped by `React.use`. // Handle the `fetch` readable stream that can be unwrapped by `React.use`.
const flightData: FlightData = await createFromFetch(Promise.resolve(res)) const flightData: FlightData = await createFromFetch(Promise.resolve(res))
return [flightData, canonicalUrl] return [flightData, canonicalUrl]
......
...@@ -407,7 +407,6 @@ export default async function exportPage({ ...@@ -407,7 +407,6 @@ export default async function exportPage({
page, page,
query, query,
curRenderOpts as any, curRenderOpts as any,
false,
true true
) )
const html = result?.toUnchunkedString() const html = result?.toUnchunkedString()
......
...@@ -6,7 +6,7 @@ import type { ServerRuntime } from '../types' ...@@ -6,7 +6,7 @@ import type { ServerRuntime } from '../types'
// @ts-ignore // @ts-ignore
import React, { experimental_use as use } from 'react' import React, { experimental_use as use } from 'react'
import { ParsedUrlQuery, stringify as stringifyQuery } from 'querystring' import { ParsedUrlQuery } from 'querystring'
import { createFromReadableStream } from 'next/dist/compiled/react-server-dom-webpack' import { createFromReadableStream } from 'next/dist/compiled/react-server-dom-webpack'
import { NextParsedUrlQuery } from './request-meta' import { NextParsedUrlQuery } from './request-meta'
import RenderResult from './render-result' import RenderResult from './render-result'
...@@ -609,7 +609,6 @@ export async function renderToHTMLOrFlight( ...@@ -609,7 +609,6 @@ export async function renderToHTMLOrFlight(
pathname: string, pathname: string,
query: NextParsedUrlQuery, query: NextParsedUrlQuery,
renderOpts: RenderOpts, renderOpts: RenderOpts,
isPagesDir: boolean,
isStaticGeneration: boolean = false isStaticGeneration: boolean = false
): Promise<RenderResult | null> { ): Promise<RenderResult | null> {
const isFlight = req.headers.__rsc__ !== undefined const isFlight = req.headers.__rsc__ !== undefined
...@@ -638,32 +637,6 @@ export async function renderToHTMLOrFlight( ...@@ -638,32 +637,6 @@ export async function renderToHTMLOrFlight(
ComponentMod, ComponentMod,
} = renderOpts } = renderOpts
// Handle client-side navigation to pages directory
// This is handled before
if (isFlight && isPagesDir) {
stripInternalQueries(query)
const search = stringifyQuery(query)
// For pages dir, there is only the SSR pass and we don't have the bundled
// React subset. Here we directly import the flight renderer with the
// unbundled React.
// TODO-APP: Is it possible to hard code the flight response here instead of
// rendering it?
const ReactServerDOMWebpack = require('next/dist/compiled/react-server-dom-webpack/writer.browser.server')
// Empty so that the client-side router will do a full page navigation.
const flightData: FlightData = pathname + (search ? `?${search}` : '')
return new FlightRenderResult(
ReactServerDOMWebpack.renderToReadableStream(
flightData,
serverComponentManifest,
{
onError: flightDataRendererErrorHandler,
}
).pipeThrough(createBufferedTransformStream())
)
}
patchFetch(ComponentMod) patchFetch(ComponentMod)
const staticGenerationAsyncStorage = ComponentMod.staticGenerationAsyncStorage const staticGenerationAsyncStorage = ComponentMod.staticGenerationAsyncStorage
......
...@@ -826,18 +826,13 @@ export default class NextNodeServer extends BaseServer { ...@@ -826,18 +826,13 @@ export default class NextNodeServer extends BaseServer {
renderOpts.serverCSSManifest = this.serverCSSManifest renderOpts.serverCSSManifest = this.serverCSSManifest
renderOpts.fontLoaderManifest = this.fontLoaderManifest renderOpts.fontLoaderManifest = this.fontLoaderManifest
if ( if (this.nextConfig.experimental.appDir && renderOpts.isAppPath) {
this.nextConfig.experimental.appDir &&
(renderOpts.isAppPath || req.headers.__rsc__)
) {
const isPagesDir = !renderOpts.isAppPath
return appRenderToHTMLOrFlight( return appRenderToHTMLOrFlight(
req.originalRequest, req.originalRequest,
res.originalResponse, res.originalResponse,
pathname, pathname,
query, query,
renderOpts, renderOpts
isPagesDir
) )
} }
......
...@@ -524,18 +524,21 @@ describe('app dir', () => { ...@@ -524,18 +524,21 @@ describe('app dir', () => {
} }
}) })
// TODO-APP: should enable when implemented it('should allow linking from app page to pages page', async () => {
it.skip('should allow linking from app page to pages page', async () => {
const browser = await webdriver(next.url, '/pages-linking') const browser = await webdriver(next.url, '/pages-linking')
try { try {
// Click the link. // Click the link.
await browser.elementById('app-link').click() await browser.elementById('app-link').click()
await browser.waitForElementByCss('#pages-link') expect(await browser.waitForElementByCss('#pages-link').text()).toBe(
'To App Page'
)
// Click the other link. // Click the other link.
await browser.elementById('pages-link').click() await browser.elementById('pages-link').click()
await browser.waitForElementByCss('#app-link') expect(await browser.waitForElementByCss('#app-link').text()).toBe(
'To Pages Page'
)
} finally { } finally {
await browser.close() await browser.close()
} }
......
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