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

Root router have a path different to '' or '/' #10852

Merged
merged 8 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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/404-root-with-path.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

Allow 404 detection to leverage root route error boundary if path contains a URL segment
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,5 @@
- xcsnowcity
- yionr
- yuleicul
- yracnet
- zheng-chuang
83 changes: 81 additions & 2 deletions packages/router/__tests__/router-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,13 @@ function setup({
// active navigation loader/action
function enhanceRoutes(_routes: TestRouteObject[]) {
return _routes.map((r) => {
let enhancedRoute: AgnosticDataRouteObject = {
let enhancedRoute: AgnosticRouteObject = {
...r,
lazy: undefined,
loader: undefined,
action: undefined,
children: undefined,
id: r.id || `route-${guid++}`,
id: r.id,
};
if (r.lazy) {
// @ts-expect-error
Expand Down Expand Up @@ -4817,6 +4817,85 @@ describe("a router", () => {
});
});

it("handles 404 routes when the root route contains a path (initialization)", () => {
let t = setup({
routes: [
{
id: "root",
path: "/path",
children: [
{
index: true,
},
],
},
],
initialEntries: ["/junk"],
});
expect(t.router.state).toMatchObject({
errors: {
root: new ErrorResponse(
404,
"Not Found",
new Error('No route matches URL "/junk"'),
true
),
},
initialized: true,
location: {
pathname: "/junk",
},
matches: [
{
route: {
id: "root",
},
},
],
});
});

it("handles 404 routes when the root route contains a path (navigation)", () => {
let t = setup({
routes: [
{
id: "root",
path: "/path",
children: [
{
index: true,
},
],
},
],
initialEntries: ["/path"],
});
expect(t.router.state).toMatchObject({
errors: null,
});
t.navigate("/junk");
expect(t.router.state).toMatchObject({
errors: {
root: new ErrorResponse(
404,
"Not Found",
new Error('No route matches URL "/junk"'),
true
),
},
location: {
pathname: "/junk",
},
matches: [
{
route: {
id: "root",
},
},
],
});
});

it("converts formData to URLSearchParams for unspecified formMethod", async () => {
let t = setup({
routes: TASK_ROUTES,
Expand Down
9 changes: 6 additions & 3 deletions packages/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4075,9 +4075,12 @@ function getShortCircuitMatches(routes: AgnosticDataRouteObject[]): {
route: AgnosticDataRouteObject;
} {
// Prefer a root layout route if present, otherwise shim in a route object
let route = routes.find((r) => r.index || !r.path || r.path === "/") || {
id: `__shim-error-route__`,
};
let route =
routes.length === 1
? routes[0]
: routes.find((r) => r.index || !r.path || r.path === "/") || {
id: `__shim-error-route__`,
};

return {
matches: [
Expand Down
Loading