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

Optimize inlined Flight data array format #52028

Merged
merged 7 commits into from Jun 30, 2023
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
Expand Up @@ -41,19 +41,19 @@ let initialServerDataWriter: ReadableStreamDefaultController | undefined =
let initialServerDataLoaded = false
let initialServerDataFlushed = false

function nextServerDataCallback(
seg: [isBootStrap: 0] | [isNotBootstrap: 1, responsePartial: string]
): number {
if (seg[0] === 0) {
type IsBootStrap = 0
type ResponsePartial = string
function nextServerDataCallback(seg: IsBootStrap | ResponsePartial): number {
if (seg === 0) {
initialServerDataBuffer = []
} else {
if (!initialServerDataBuffer)
throw new Error('Unexpected server data: missing bootstrap script.')

if (initialServerDataWriter) {
initialServerDataWriter.enqueue(encoder.encode(seg[1]))
initialServerDataWriter.enqueue(encoder.encode(seg))
} else {
initialServerDataBuffer.push(seg[1])
initialServerDataBuffer.push(seg)
}
}
return 0
Expand Down
8 changes: 4 additions & 4 deletions packages/next-swc/crates/next-core/js/types/globals.d.ts
Expand Up @@ -10,10 +10,10 @@ declare global {

var __next_require__: (id: string) => any
var __next_chunk_load__: (id: string) => Promise
var __next_f: (
| [isBootStrap: 0]
| [isNotBootstrap: 1, responsePartial: string]
)[]

type isBootStrap = 0
type responsePartial = string
var __next_f: (isBootStrap | responsePartial)[]
var next: {
version: string
appDir?: boolean
Expand Down
12 changes: 6 additions & 6 deletions packages/next/src/client/app-index.tsx
Expand Up @@ -116,19 +116,19 @@ let initialServerDataWriter: ReadableStreamDefaultController | undefined =
let initialServerDataLoaded = false
let initialServerDataFlushed = false

function nextServerDataCallback(
seg: [isBootStrap: 0] | [isNotBootstrap: 1, responsePartial: string]
): void {
if (seg[0] === 0) {
type IsBootStrap = 0
type ResponsePartial = string
function nextServerDataCallback(seg: IsBootStrap | ResponsePartial): void {
if (seg === 0) {
initialServerDataBuffer = []
} else {
if (!initialServerDataBuffer)
throw new Error('Unexpected server data: missing bootstrap script.')

if (initialServerDataWriter) {
initialServerDataWriter.enqueue(encoder.encode(seg[1]))
initialServerDataWriter.enqueue(encoder.encode(seg))
} else {
initialServerDataBuffer.push(seg[1])
initialServerDataBuffer.push(seg)
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions packages/next/src/server/app-render/use-flight-response.tsx
Expand Up @@ -53,7 +53,7 @@ export function useFlightResponse(
writer.write(
encodeText(
`${startScriptTag}(self.__next_f=self.__next_f||[]).push(${htmlEscapeJsonString(
JSON.stringify([0])
JSON.stringify(0)
)})</script>`
)
)
Expand All @@ -63,8 +63,14 @@ export function useFlightResponse(
writer.close()
} else {
const responsePartial = decodeText(value, textDecoder)
const scripts = `${startScriptTag}self.__next_f.push(${htmlEscapeJsonString(
JSON.stringify([1, responsePartial])
const scripts = `${startScriptTag}__next_f.push(${htmlEscapeJsonString(
// Since the inlined payload is always a JSON-ish encoded string with
// many double quotes, we can safely un-escape these quotes and use
// a single quote to wrap the string. This saves a lot of bytes.
JSON.stringify(responsePartial)
.replace(/\\"/g, '"')
.replace(/'/g, "\\'")
.replace(/(^")|("$)/g, "'")
)})</script>`

writer.write(encodeText(scripts))
Expand Down
7 changes: 5 additions & 2 deletions test/e2e/app-dir/not-found/not-found.test.ts
Expand Up @@ -15,8 +15,11 @@ createNextDescribe(
})

it('should allow to have a valid /not-found route', async () => {
const html = await next.render('/not-found')
expect(html).toContain("I'm still a valid page")
const browser = await next.browser('/not-found')
await check(
() => browser.elementByCss('h1').text(),
`I'm still a valid page`
)
})

if (isNextDev) {
Expand Down