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

Use zen-observable library #28214

Merged
merged 15 commits into from Aug 18, 2021
Expand Up @@ -11,7 +11,7 @@ import { setLazyProp, getCookieParser } from '../../../../server/api-utils'
import { getRedirectStatus } from '../../../../lib/load-custom-routes'
import getRouteNoAssetPath from '../../../../shared/lib/router/utils/get-route-from-asset-path'
import { PERMANENT_REDIRECT_STATUS } from '../../../../shared/lib/constants'
import { resultToChunks } from '../../../../server/utils'
import { resultsToString } from '../../../../server/utils'

export function getPageHandler(ctx: ServerlessHandlerCtx) {
const {
Expand Down Expand Up @@ -334,7 +334,7 @@ export function getPageHandler(ctx: ServerlessHandlerCtx) {
defaultLocale: i18n?.defaultLocale,
})
)
const html = result2 ? (await resultToChunks(result2)).join('') : ''
const html = result2 ? await resultsToString([result2]) : ''
sendPayload(
req,
res,
Expand Down Expand Up @@ -402,7 +402,7 @@ export function getPageHandler(ctx: ServerlessHandlerCtx) {
}

if (renderMode) return { html: result, renderOpts }
return result ? (await resultToChunks(result)).join('') : null
return result ? await resultsToString([result]) : null
} catch (err) {
if (!parsedUrl!) {
parsedUrl = parseUrl(req.url!, true)
Expand Down Expand Up @@ -464,7 +464,7 @@ export function getPageHandler(ctx: ServerlessHandlerCtx) {
err: res.statusCode === 404 ? undefined : err,
})
)
return result2 ? (await resultToChunks(result2)).join('') : null
return result2 ? await resultsToString([result2]) : null
}
}

Expand Down
15 changes: 8 additions & 7 deletions packages/next/export/worker.ts
Expand Up @@ -18,9 +18,10 @@ import { FontManifest } from '../server/font-utils'
import { normalizeLocalePath } from '../shared/lib/i18n/normalize-locale-path'
import { trace } from '../telemetry/trace'
import { isInAmpMode } from '../shared/lib/amp'
import { resultFromChunks, resultToChunks } from '../server/utils'
import { resultsToString } from '../server/utils'
import { NextConfigComplete } from '../server/config-shared'
import { setHttpAgentOptions } from '../server/config'
import Observable from 'zen-observable'

const envConfig = require('../shared/lib/runtime-config')

Expand Down Expand Up @@ -274,7 +275,7 @@ export default async function exportPage({

// if it was auto-exported the HTML is loaded here
if (typeof mod === 'string') {
renderResult = resultFromChunks([mod])
renderResult = Observable.of(mod)
queryWithAutoExportWarn()
} else {
// for non-dynamic SSG pages we should have already
Expand Down Expand Up @@ -352,7 +353,7 @@ export default async function exportPage({
}

if (typeof components.Component === 'string') {
renderResult = resultFromChunks([components.Component])
renderResult = Observable.of(components.Component)
queryWithAutoExportWarn()
} else {
/**
Expand Down Expand Up @@ -417,8 +418,7 @@ export default async function exportPage({
}
}

const htmlChunks = renderResult ? await resultToChunks(renderResult) : []
const html = htmlChunks.join('')
const html = renderResult ? await resultsToString([renderResult]) : ''
if (inAmpMode && !curRenderOpts.ampSkipValidation) {
if (!results.ssgNotFound) {
await validateAmp(html, path, curRenderOpts.ampValidatorPath)
Expand Down Expand Up @@ -460,8 +460,9 @@ export default async function exportPage({
)
}

const ampChunks = await resultToChunks(ampRenderResult)
const ampHtml = ampChunks.join('')
const ampHtml = ampRenderResult
? await resultsToString([ampRenderResult])
: ''
if (!curRenderOpts.ampSkipValidation) {
await validateAmp(ampHtml, page + '?amp=1')
}
Expand Down
4 changes: 3 additions & 1 deletion packages/next/package.json
Expand Up @@ -112,7 +112,8 @@
"use-subscription": "1.5.1",
"util": "0.12.4",
"vm-browserify": "1.1.2",
"watchpack": "2.1.1"
"watchpack": "2.1.1",
"zen-observable": "0.8.15"
},
"peerDependencies": {
"fibers": ">= 3.1.0",
Expand Down Expand Up @@ -185,6 +186,7 @@
"@types/text-table": "0.2.1",
"@types/webpack": "5.28.0",
"@types/webpack-sources": "0.1.5",
"@types/zen-observable": "0.8.3",
"@vercel/ncc": "0.27.0",
"@vercel/nft": "0.12.2",
"amphtml-validator": "1.0.33",
Expand Down
23 changes: 9 additions & 14 deletions packages/next/server/next-server.ts
Expand Up @@ -76,12 +76,7 @@ import { sendRenderResult, setRevalidateHeaders } from './send-payload'
import { serveStatic } from './serve-static'
import { IncrementalCache } from './incremental-cache'
import { execOnce } from '../shared/lib/utils'
import {
isBlockedPage,
RenderResult,
resultFromChunks,
resultToChunks,
} from './utils'
import { isBlockedPage, RenderResult, resultsToString } from './utils'
import { loadEnvConfig } from '@next/env'
import './node-polyfill-fetch'
import { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
Expand All @@ -102,6 +97,7 @@ import ResponseCache, {
} from './response-cache'
import { NextConfigComplete } from './config-shared'
import { parseNextUrl } from '../shared/lib/router/utils/parse-next-url'
import Observable from 'zen-observable'
devknoll marked this conversation as resolved.
Show resolved Hide resolved

const getCustomRouteMatcher = pathMatch(true)

Expand Down Expand Up @@ -1267,7 +1263,7 @@ export default class Server {
req,
res,
resultOrPayload: requireStaticHTML
? (await resultToChunks(body)).join('')
? await resultsToString([body])
: body,
type,
generateEtags,
Expand Down Expand Up @@ -1296,8 +1292,7 @@ export default class Server {
if (payload === null) {
return null
}
const chunks = await resultToChunks(payload.body)
return chunks.join('')
return resultsToString([payload.body])
}

public async render(
Expand Down Expand Up @@ -1472,7 +1467,7 @@ export default class Server {
return {
type: 'html',
// TODO: Static pages should be written as chunks
body: resultFromChunks([components.Component]),
body: Observable.of(components.Component),
}
}

Expand Down Expand Up @@ -1751,7 +1746,7 @@ export default class Server {
return {
value: {
kind: 'PAGE',
html: resultFromChunks([html]),
html: Observable.of(html),
pageData: {},
},
}
Expand Down Expand Up @@ -1830,7 +1825,7 @@ export default class Server {
if (isDataReq) {
return {
type: 'json',
body: resultFromChunks([JSON.stringify(cachedData.props)]),
body: Observable.of(JSON.stringify(cachedData.props)),
revalidateOptions,
}
} else {
Expand All @@ -1841,7 +1836,7 @@ export default class Server {
return {
type: isDataReq ? 'json' : 'html',
body: isDataReq
? resultFromChunks([JSON.stringify(cachedData.pageData)])
? Observable.of(JSON.stringify(cachedData.pageData))
: cachedData.html,
revalidateOptions,
}
Expand Down Expand Up @@ -2076,7 +2071,7 @@ export default class Server {
}
return {
type: 'html',
body: resultFromChunks(['Internal Server Error']),
body: Observable.of('Internal Server Error'),
}
}
}
Expand Down