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

Move handling of navigation to pages from new router #41001

Merged
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
8 changes: 8 additions & 0 deletions packages/next/client/components/app-router.client.tsx
Expand Up @@ -65,6 +65,14 @@ export async function fetchServerResponse(
? urlToUrlWithoutFlightMarker(res.url)
: 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`.
const flightData: FlightData = await createFromFetch(Promise.resolve(res))
return [flightData, canonicalUrl]
Expand Down
1 change: 0 additions & 1 deletion packages/next/export/worker.ts
Expand Up @@ -407,7 +407,6 @@ export default async function exportPage({
page,
query,
curRenderOpts as any,
false,
true
)
const html = result?.toUnchunkedString()
Expand Down
29 changes: 1 addition & 28 deletions packages/next/server/app-render.tsx
Expand Up @@ -6,7 +6,7 @@ import type { ServerRuntime } from '../types'
// @ts-ignore
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 { NextParsedUrlQuery } from './request-meta'
import RenderResult from './render-result'
Expand Down Expand Up @@ -609,7 +609,6 @@ export async function renderToHTMLOrFlight(
pathname: string,
query: NextParsedUrlQuery,
renderOpts: RenderOpts,
isPagesDir: boolean,
isStaticGeneration: boolean = false
): Promise<RenderResult | null> {
const isFlight = req.headers.__rsc__ !== undefined
Expand Down Expand Up @@ -638,32 +637,6 @@ export async function renderToHTMLOrFlight(
ComponentMod,
} = 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)

const staticGenerationAsyncStorage = ComponentMod.staticGenerationAsyncStorage
Expand Down
9 changes: 2 additions & 7 deletions packages/next/server/next-server.ts
Expand Up @@ -826,18 +826,13 @@ export default class NextNodeServer extends BaseServer {
renderOpts.serverCSSManifest = this.serverCSSManifest
renderOpts.fontLoaderManifest = this.fontLoaderManifest

if (
this.nextConfig.experimental.appDir &&
(renderOpts.isAppPath || req.headers.__rsc__)
) {
const isPagesDir = !renderOpts.isAppPath
if (this.nextConfig.experimental.appDir && renderOpts.isAppPath) {
return appRenderToHTMLOrFlight(
req.originalRequest,
res.originalResponse,
pathname,
query,
renderOpts,
isPagesDir
renderOpts
)
}

Expand Down
11 changes: 7 additions & 4 deletions test/e2e/app-dir/index.test.ts
Expand Up @@ -524,18 +524,21 @@ describe('app dir', () => {
}
})

// TODO-APP: should enable when implemented
it.skip('should allow linking from app page to pages page', async () => {
it('should allow linking from app page to pages page', async () => {
const browser = await webdriver(next.url, '/pages-linking')

try {
// Click the link.
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.
await browser.elementById('pages-link').click()
await browser.waitForElementByCss('#app-link')
expect(await browser.waitForElementByCss('#app-link').text()).toBe(
'To Pages Page'
)
} finally {
await browser.close()
}
Expand Down