Skip to content

Commit

Permalink
fix(gatsby): Render to pipeable stream in dev SSR (#37534)
Browse files Browse the repository at this point in the history
Fixes #36678
  • Loading branch information
panzacoder authored Jan 26, 2023
1 parent d5a10f8 commit 049e8e1
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions packages/gatsby/cache-dir/ssr-develop-static-entry.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* global BROWSER_ESM_ONLY */
import React from "react"
import fs from "fs-extra"
import { renderToString, renderToStaticMarkup } from "react-dom/server"
import { renderToStaticMarkup, renderToPipeableStream } from "react-dom/server"
import { get, merge, isObject, flatten, uniqBy, concat } from "lodash"
import nodePath from "path"
import { apiRunner, apiRunnerAsync } from "./api-runner-ssr"
Expand All @@ -12,6 +12,7 @@ import { RouteAnnouncerProps } from "./route-announcer-props"
import { ServerLocation, Router, isRedirect } from "@gatsbyjs/reach-router"
import { headHandlerForSSR } from "./head/head-export-handler-for-ssr"
import { getStaticQueryResults } from "./loader"
import { WritableAsPromise } from "./server-utils/writable-as-promise"

// prefer default export if available
const preferDefault = m => (m && m.default) || m
Expand Down Expand Up @@ -289,7 +290,17 @@ export default async function staticPage({
// If no one stepped up, we'll handle it.
if (!bodyHtml) {
try {
bodyHtml = renderToString(bodyComponent)
const writableStream = new WritableAsPromise()
const { pipe } = renderToPipeableStream(bodyComponent, {
onAllReady() {
pipe(writableStream)
},
onError(error) {
writableStream.destroy(error)
},
})

bodyHtml = await writableStream
} catch (e) {
// ignore @reach/router redirect errors
if (!isRedirect(e)) throw e
Expand Down

0 comments on commit 049e8e1

Please sign in to comment.