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

fix: account for presence of Layout in default error boundary #8859

Merged
merged 1 commit into from
Feb 22, 2024
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/layout-error-boundary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/react": patch
---

account for presence of Layout in default error boundary
40 changes: 40 additions & 0 deletions integration/root-route-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,44 @@ test.describe("root route", () => {

console.error = oldConsoleError;
});

test("renders the Layout around the default ErrorBoundary", async ({ page }) => {
let oldConsoleError;
oldConsoleError = console.error;
console.error = () => {};

fixture = await createFixture(
{
files: {
"app/root.tsx": js`
export function Layout({ children }) {
return (
<html>
<head>
<title>Layout Title</title>
</head>
<body>
{children}
</body>
</html>
);
}
export default function Root() {
throw new Error('broken render')
}
`,
},
},
ServerMode.Development
);
appFixture = await createAppFixture(fixture, ServerMode.Development);
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
await page.waitForSelector("h1");
console.log(await app.getHtml())
expect(await app.getHtml("title")).toMatch("Layout Title");
expect(await app.getHtml("h1")).toMatch("Application Error");

console.error = oldConsoleError;
});
});
42 changes: 29 additions & 13 deletions packages/remix-react/errorBoundaries.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import * as React from "react";
import type { Location } from "@remix-run/router";
import { isRouteErrorResponse } from "react-router-dom";

import { useRemixContext } from "./components"

type RemixErrorBoundaryProps = React.PropsWithChildren<{
location: Location;
error?: Error;
Expand Down Expand Up @@ -67,9 +69,11 @@ export function RemixRootDefaultErrorBoundary({ error }: { error: unknown }) {
if (isRouteErrorResponse(error)) {
return (
<BoundaryShell title="Unhandled Thrown Response!">
<h1 style={{ fontFamily: "system-ui, sans-serif", padding: "2rem" }}>
{error.status} {error.statusText}
</h1>
<main style={{ fontFamily: "system-ui, sans-serif", padding: "2rem" }}>
<h1 style={{ fontSize: "24px" }}>
{error.status} {error.statusText}
</h1>
</main>
</BoundaryShell>
);
}
Expand Down Expand Up @@ -113,6 +117,27 @@ function BoundaryShell({
title: string;
children: React.ReactNode;
}) {
let { routeModules } = useRemixContext();

let contents = (
<>
{children}
<script
dangerouslySetInnerHTML={{
__html: `
console.log(
"💿 Hey developer 👋. You can provide a way better UX than this when your app throws errors. Check out https://remix.run/guides/errors for more information."
);
`,
}}
/>
</>
);

if (routeModules.root?.Layout) {
return contents;
}

return (
<html lang="en">
<head>
Expand All @@ -124,16 +149,7 @@ function BoundaryShell({
<title>{title}</title>
</head>
<body>
{children}
<script
dangerouslySetInnerHTML={{
__html: `
console.log(
"💿 Hey developer 👋. You can provide a way better UX than this when your app throws errors. Check out https://remix.run/guides/errors for more information."
);
`,
}}
/>
{contents}
</body>
</html>
);
Expand Down