Unverified Commit 088354d4 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Render resolved streaming content for static render result (#35221)

* Fix the static streaming render result isn't the resolved streaming rendering content (resolve suspense)
* Update readable stream polyfill to fit the w3c standard
parent 7ce5d3f2
...@@ -131,7 +131,6 @@ export default class NextWebServer extends BaseServer { ...@@ -131,7 +131,6 @@ export default class NextWebServer extends BaseServer {
query, query,
{ {
...renderOpts, ...renderOpts,
// supportsDynamicHTML: true,
disableOptimizedLoading: true, disableOptimizedLoading: true,
runtime: 'edge', runtime: 'edge',
} }
......
...@@ -39,17 +39,17 @@ class ReadableStream<T> { ...@@ -39,17 +39,17 @@ class ReadableStream<T> {
const pull = () => { const pull = () => {
if (opts.pull) { if (opts.pull) {
if (!pullPromise) { const shouldPull =
controller.desiredSize !== null && controller.desiredSize > 0
if (!pullPromise && shouldPull) {
pullPromise = Promise.resolve().then(() => { pullPromise = Promise.resolve().then(() => {
pullPromise = 0 pullPromise = 0
opts.pull!(controller) opts.pull!(controller)
}) })
return pullPromise
} }
} }
} return Promise.resolve()
if (opts.start) {
opts.start(controller)
} }
if (opts.cancel) { if (opts.cancel) {
...@@ -59,7 +59,20 @@ class ReadableStream<T> { ...@@ -59,7 +59,20 @@ class ReadableStream<T> {
} }
} }
pull() function registerPull() {
const getReader = readable.getReader.bind(readable)
readable.getReader = () => {
pull()
return getReader()
}
}
const started = opts.start && opts.start(controller)
if (started && typeof started.then === 'function') {
started.then(() => registerPull())
} else {
registerPull()
}
return readable return readable
} }
......
...@@ -54,4 +54,13 @@ export default async function basic(context, { env }) { ...@@ -54,4 +54,13 @@ export default async function basic(context, { env }) {
const html = await renderViaHTTP(context.appPort, '/err/suspense') const html = await renderViaHTTP(context.appPort, '/err/suspense')
expect(html).toContain('error-fallback') expect(html).toContain('error-fallback')
}) })
it('should support React.lazy and dynamic imports', async () => {
const html = await renderViaHTTP(context.appPort, '/dynamic-imports')
expect(html).toContain('foo.client')
const browser = await webdriver(context.appPort, '/dynamic-imports')
const content = await browser.eval(`window.document.body.innerText`)
expect(content).toMatchInlineSnapshot('"foo.client"')
})
} }
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
import { join } from 'path' import { join } from 'path'
import fs from 'fs-extra' import fs from 'fs-extra'
import webdriver from 'next-webdriver'
import { fetchViaHTTP, findPort, killApp, renderViaHTTP } from 'next-test-utils' import { fetchViaHTTP, findPort, killApp, renderViaHTTP } from 'next-test-utils'
...@@ -146,11 +145,6 @@ describe('Edge runtime - prod', () => { ...@@ -146,11 +145,6 @@ describe('Edge runtime - prod', () => {
expect(content.clientInfo).not.toContainEqual([['/404', true]]) expect(content.clientInfo).not.toContainEqual([['/404', true]])
}) })
it('should support React.lazy and dynamic imports', async () => {
const html = await renderViaHTTP(context.appPort, '/dynamic-imports')
expect(html).toContain('foo.client')
})
const options = { runtime: 'edge', env: 'prod' } const options = { runtime: 'edge', env: 'prod' }
basic(context, options) basic(context, options)
streaming(context, options) streaming(context, options)
...@@ -171,15 +165,6 @@ describe('Edge runtime - dev', () => { ...@@ -171,15 +165,6 @@ describe('Edge runtime - dev', () => {
await killApp(context.server) await killApp(context.server)
}) })
it('should support React.lazy and dynamic imports', async () => {
const html = await renderViaHTTP(context.appPort, '/dynamic-imports')
expect(html).toContain('loading...')
const browser = await webdriver(context.appPort, '/dynamic-imports')
const content = await browser.eval(`window.document.body.innerText`)
expect(content).toMatchInlineSnapshot('"foo.client"')
})
it('should have content-type and content-encoding headers', async () => { it('should have content-type and content-encoding headers', async () => {
const res = await fetchViaHTTP(context.appPort, '/') const res = await fetchViaHTTP(context.appPort, '/')
expect(res.headers.get('content-type')).toBe('text/html; charset=utf-8') expect(res.headers.get('content-type')).toBe('text/html; charset=utf-8')
......
/* eslint-env jest */ /* eslint-env jest */
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import cheerio from 'cheerio'
import { renderViaHTTP, check } from 'next-test-utils' import { renderViaHTTP, check } from 'next-test-utils'
import { join } from 'path' import { join } from 'path'
import fs from 'fs-extra' import fs from 'fs-extra'
import { distDir, getNodeBySelector } from './utils'
import { distDir } from './utils'
function getNodeBySelector(html, selector) {
const $ = cheerio.load(html)
return $(selector)
}
export default function (context, { runtime, env }) { export default function (context, { runtime, env }) {
it('should render server components correctly', async () => { it('should render server components correctly', async () => {
......
/* eslint-env jest */ /* eslint-env jest */
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { fetchViaHTTP, waitFor } from 'next-test-utils' import { fetchViaHTTP, waitFor } from 'next-test-utils'
import { getNodeBySelector } from './utils'
async function resolveStreamResponse(response, onData) { async function resolveStreamResponse(response, onData) {
let result = '' let result = ''
...@@ -218,6 +219,10 @@ export default function (context, { env, runtime }) { ...@@ -218,6 +219,10 @@ export default function (context, { env, runtime }) {
flushCount++ flushCount++
}) })
expect(flushCount).toBe(1) expect(flushCount).toBe(1)
const html = await res1.text()
const body = await getNodeBySelector(html, '#__next')
// Resolve data instead of fallback
expect(body.text()).toBe('next_streaming_data')
if (runtime === 'nodejs') { if (runtime === 'nodejs') {
expect(res1.headers.get('etag')).toBeDefined() expect(res1.headers.get('etag')).toBeDefined()
......
...@@ -5,6 +5,7 @@ import { ...@@ -5,6 +5,7 @@ import {
nextBuild as _nextBuild, nextBuild as _nextBuild,
nextStart as _nextStart, nextStart as _nextStart,
} from 'next-test-utils' } from 'next-test-utils'
import cheerio from 'cheerio'
const nodeArgs = ['-r', join(__dirname, '../../react-18/test/require-hook.js')] const nodeArgs = ['-r', join(__dirname, '../../react-18/test/require-hook.js')]
...@@ -44,3 +45,8 @@ export async function nextDev(dir, port) { ...@@ -44,3 +45,8 @@ export async function nextDev(dir, port) {
nodeArgs, nodeArgs,
}) })
} }
export function getNodeBySelector(html, selector) {
const $ = cheerio.load(html)
return $(selector)
}
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