-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
refactor(react-router): Slight reorg of the RouteModule
types
#12560
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,4 +1,4 @@ | ||||||||||
import type { ComponentType, ReactElement } from "react"; | ||||||||||
import type * as React from "react"; | ||||||||||
import type { Location } from "../../router/history"; | ||||||||||
import type { | ||||||||||
ActionFunction, | ||||||||||
|
@@ -18,19 +18,82 @@ export interface RouteModules { | |||||||||
[routeId: string]: RouteModule | undefined; | ||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
* The shape of a route module shipped to the client | ||||||||||
*/ | ||||||||||
export interface RouteModule { | ||||||||||
/** | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be a
Suggested change
|
||||||||||
* 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; | ||||||||||
ErrorBoundary?: ErrorBoundaryComponent; | ||||||||||
HydrateFallback?: HydrateFallbackComponent; | ||||||||||
/** | ||||||||||
* ErrorBoundary to display for this route | ||||||||||
*/ | ||||||||||
ErrorBoundary?: React.ComponentType; | ||||||||||
/** | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I got rid of the type aliases here in favor of just using |
||||||||||
* `<Route HydrateFallback>` component to render on initial loads | ||||||||||
* when client loaders are present | ||||||||||
*/ | ||||||||||
HydrateFallback?: React.ComponentType; | ||||||||||
/** | ||||||||||
* 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; | ||||||||||
default: RouteComponent; | ||||||||||
/** | ||||||||||
* Defines the component that will render when the route matches. | ||||||||||
*/ | ||||||||||
default: React.ComponentType<{}>; | ||||||||||
/** | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inlined but I think this is invalid now with the typegen props - going to look into making this actually pull the typegen stuff ( |
||||||||||
* 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 { | ||||||||||
/** | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for this to be an
Suggested change
|
||||||||||
* 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; | ||||||||||
} | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved into this file from the now deleted version from server-runtime |
||||||||||
|
||||||||||
/** | ||||||||||
* A function that handles data mutations for a route on the client | ||||||||||
*/ | ||||||||||
|
@@ -62,26 +125,30 @@ export type ClientLoaderFunctionArgs = LoaderFunctionArgs<undefined> & { | |||||||||
}; | ||||||||||
|
||||||||||
/** | ||||||||||
* ErrorBoundary to display for this route | ||||||||||
* Parameters passed to the [`headers`]{@link HeadersFunction} function | ||||||||||
*/ | ||||||||||
export type ErrorBoundaryComponent = ComponentType; | ||||||||||
export type HeadersArgs = { | ||||||||||
loaderHeaders: Headers; | ||||||||||
parentHeaders: Headers; | ||||||||||
actionHeaders: Headers; | ||||||||||
errorHeaders: Headers | undefined; | ||||||||||
}; | ||||||||||
|
||||||||||
/** | ||||||||||
* `<Route HydrateFallback>` component to render on initial loads | ||||||||||
* when client loaders are present | ||||||||||
* 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 type HydrateFallbackComponent = ComponentType; | ||||||||||
export interface HeadersFunction { | ||||||||||
(args: HeadersArgs): Headers | HeadersInit; | ||||||||||
} | ||||||||||
|
||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved in from the server-runtime file
Comment on lines
+141
to
+143
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be simplified to
Suggested change
|
||||||||||
/** | ||||||||||
* Optional, root-only `<Route Layout>` component to wrap the root content in. | ||||||||||
* Useful for defining the <html>/<head>/<body> document shell shared by the | ||||||||||
* Component, HydrateFallback, and ErrorBoundary | ||||||||||
*/ | ||||||||||
export type LayoutComponent = ComponentType<{ | ||||||||||
children: ReactElement< | ||||||||||
unknown, | ||||||||||
ErrorBoundaryComponent | HydrateFallbackComponent | RouteComponent | ||||||||||
>; | ||||||||||
export type LayoutComponent = React.ComponentType<{ | ||||||||||
children: React.ReactElement; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I legit didn't know this type existed, FWIW I added it here and got a comment from Michael to maybe change it to a different React type, check it out here: |
||||||||||
}>; | ||||||||||
|
||||||||||
Comment on lines
+150
to
152
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be simplified to
Suggested change
|
||||||||||
/** | ||||||||||
|
@@ -218,11 +285,6 @@ type LdJsonArray = LdJsonValue[] | readonly LdJsonValue[]; | |||||||||
type LdJsonPrimitive = string | number | boolean | null; | ||||||||||
type LdJsonValue = LdJsonPrimitive | LdJsonObject | LdJsonArray; | ||||||||||
|
||||||||||
/** | ||||||||||
* A React component that is rendered for a route. | ||||||||||
*/ | ||||||||||
export type RouteComponent = ComponentType<{}>; | ||||||||||
|
||||||||||
/** | ||||||||||
* An arbitrary object that is associated with a route. | ||||||||||
* | ||||||||||
|
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); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be weird, and potentially even a sign that this doesn't belong in type docs?
This is not a user-facing type but the only way to get the typedoc generated is to make it public API so we have to export it even though we never want anyone to import it and use it.