-
Notifications
You must be signed in to change notification settings - Fork 7
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 renderToPipeableStream wired up to a ReadableStream to add streaming support #2
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# .env.dev | ||
NODE_ENV=development |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,7 @@ import { DataProvider } from "./data"; | |
import { ABORT_DELAY, API_DELAY } from "./delays"; | ||
import { | ||
renderToPipeableStream, | ||
renderToReadableStream, | ||
} from "react-dom/server"; | ||
} from "react-dom/cjs/react-dom-server.node.development"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like this is using node specific code. Actually, that looks like that was already the case with renderToPipeableStream. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to provide a very specific path because we set the As for the node-specific code, it only uses |
||
|
||
/** | ||
* Streaming SSR in a Node.js runtime | ||
|
@@ -46,19 +45,57 @@ export function renderInNode({ res, head }) { | |
export function renderInWorkers({ head }) { | ||
const data = createServerData(); | ||
|
||
const proxy = { | ||
write(chunk) { | ||
this.controller.enqueue(chunk); | ||
// no backpressure support yet... | ||
return true; | ||
}, | ||
end() { | ||
this.controller.close(); | ||
}, | ||
destroy(err) { | ||
this.controller.error(err); | ||
}, | ||
on() {} | ||
}; | ||
|
||
let didError = false; | ||
try { | ||
const {pipe, abort} = renderToPipeableStream( | ||
<DataProvider data={data}> | ||
<App head={head} /> | ||
</DataProvider>, | ||
{ | ||
onCompleteShell() { | ||
pipe(proxy); | ||
}, | ||
onError(x) { | ||
didError = true; | ||
console.error(x); | ||
}, | ||
} | ||
); | ||
proxy.abort = (reason) => { | ||
console.error("Proxy aborted:", reason); | ||
abort(); | ||
} | ||
} catch(e) { | ||
console.error("renderToPipeableStream failed:", e); | ||
} | ||
|
||
const stream = renderToReadableStream( | ||
<DataProvider data={data}> | ||
<App head={head} /> | ||
</DataProvider>, | ||
{ | ||
onError(x) { | ||
didError = true; | ||
console.error(x); | ||
}, | ||
if (didError) { | ||
throw new Error("renderToPipeableStream failed right after start"); | ||
} | ||
|
||
const stream = new ReadableStream({ | ||
start: function (controller) { | ||
proxy.controller = controller; | ||
}, | ||
cancel: function(reason) { | ||
proxy.abort(reason); | ||
} | ||
); | ||
}); | ||
|
||
return new Response(stream, { | ||
headers: { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1104,14 +1104,14 @@ queue-microtask@^1.2.2: | |
resolved "https://registry.yarnpkg.com/queue-microtask/-/queue-microtask-1.2.3.tgz#4929228bbc724dfac43e0efb058caf7b6cfb6243" | ||
integrity sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A== | ||
|
||
react-dom@^18.0.0-beta-96ca8d915-20211115: | ||
version "18.0.0-beta-96ca8d915-20211115" | ||
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-18.0.0-beta-96ca8d915-20211115.tgz#511a754eddbb760c83d85251249fe9c52522326e" | ||
integrity sha512-ISFQGQ0lHt+fz/Ch9K9coggTWzp9ScfuvzdshGJ73z5AI7mi9i6mVjVekMDnx0wiECjHP1s87rxNDQGGj9XnHw== | ||
[email protected]c1220ebdd-20211123: | ||
version "18.0.0-beta-c1220ebdd-20211123" | ||
resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-18.0.0-beta-c1220ebdd-20211123.tgz#e98d0e28cfc3665730b2a7abf00c087cdd41abeb" | ||
integrity sha512-kY++te33IPgLbuRlVrm5uYrxla9y1lkW/icHNHD1zeSa4x0rKOqyW01HLBJPT6bN+PT89b98I5I/Qr0CXN+w6Q== | ||
dependencies: | ||
loose-envify "^1.1.0" | ||
object-assign "^4.1.1" | ||
scheduler "0.21.0-beta-96ca8d915-20211115" | ||
scheduler "0.21.0-beta-c1220ebdd-20211123" | ||
|
||
react-error-boundary@^3.1.4: | ||
version "3.1.4" | ||
|
@@ -1125,10 +1125,10 @@ react-refresh@^0.10.0: | |
resolved "https://registry.yarnpkg.com/react-refresh/-/react-refresh-0.10.0.tgz#2f536c9660c0b9b1d500684d9e52a65e7404f7e3" | ||
integrity sha512-PgidR3wST3dDYKr6b4pJoqQFpPGNKDSCDx4cZoshjXipw3LzO7mG1My2pwEzz2JVkF+inx3xRpDeQLFQGH/hsQ== | ||
|
||
react@^18.0.0-beta-96ca8d915-20211115: | ||
version "18.0.0-beta-96ca8d915-20211115" | ||
resolved "https://registry.yarnpkg.com/react/-/react-18.0.0-beta-96ca8d915-20211115.tgz#0b2a0cd808b85fdce987a0c254e0fab979a3828d" | ||
integrity sha512-Io6qvToVSO1UWNWbJSH16gbgYTkseSbfpQvLaB7JPutt0QVvjpUlEE0nfmww77CFdawxGkyfm/x4zvNTlMkHsw== | ||
[email protected]c1220ebdd-20211123: | ||
version "18.0.0-beta-c1220ebdd-20211123" | ||
resolved "https://registry.yarnpkg.com/react/-/react-18.0.0-beta-c1220ebdd-20211123.tgz#1577d2e59dfde826e110074c5f20160e0c1d36d9" | ||
integrity sha512-yP8Lvre7RS0TXFYEyRkijpaqilwM0kMG1iEkoIWgeNyjBDMeKjmqKDFCNtXoUca0WdJ0Y+DvmjKPce5XksJMaA== | ||
dependencies: | ||
loose-envify "^1.1.0" | ||
object-assign "^4.1.1" | ||
|
@@ -1211,10 +1211,10 @@ sanitize-filename@^1.6.3: | |
dependencies: | ||
truncate-utf8-bytes "^1.0.0" | ||
|
||
[email protected]96ca8d915-20211115: | ||
version "0.21.0-beta-96ca8d915-20211115" | ||
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.21.0-beta-96ca8d915-20211115.tgz#b15758da1f33734ab1219aa5147ea79037a42013" | ||
integrity sha512-gm2zVVyjuIHlcqdAv57Tk/lFBf7+RDhYvIEaOZiKf3/YpIZbsiMnfy6yP6+VR3pS//V2LY5Mv8bf6fS+4IOWbg== | ||
[email protected]c1220ebdd-20211123: | ||
version "0.21.0-beta-c1220ebdd-20211123" | ||
resolved "https://registry.yarnpkg.com/scheduler/-/scheduler-0.21.0-beta-c1220ebdd-20211123.tgz#6a4c3cc426f4316e5f0df448b01e99b364aa7fc7" | ||
integrity sha512-3qOlWLIx2gMB5FUgCuycE7vQhXyoTsy5VF9elrsXOSvpM7j4NMYYRf9ehjsllQK+8sSmmAlXHx/8kdcA500tdA== | ||
dependencies: | ||
loose-envify "^1.1.0" | ||
object-assign "^4.1.1" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, perhaps we could get react to not depend on it. If not, another approach would be to have hydrogen polyfill it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already have
Buffer
in Oxygen, because it was a dependency of some other polyfill, it's just not exposed. So we can expose it and it will work.