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

Rename meta types for consistency #378

Merged
merged 2 commits into from
Nov 19, 2021
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
26 changes: 26 additions & 0 deletions docs/api/conventions.md
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,8 @@ There are two types of link descriptors you can return:

This is an object representation of a normal `<link {...props} />` element. [View the MDN docs for the link API][link-tag].

The `links` export from a route should return an array of `HtmlLinkDescriptor` objects.

Examples:

```tsx
Expand Down Expand Up @@ -710,6 +712,30 @@ export let links: LinksFunction = () => {
};
```

#### HtmlMetaDescriptor

This is an object representation and abstraction of a `<meta {...props} />` element and its attributes. [View the MDN docs for the meta API](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta).

The `meta` export from a route should return a single `HtmlMetaDescriptor` object.

Almost every `meta` element takes a `name` and `content` attribute, with the exception of [OpenGraph tags](https://ogp.me/) which use `property` instead of `name`. In either case, the attributes represent a key/value pair for each tag. Each pair in the `HtmlMetaDescriptor` object represents a separate `meta` element, and Remix maps each to the correct attributes for that tag.

The `meta` object can also hold a `title` reference which maps to the [HTML `<title>` element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/title)

Examples:

```tsx
import type { MetaFunction } from "remix";

export let meta: MetaFunction = () => {
return {
title: "Josie's Shake Shack", // <title>Josie's Shake Shack</title>
description: "Delicious shakes", // <meta name="description" content="Delicious shakes">
"og:image": "https://josiesshakeshack.com/logo.jpg" // <meta property="og:image" content="https://josiesshakeshack.com/logo.jpg">
};
};
```

#### `PageLinkDescriptor`

These descriptors allow you to prefetch the resources for a page the user is likely to navigate to. While this API is useful, you might get more mileage out of `<Link prefetch="render">` instead. But if you'd like, you can get the same behavior with this API.
Expand Down
4 changes: 2 additions & 2 deletions packages/remix-react/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import { createClientRoutes } from "./routes";
import type { RouteData } from "./routeData";
import type { RouteMatch } from "./routeMatching";
import { matchClientRoutes } from "./routeMatching";
import type { RouteModules, MetaDescriptor } from "./routeModules";
import type { RouteModules, HtmlMetaDescriptor } from "./routeModules";
import { createTransitionManager } from "./transition";
import type { Transition, Fetcher, Submission } from "./transition";

Expand Down Expand Up @@ -614,7 +614,7 @@ export function Meta() {
let { matches, routeData, routeModules } = useRemixEntryContext();
let location = useLocation();

let meta: MetaDescriptor = {};
let meta: HtmlMetaDescriptor = {};
let parentsData: { [routeId: string]: AppData } = {};

for (let match of matches) {
Expand Down
3 changes: 2 additions & 1 deletion packages/remix-react/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export type { FormMethod, FormEncType } from "./data";
export type { ThrownResponse } from "./errors";
export { useCatch } from "./errorBoundaries";

export type { ShouldReloadFunction } from "./routeModules";
export type { HtmlLinkDescriptor } from "./links";
export type { ShouldReloadFunction, HtmlMetaDescriptor } from "./routeModules";

export type { RemixServerProps } from "./server";
export { RemixServer } from "./server";
6 changes: 3 additions & 3 deletions packages/remix-react/routeModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface RouteModule {
default: RouteComponent;
handle?: RouteHandle;
links?: LinksFunction;
meta?: MetaFunction | MetaDescriptor;
meta?: MetaFunction | HtmlMetaDescriptor;
unstable_shouldReload?: ShouldReloadFunction;
}

Expand Down Expand Up @@ -51,7 +51,7 @@ export interface MetaFunction {
parentsData: RouteData;
params: Params;
location: Location;
}): MetaDescriptor;
}): HtmlMetaDescriptor;
}

/**
Expand All @@ -60,7 +60,7 @@ export interface MetaFunction {
* tag, or an array of strings that will render multiple tags with the same
* `name` attribute.
*/
export interface MetaDescriptor {
export interface HtmlMetaDescriptor {
[name: string]: string | string[];
}

Expand Down
5 changes: 3 additions & 2 deletions packages/remix-server-runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type { EntryContext } from "./entry";

export type {
LinkDescriptor,
HTMLLinkDescriptor,
HtmlLinkDescriptor,
PageLinkDescriptor
} from "./links";

Expand All @@ -31,10 +31,11 @@ export type {
DataFunctionArgs,
ErrorBoundaryComponent,
HeadersFunction,
HtmlMetaDescriptor,
LinksFunction,
LoaderFunction,
MetaFunction,
MetaDescriptor,
MetaFunction,
RouteComponent,
RouteHandle
} from "./routeModules";
Expand Down
6 changes: 3 additions & 3 deletions packages/remix-server-runtime/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* WHATWG Specification: https://html.spec.whatwg.org/multipage/semantics.html#the-link-element
*/
export interface HTMLLinkDescriptor {
export interface HtmlLinkDescriptor {
/**
* Address of the hyperlink
*/
Expand Down Expand Up @@ -127,7 +127,7 @@ export interface HTMLLinkDescriptor {

export interface PageLinkDescriptor
extends Omit<
HTMLLinkDescriptor,
HtmlLinkDescriptor,
| "href"
| "rel"
| "type"
Expand All @@ -144,4 +144,4 @@ export interface PageLinkDescriptor
page: string;
}

export type LinkDescriptor = HTMLLinkDescriptor | PageLinkDescriptor;
export type LinkDescriptor = HtmlLinkDescriptor | PageLinkDescriptor;
3 changes: 2 additions & 1 deletion packages/remix-server-runtime/magicExports/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ export type {
AppData,
EntryContext,
LinkDescriptor,
HTMLLinkDescriptor,
HtmlLinkDescriptor,
PageLinkDescriptor,
ErrorBoundaryComponent,
ActionFunction,
HeadersFunction,
LinksFunction,
LoaderFunction,
MetaDescriptor,
HtmlMetaDescriptor,
MetaFunction,
RouteComponent,
RouteHandle,
Expand Down
8 changes: 5 additions & 3 deletions packages/remix-server-runtime/routeModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export interface MetaFunction {
parentsData: RouteData;
params: Params;
location: Location;
}): MetaDescriptor;
}): HtmlMetaDescriptor;
}

/**
Expand All @@ -91,10 +91,12 @@ export interface MetaFunction {
* tag, or an array of strings that will render multiple tags with the same
* `name` attribute.
*/
export interface MetaDescriptor {
export interface HtmlMetaDescriptor {
[name: string]: string | string[];
}

export type MetaDescriptor = HtmlMetaDescriptor;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This alias is for consistency with LinkDescriptor, even though HtmlMetaDescriptor will only ever be HtmlMetaDescriptor (probably). Gives us a little wiggle room if we ever change that.


/**
* A React component that is rendered for a route.
*/
Expand All @@ -111,7 +113,7 @@ export interface EntryRouteModule {
default: RouteComponent;
handle?: RouteHandle;
links?: LinksFunction;
meta?: MetaFunction | MetaDescriptor;
meta?: MetaFunction | HtmlMetaDescriptor;
}

export interface ServerRouteModule extends EntryRouteModule {
Expand Down