Skip to content

Commit

Permalink
Slight reorg of the RouteModule types
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed Dec 16, 2024
1 parent 3be44ec commit 6352d2e
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 65 deletions.
5 changes: 5 additions & 0 deletions .changeset/slimy-cows-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router": patch
---

Internal reorg of the `RouteModule` interfaces for deduplication and documentation purposes
8 changes: 3 additions & 5 deletions packages/react-router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,13 @@ export type {
ClientActionFunctionArgs,
ClientLoaderFunction,
ClientLoaderFunctionArgs,
HeadersArgs,
HeadersFunction,
MetaArgs,
MetaDescriptor,
MetaFunction,
LinksFunction,
ServerRouteModule,
} from "./lib/dom/ssr/routeModules";
export type { ServerRouterProps } from "./lib/dom/ssr/server";
export { ServerRouter } from "./lib/dom/ssr/server";
Expand Down Expand Up @@ -251,11 +254,6 @@ export type {
LinkDescriptor,
} from "./lib/router/links";

export type {
HeadersArgs,
HeadersFunction,
} from "./lib/server-runtime/routeModules";

export type { RequestHandler } from "./lib/server-runtime/server";

export type {
Expand Down
83 changes: 83 additions & 0 deletions packages/react-router/lib/dom/ssr/routeModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,84 @@ export interface RouteModules {
[routeId: string]: RouteModule | undefined;
}

/**
* The shape of a route module shipped to the client
*/
export interface RouteModule {
/**
* Action function called only in the browser. Route client loaders provide
* data to route components in addition to, or in place of, route loaders.
*/
clientAction?: ClientActionFunction;
/**
* Like route actions but only called in the browser.
*/
clientLoader?: ClientLoaderFunction;
/**
* When other route module APIs throw, the route module `ErrorBoundary` will
* render instead of the route component.
*/
ErrorBoundary?: ErrorBoundaryComponent;
/**
* On initial page load, the route component renders only after the client
* loader is finished. If exported, a `HydrateFallback` can render
* immediately in place of the route component.
*/
HydrateFallback?: HydrateFallbackComponent;
/**
* The `Layout` export is only applicable to the root route. Because the root
* route manages the document for all routes, it supports an additional optional
* `Layout` export that will be wrapped around the rendered UI, which may come
* from the default `Component` export, the `ErrorBoundary`, or the `HydrateFallback`
*/
Layout?: LayoutComponent;
/**
* Defines the component that will render when the route matches.
*/
default: RouteComponent;
/**
* Route handle allows apps to add anything to a route match in `useMatches`
* to create abstractions (like breadcrumbs, etc.).
*/
handle?: RouteHandle;
/**
* Route links define [`<link>` element][link-element]s to be rendered in the
* document `<head>`.
*/
links?: LinksFunction;
/**
* Route meta defines meta tags to be rendered in the `<head>` of the document.
*/
meta?: MetaFunction;
/**
* By default, all routes are revalidated after actions. This function allows
* a route to opt-out of revalidation for actions that don't affect its data.
*/
shouldRevalidate?: ShouldRevalidateFunction;
}

/**
* The shape of a route module file for your application
*/
export interface ServerRouteModule extends RouteModule {
/**
* Route actions allow server-side data mutations with automatic revalidation
* of all loader data on the page when called from `<Form>`, `useFetcher`,
* and `useSubmit`.
* */
action?: ActionFunction;
/**
* Route headers define HTTP headers to be sent with the response when server rendering.
*/
headers?: HeadersFunction | { [name: string]: string };
/**
* Route loaders provide data to route components before they are rendered.
* They are only called on the server when server rendering or during the
* build with pre-rendering.
*/
loader?: LoaderFunction;
}

/**
* A function that handles data mutations for a route on the client
*/
Expand Down Expand Up @@ -66,6 +131,24 @@ export type ClientLoaderFunctionArgs = LoaderFunctionArgs<undefined> & {
*/
export type ErrorBoundaryComponent = ComponentType;

/**
* Parameters passed to the [`headers`]{@link HeadersFunction} function
*/
export type HeadersArgs = {
loaderHeaders: Headers;
parentHeaders: Headers;
actionHeaders: Headers;
errorHeaders: Headers | undefined;
};

/**
* A function that returns HTTP headers to be used for a route. These headers
* will be merged with (and take precedence over) headers from parent routes.
*/
export interface HeadersFunction {
(args: HeadersArgs): Headers | HeadersInit;
}

/**
* `<Route HydrateFallback>` component to render on initial loads
* when client loaders are present
Expand Down
6 changes: 3 additions & 3 deletions packages/react-router/lib/server-runtime/entry.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { RouteModules } from "../dom/ssr/routeModules";
import type { ServerRouteManifest } from "./routes";
import type { RouteModules, EntryRouteModule } from "./routeModules";

export function createEntryRouteModules(
manifest: ServerRouteManifest
): RouteModules<EntryRouteModule> {
): RouteModules {
return Object.keys(manifest).reduce((memo, routeId) => {
let route = manifest[routeId];
if (route) {
memo[routeId] = route.module;
}
return memo;
}, {} as RouteModules<EntryRouteModule>);
}, {} as RouteModules);
}
56 changes: 0 additions & 56 deletions packages/react-router/lib/server-runtime/routeModules.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/react-router/lib/server-runtime/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import type {
} from "../router/utils";
import { callRouteHandler } from "./data";
import type { FutureConfig } from "../dom/ssr/entry";
import type { ServerRouteModule } from "../dom/ssr/routeModules";
import type { Route } from "../dom/ssr/routes";
import type { ServerRouteModule } from "./routeModules";
import type {
SingleFetchResult,
SingleFetchResults,
Expand Down

0 comments on commit 6352d2e

Please sign in to comment.