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

Prevent errors in React components from crashing the dev server #4816

Merged
merged 3 commits into from
Sep 20, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tricky-walls-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/react': patch
---

Prevent errors in React components from crashing the dev server
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import ThrowsAnError from "./ThrowsAnError";

export default function() {
return <>
<ThrowsAnError />
</>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useState } from 'react';

export default function() {
let player = undefined;
// This is tested in dev mode, so make it work during the build to prevent
// breaking other tests.
if(import.meta.env.MODE === 'production') {
player = {};
}
const [] = useState(player.currentTime || null);

return (
<div>Should have thrown</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
import ImportsThrowsAnError from '../components/ImportsThrowsAnError';
---
<html>
<head>
<title>Testing</title>
</head>
<body>
<ImportsThrowsAnError />
</body>
</html>
6 changes: 6 additions & 0 deletions packages/astro/test/react-component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ describe('React Components', () => {
if (isWindows) return;

describe('dev', () => {
/** @type {import('./test-utils').Fixture} */
let devServer;

before(async () => {
Expand Down Expand Up @@ -145,5 +146,10 @@ describe('React Components', () => {
// test 1: react/jsx-runtime is used for the component
expect(jsxRuntime).to.be.ok;
});

it('When a nested component throws it does not crash the server', async () => {
const res = await fixture.fetch('/error-rendering');
await res.arrayBuffer();
});
});
});
5 changes: 4 additions & 1 deletion packages/integrations/react/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,11 @@ async function renderToPipeableStreamAsync(vnode) {
async function renderToStaticNodeStreamAsync(vnode) {
const Writable = await getNodeWritable();
let html = '';
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
let stream = ReactDOM.renderToStaticNodeStream(vnode);
stream.on('error', err => {
reject(err);
});
stream.pipe(
new Writable({
write(chunk, _encoding, callback) {
Expand Down