Skip to content

Commit

Permalink
feat(meta): add thrown error to the meta params (#7105)
Browse files Browse the repository at this point in the history
Co-authored-by: Matt Brophy <[email protected]>
  • Loading branch information
ryanflorence and brophdawg11 authored Aug 16, 2023
1 parent 43f9262 commit 65d3345
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 6 deletions.
11 changes: 11 additions & 0 deletions .changeset/add-error-to-meta.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"@remix-run/react": patch
---

Add error to meta params so you can render error titles, etc.

```tsx
export function meta({ error }) {
return [{ title: error.message }]
}
```
10 changes: 10 additions & 0 deletions docs/route/meta.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ export const meta: MetaFunction<typeof loader> = ({

The route's URL params. See [Dynamic Segments in the Routing Guide][url-params].

### `error`

Thrown errors that trigger error boundaries will be passed to the `meta` function. This is useful for generating metadata for error pages.

```tsx
export const meta: MetaFunction = ({ error }) => {
return [{ title: error ? "oops!" : "Actual title" }];
};
```

## Accessing Data from Parent Route Loaders

In addition to the current route's data, often you'll want to access data from a route higher up in the route hierarchy. You can look it up by its route ID in `matches`.
Expand Down
39 changes: 39 additions & 0 deletions integration/meta-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,26 @@ test.describe("meta", () => {
return <h1>Music</h1>;
}
`,

"app/routes/error.tsx": js`
import { Link, useRouteError } from '@remix-run/react'
export function loader() {
throw new Error('lol oops')
}
export const meta = (args) => {
return [{ title: args.error ? "Oops!" : "Home"}]
}
export default function Error() {
return <h1>Error</h1>
}
export function ErrorBoundary() {
return <h1>Error boundary</h1>
}
`,
},
});
appFixture = await createAppFixture(fixture);
Expand Down Expand Up @@ -231,4 +251,23 @@ test.describe("meta", () => {
await app.goto("/authors/1");
expect(await app.getHtml('link[rel="canonical"]')).toBeTruthy();
});

test("loader errors are passed to meta", async ({ page }) => {
let restoreErrors = hideErrors();

new PlaywrightFixture(appFixture, page);
let response = await fixture.requestDocument("/error");
expect(await response.text()).toMatch("<title>Oops!</title>");

restoreErrors();
});
});

function hideErrors() {
let oldConsoleError: any;
oldConsoleError = console.error;
console.error = () => {};
return () => {
console.error = oldConsoleError;
};
}
15 changes: 9 additions & 6 deletions packages/remix-react/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -482,12 +482,13 @@ export function Meta() {
} = useDataRouterStateContext();
let location = useLocation();

let _matches = errors
? routerMatches.slice(
0,
routerMatches.findIndex((m) => errors![m.route.id]) + 1
)
: routerMatches;
let _matches: AgnosticDataRouteMatch[] = routerMatches;
let error: any = null;
if (errors) {
let errorIdx = routerMatches.findIndex((m) => errors![m.route.id]);
_matches = routerMatches.slice(0, errorIdx + 1);
error = errors[routerMatches[errorIdx].route.id];
}

let meta: MetaDescriptor[] = [];
let leafMeta: MetaDescriptor[] | null = null;
Expand All @@ -507,6 +508,7 @@ export function Meta() {
params: _match.params,
pathname: _match.pathname,
handle: _match.route.handle,
error,
};
matches[i] = match;

Expand All @@ -518,6 +520,7 @@ export function Meta() {
params,
location,
matches,
error,
})
: Array.isArray(routeModule.meta)
? [...routeModule.meta]
Expand Down
2 changes: 2 additions & 0 deletions packages/remix-react/routeModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export interface MetaMatch<
handle?: unknown;
params: DataRouteMatch["params"];
meta: MetaDescriptor[];
error?: unknown;
}

export type MetaMatches<
Expand All @@ -74,6 +75,7 @@ export interface MetaArgs<
params: Params;
location: Location;
matches: MetaMatches<MatchLoaders>;
error?: unknown;
}

export interface MetaFunction<
Expand Down

0 comments on commit 65d3345

Please sign in to comment.