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(remix-react): fix false positive resource route identification #6125

Merged
merged 3 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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/resource-route-boundary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/react": patch
---

Fix false-positive resource route identification if a route only exports a boundary
65 changes: 65 additions & 0 deletions integration/resource-routes-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,68 @@ test.describe("loader in an app", async () => {
);
});
});

test.describe("Development server", async () => {
let appFixture: AppFixture;
let fixture: Fixture;
let _consoleError: typeof console.error;

test.beforeAll(async () => {
_consoleError = console.error;
console.error = () => {};

fixture = await createFixture(
{
future: {
v2_routeConvention: true,
v2_errorBoundary: true,
},
files: {
"app/routes/_index.jsx": js`
import { Link } from "@remix-run/react";
export default () => <Link to="/child">Child</Link>;
`,
"app/routes/_main.jsx": js`
import { useRouteError } from "@remix-run/react";
export function ErrorBoundary() {
return <pre>{useRouteError().message}</pre>;
}
`,
"app/routes/_main.child.jsx": js`
export default function Component() {
throw new Error('Error from render')
}
`,
},
},
ServerMode.Development
);
appFixture = await createAppFixture(fixture, ServerMode.Development);
});

test.afterAll(() => {
appFixture.close();
console.error = _consoleError;
});

test.describe("with JavaScript", () => {
runTests();
});

test.describe("without JavaScript", () => {
test.use({ javaScriptEnabled: false });
runTests();
});

function runTests() {
test("should not treat an ErrorBoundary-only route as a resource route", async ({
page,
}) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/child");
let html = await app.getHtml();
expect(html).not.toMatch("has no component");
expect(html).toMatch("Error from render");
});
}
});
13 changes: 11 additions & 2 deletions packages/remix-react/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
Await as AwaitRR,
Link as RouterLink,
NavLink as RouterNavLink,
Outlet,
UNSAFE_DataRouterContext as DataRouterContext,
UNSAFE_DataRouterStateContext as DataRouterStateContext,
isRouteErrorResponse,
Expand Down Expand Up @@ -113,15 +114,23 @@ function useRemixContext(): RemixContextObject {
// RemixRoute

export function RemixRoute({ id }: { id: string }) {
let { routeModules } = useRemixContext();
let { routeModules, future } = useRemixContext();

invariant(
routeModules,
"Cannot initialize 'routeModules'. This normally occurs when you have server code in your client modules.\n" +
"Check this link for more details:\nhttps://remix.run/pages/gotchas#server-code-in-client-bundles"
);

let { default: Component } = routeModules[id];
let { default: Component, ErrorBoundary, CatchBoundary } = routeModules[id];

// Default Component to Outlet if we expose boundary UI components
if (
!Component &&
(ErrorBoundary || (!future.v2_errorBoundary && CatchBoundary))
) {
Component = Outlet;
}

invariant(
Component,
Expand Down