Skip to content

Commit

Permalink
Merge branch 'canary' of github.com:vercel/next.js into next-edge-ssr…
Browse files Browse the repository at this point in the history
…-bench
  • Loading branch information
timneutkens committed Sep 21, 2022
2 parents ff6664f + 2b9afcf commit 03bbba6
Show file tree
Hide file tree
Showing 26 changed files with 140 additions and 166 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Expand Up @@ -16,5 +16,5 @@
"registry": "https://registry.npmjs.org/"
}
},
"version": "12.3.1"
"version": "12.3.2-canary.0"
}
2 changes: 1 addition & 1 deletion packages/create-next-app/package.json
@@ -1,6 +1,6 @@
{
"name": "create-next-app",
"version": "12.3.1",
"version": "12.3.2-canary.0",
"keywords": [
"react",
"next",
Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-config-next/package.json
@@ -1,6 +1,6 @@
{
"name": "eslint-config-next",
"version": "12.3.1",
"version": "12.3.2-canary.0",
"description": "ESLint configuration used by NextJS.",
"main": "index.js",
"license": "MIT",
Expand All @@ -9,7 +9,7 @@
"directory": "packages/eslint-config-next"
},
"dependencies": {
"@next/eslint-plugin-next": "12.3.1",
"@next/eslint-plugin-next": "12.3.2-canary.0",
"@rushstack/eslint-patch": "^1.1.3",
"@typescript-eslint/parser": "^5.21.0",
"eslint-import-resolver-node": "^0.3.6",
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin-next/package.json
@@ -1,6 +1,6 @@
{
"name": "@next/eslint-plugin-next",
"version": "12.3.1",
"version": "12.3.2-canary.0",
"description": "ESLint plugin for NextJS.",
"main": "lib/index.js",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-bundle-analyzer/package.json
@@ -1,6 +1,6 @@
{
"name": "@next/bundle-analyzer",
"version": "12.3.1",
"version": "12.3.2-canary.0",
"main": "index.js",
"types": "index.d.ts",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-codemod/package.json
@@ -1,6 +1,6 @@
{
"name": "@next/codemod",
"version": "12.3.1",
"version": "12.3.2-canary.0",
"license": "MIT",
"dependencies": {
"chalk": "4.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-env/package.json
@@ -1,6 +1,6 @@
{
"name": "@next/env",
"version": "12.3.1",
"version": "12.3.2-canary.0",
"keywords": [
"react",
"next",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-mdx/package.json
@@ -1,6 +1,6 @@
{
"name": "@next/mdx",
"version": "12.3.1",
"version": "12.3.2-canary.0",
"main": "index.js",
"license": "MIT",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/next-plugin-storybook/package.json
@@ -1,6 +1,6 @@
{
"name": "@next/plugin-storybook",
"version": "12.3.1",
"version": "12.3.2-canary.0",
"repository": {
"url": "vercel/next.js",
"directory": "packages/next-plugin-storybook"
Expand Down
2 changes: 1 addition & 1 deletion packages/next-polyfill-module/package.json
@@ -1,6 +1,6 @@
{
"name": "@next/polyfill-module",
"version": "12.3.1",
"version": "12.3.2-canary.0",
"description": "A standard library polyfill for ES Modules supporting browsers (Edge 16+, Firefox 60+, Chrome 61+, Safari 10.1+)",
"main": "dist/polyfill-module.js",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-polyfill-nomodule/package.json
@@ -1,6 +1,6 @@
{
"name": "@next/polyfill-nomodule",
"version": "12.3.1",
"version": "12.3.2-canary.0",
"description": "A polyfill for non-dead, nomodule browsers.",
"main": "dist/polyfill-nomodule.js",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion packages/next-swc/package.json
@@ -1,6 +1,6 @@
{
"name": "@next/swc",
"version": "12.3.1",
"version": "12.3.2-canary.0",
"private": true,
"scripts": {
"build-native": "napi build --platform -p next-swc-napi --cargo-name next_swc_napi native --features plugin",
Expand Down
34 changes: 18 additions & 16 deletions packages/next/client/components/app-router.client.tsx
Expand Up @@ -29,11 +29,9 @@ import {
} from './hooks-client-context'
import { useReducerWithReduxDevtools } from './use-reducer-with-devtools'

function urlToUrlWithoutFlightParameters(url: string): URL {
function urlToUrlWithoutFlightMarker(url: string): URL {
const urlWithoutFlightParameters = new URL(url, location.origin)
urlWithoutFlightParameters.searchParams.delete('__flight__')
urlWithoutFlightParameters.searchParams.delete('__flight_router_state_tree__')
urlWithoutFlightParameters.searchParams.delete('__flight_prefetch__')
// TODO-APP: handle .rsc for static export case
return urlWithoutFlightParameters
}

Expand All @@ -45,22 +43,26 @@ export async function fetchServerResponse(
flightRouterState: FlightRouterState,
prefetch?: true
): Promise<[FlightData: FlightData, canonicalUrlOverride: URL | undefined]> {
const flightUrl = new URL(url)
const searchParams = flightUrl.searchParams
// Enable flight response
searchParams.append('__flight__', '1')
// Provide the current router state
searchParams.append(
'__flight_router_state_tree__',
JSON.stringify(flightRouterState)
)
const headers: {
__flight__: '1'
__flight_router_state_tree__: string
__flight_prefetch__?: '1'
} = {
// Enable flight response
__flight__: '1',
// Provide the current router state
__flight_router_state_tree__: JSON.stringify(flightRouterState),
}
if (prefetch) {
searchParams.append('__flight_prefetch__', '1')
// Enable prefetch response
headers.__flight_prefetch__ = '1'
}

const res = await fetch(flightUrl.toString())
const res = await fetch(url.toString(), {
headers,
})
const canonicalUrl = res.redirected
? urlToUrlWithoutFlightParameters(res.url)
? urlToUrlWithoutFlightMarker(res.url)
: undefined

// Handle the `fetch` readable stream that can be unwrapped by `React.use`.
Expand Down
14 changes: 7 additions & 7 deletions packages/next/package.json
@@ -1,6 +1,6 @@
{
"name": "next",
"version": "12.3.1",
"version": "12.3.2-canary.0",
"description": "The React Framework",
"main": "./dist/server/next.js",
"license": "MIT",
Expand Down Expand Up @@ -68,7 +68,7 @@
]
},
"dependencies": {
"@next/env": "12.3.1",
"@next/env": "12.3.2-canary.0",
"@swc/helpers": "0.4.11",
"caniuse-lite": "^1.0.30001406",
"postcss": "8.4.14",
Expand Down Expand Up @@ -119,11 +119,11 @@
"@hapi/accept": "5.0.2",
"@napi-rs/cli": "2.7.0",
"@napi-rs/triples": "1.1.0",
"@next/polyfill-module": "12.3.1",
"@next/polyfill-nomodule": "12.3.1",
"@next/react-dev-overlay": "12.3.1",
"@next/react-refresh-utils": "12.3.1",
"@next/swc": "12.3.1",
"@next/polyfill-module": "12.3.2-canary.0",
"@next/polyfill-nomodule": "12.3.2-canary.0",
"@next/react-dev-overlay": "12.3.2-canary.0",
"@next/react-refresh-utils": "12.3.2-canary.0",
"@next/swc": "12.3.2-canary.0",
"@segment/ajv-human-errors": "2.1.2",
"@taskr/clear": "1.1.0",
"@taskr/esnext": "1.1.0",
Expand Down
26 changes: 20 additions & 6 deletions packages/next/server/app-render.tsx
@@ -1,4 +1,4 @@
import type { IncomingMessage, ServerResponse } from 'http'
import type { IncomingHttpHeaders, IncomingMessage, ServerResponse } from 'http'
import type { LoadComponentsReturnType } from './load-components'
import type { ServerRuntime } from '../types'

Expand Down Expand Up @@ -493,6 +493,20 @@ function getScriptNonceFromHeader(cspHeaderValue: string): string | undefined {
return nonce
}

const FLIGHT_PARAMETERS = [
'__flight__',
'__flight_router_state_tree__',
'__flight_prefetch__',
] as const

function headersWithoutFlight(headers: IncomingHttpHeaders) {
const newHeaders = { ...headers }
for (const param of FLIGHT_PARAMETERS) {
delete newHeaders[param]
}
return newHeaders
}

export async function renderToHTMLOrFlight(
req: IncomingMessage,
res: ServerResponse,
Expand Down Expand Up @@ -545,8 +559,8 @@ export async function renderToHTMLOrFlight(
ComponentMod,
} = renderOpts

const isFlight = query.__flight__ !== undefined
const isPrefetch = query.__flight_prefetch__ !== undefined
const isFlight = req.headers.__flight__ !== undefined
const isPrefetch = req.headers.__flight_prefetch__ !== undefined

// Handle client-side navigation to pages directory
if (isFlight && isPagesDir) {
Expand All @@ -569,8 +583,8 @@ export async function renderToHTMLOrFlight(
* Router state provided from the client-side router. Used to handle rendering from the common layout down.
*/
const providedFlightRouterState: FlightRouterState = isFlight
? query.__flight_router_state_tree__
? JSON.parse(query.__flight_router_state_tree__ as string)
? req.headers.__flight_router_state_tree__
? JSON.parse(req.headers.__flight_router_state_tree__ as string)
: {}
: undefined

Expand All @@ -584,7 +598,7 @@ export async function renderToHTMLOrFlight(
| typeof import('../client/components/hot-reloader.client').default
| null

const headers = req.headers
const headers = headersWithoutFlight(req.headers)
// TODO-APP: fix type of req
// @ts-expect-error
const cookies = req.cookies
Expand Down
2 changes: 1 addition & 1 deletion packages/next/server/base-server.ts
Expand Up @@ -1032,7 +1032,7 @@ export default abstract class Server<ServerOptions extends Options = Options> {

// Don't delete query.__flight__ yet, it still needs to be used in renderToHTML later
const isFlightRequest = Boolean(
this.serverComponentManifest && query.__flight__
this.serverComponentManifest && req.headers.__flight__
)

// we need to ensure the status code if /404 is visited directly
Expand Down
3 changes: 1 addition & 2 deletions packages/next/server/next-server.ts
Expand Up @@ -825,7 +825,7 @@ export default class NextNodeServer extends BaseServer {

if (
this.nextConfig.experimental.appDir &&
(renderOpts.isAppPath || query.__flight__)
(renderOpts.isAppPath || req.headers.__flight__)
) {
const isPagesDir = !renderOpts.isAppPath
return appRenderToHTMLOrFlight(
Expand Down Expand Up @@ -981,7 +981,6 @@ export default class NextNodeServer extends BaseServer {
__nextDataReq: query.__nextDataReq,
__nextLocale: query.__nextLocale,
__nextDefaultLocale: query.__nextDefaultLocale,
__flight__: query.__flight__,
} as NextParsedUrlQuery)
: query),
// For appDir params is excluded.
Expand Down
19 changes: 11 additions & 8 deletions packages/next/server/web/adapter.ts
Expand Up @@ -35,6 +35,12 @@ class NextRequestHint extends NextRequest {
}
}

const FLIGHT_PARAMETERS = [
'__flight__',
'__flight_router_state_tree__',
'__flight_prefetch__',
] as const

export async function adapter(params: {
handler: NextMiddleware
page: string
Expand All @@ -58,11 +64,12 @@ export async function adapter(params: {
requestUrl.pathname = '/'
}

// Preserve flight data.
const flightSearchParameters = requestUrl.flightSearchParameters
const requestHeaders = fromNodeHeaders(params.request.headers)
// Parameters should only be stripped for middleware
if (!isEdgeRendering) {
requestUrl.flightSearchParameters = undefined
for (const param of FLIGHT_PARAMETERS) {
requestHeaders.delete(param)
}
}

// Strip internal query parameters off the request.
Expand All @@ -74,7 +81,7 @@ export async function adapter(params: {
init: {
body: params.request.body,
geo: params.request.geo,
headers: fromNodeHeaders(params.request.headers),
headers: requestHeaders,
ip: params.request.ip,
method: params.request.method,
nextConfig: params.request.nextConfig,
Expand Down Expand Up @@ -112,8 +119,6 @@ export async function adapter(params: {

if (rewriteUrl.host === request.nextUrl.host) {
rewriteUrl.buildId = buildId || rewriteUrl.buildId
rewriteUrl.flightSearchParameters =
flightSearchParameters || rewriteUrl.flightSearchParameters
response.headers.set('x-middleware-rewrite', String(rewriteUrl))
}

Expand Down Expand Up @@ -151,8 +156,6 @@ export async function adapter(params: {

if (redirectURL.host === request.nextUrl.host) {
redirectURL.buildId = buildId || redirectURL.buildId
redirectURL.flightSearchParameters =
flightSearchParameters || redirectURL.flightSearchParameters
response.headers.set('Location', String(redirectURL))
}

Expand Down

0 comments on commit 03bbba6

Please sign in to comment.