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

feat(meta): add thrown error to the meta params #7105

Merged
merged 4 commits into from
Aug 16, 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
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 Index() {
return <h1>Home</h1>
}

export function ErrorBoundary() {
return <h1>Home 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;
};
}
4 changes: 4 additions & 0 deletions packages/remix-react/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,8 @@ export function Meta() {
let meta: MetaDescriptor[] = [];
let leafMeta: MetaDescriptor[] | null = null;
let matches: MetaMatches = [];
let errorMatch = matches.find((m) => errors && errors[m.id] != null);
let error = errors && errorMatch ? errors[errorMatch.id] : null;
for (let i = 0; i < _matches.length; i++) {
let _match = _matches[i];
let routeId = _match.route.id;
Expand All @@ -507,6 +509,7 @@ export function Meta() {
params: _match.params,
pathname: _match.pathname,
handle: _match.route.handle,
error,
};
matches[i] = match;

Expand All @@ -518,6 +521,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