Skip to content

Commit c3d95f0

Browse files
authored
Merge pull request #11960 from remix-run/pedro/deduplicate-link-types
deduplicate link types
2 parents 461534e + 67bdbba commit c3d95f0

File tree

7 files changed

+20
-210
lines changed

7 files changed

+20
-210
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router": major
3+
---
4+
5+
PrefetchPageDescriptor replaced by PageLinkDescriptor

packages/react-router/index.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,6 @@ export {
201201
} from "./lib/dom/ssr/components";
202202
export type { ScriptsProps } from "./lib/dom/ssr/components";
203203
export type { EntryContext } from "./lib/dom/ssr/entry";
204-
export type {
205-
HtmlLinkDescriptor,
206-
LinkDescriptor,
207-
PrefetchPageDescriptor,
208-
} from "./lib/dom/ssr/links";
209204
export type {
210205
ClientActionFunction,
211206
ClientActionFunctionArgs,
@@ -282,11 +277,10 @@ export type {
282277
export type { AppLoadContext } from "./lib/server-runtime/data";
283278

284279
export type {
285-
// TODO: (v7) Clean up code paths for these exports
286-
// HtmlLinkDescriptor,
287-
// LinkDescriptor,
288280
PageLinkDescriptor,
289-
} from "./lib/server-runtime/links";
281+
HtmlLinkDescriptor,
282+
LinkDescriptor,
283+
} from "./lib/router/links";
290284

291285
export type { TypedResponse } from "./lib/server-runtime/responses";
292286

packages/react-router/lib/dom/ssr/components.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
getNewMatchesForLinks,
1919
isPageLinkDescriptor,
2020
} from "./links";
21-
import type { KeyedHtmlLinkDescriptor, PrefetchPageDescriptor } from "./links";
21+
import type { KeyedHtmlLinkDescriptor } from "./links";
2222
import { createHtml } from "./markup";
2323
import type {
2424
MetaFunction,
@@ -30,6 +30,7 @@ import { addRevalidationParam, singleFetchUrl } from "./single-fetch";
3030
import { DataRouterContext, DataRouterStateContext } from "../../context";
3131
import { useLocation } from "../../hooks";
3232
import { getPartialManifest, isFogOfWarEnabled } from "./fog-of-war";
33+
import type { PageLinkDescriptor } from "../../router/links";
3334

3435
// TODO: Temporary shim until we figure out the way to handle typings in v7
3536
export type SerializeFrom<D> = D extends () => {} ? Awaited<ReturnType<D>> : D;
@@ -273,7 +274,7 @@ export function Links() {
273274
export function PrefetchPageLinks({
274275
page,
275276
...dataLinkProps
276-
}: PrefetchPageDescriptor) {
277+
}: PageLinkDescriptor) {
277278
let { router } = useDataRouterContext();
278279
let matches = React.useMemo(
279280
() => matchRoutes(router.routes, page, router.basename),
@@ -320,7 +321,7 @@ function PrefetchPageLinksImpl({
320321
page,
321322
matches: nextMatches,
322323
...linkProps
323-
}: PrefetchPageDescriptor & {
324+
}: PageLinkDescriptor & {
324325
matches: AgnosticDataRouteMatch[];
325326
}) {
326327
let location = useLocation();

packages/react-router/lib/dom/ssr/links.ts

Lines changed: 6 additions & 196 deletions
Original file line numberDiff line numberDiff line change
@@ -6,201 +6,11 @@ import type { AssetsManifest } from "./entry";
66
import type { RouteModules, RouteModule } from "./routeModules";
77
import type { EntryRoute } from "./routes";
88
import { loadRouteModule } from "./routeModules";
9-
10-
type Primitive = null | undefined | string | number | boolean | symbol | bigint;
11-
12-
type LiteralUnion<LiteralType, BaseType extends Primitive> =
13-
| LiteralType
14-
| (BaseType & Record<never, never>);
15-
16-
interface HtmlLinkProps {
17-
/**
18-
* Address of the hyperlink
19-
*/
20-
href?: string;
21-
22-
/**
23-
* How the element handles crossorigin requests
24-
*/
25-
crossOrigin?: "anonymous" | "use-credentials";
26-
27-
/**
28-
* Relationship between the document containing the hyperlink and the destination resource
29-
*/
30-
rel: LiteralUnion<
31-
| "alternate"
32-
| "dns-prefetch"
33-
| "icon"
34-
| "manifest"
35-
| "modulepreload"
36-
| "next"
37-
| "pingback"
38-
| "preconnect"
39-
| "prefetch"
40-
| "preload"
41-
| "prerender"
42-
| "search"
43-
| "stylesheet",
44-
string
45-
>;
46-
47-
/**
48-
* Applicable media: "screen", "print", "(max-width: 764px)"
49-
*/
50-
media?: string;
51-
52-
/**
53-
* Integrity metadata used in Subresource Integrity checks
54-
*/
55-
integrity?: string;
56-
57-
/**
58-
* Language of the linked resource
59-
*/
60-
hrefLang?: string;
61-
62-
/**
63-
* Hint for the type of the referenced resource
64-
*/
65-
type?: string;
66-
67-
/**
68-
* Referrer policy for fetches initiated by the element
69-
*/
70-
referrerPolicy?:
71-
| ""
72-
| "no-referrer"
73-
| "no-referrer-when-downgrade"
74-
| "same-origin"
75-
| "origin"
76-
| "strict-origin"
77-
| "origin-when-cross-origin"
78-
| "strict-origin-when-cross-origin"
79-
| "unsafe-url";
80-
81-
/**
82-
* Sizes of the icons (for rel="icon")
83-
*/
84-
sizes?: string;
85-
86-
/**
87-
* Potential destination for a preload request (for rel="preload" and rel="modulepreload")
88-
*/
89-
as?: LiteralUnion<
90-
| "audio"
91-
| "audioworklet"
92-
| "document"
93-
| "embed"
94-
| "fetch"
95-
| "font"
96-
| "frame"
97-
| "iframe"
98-
| "image"
99-
| "manifest"
100-
| "object"
101-
| "paintworklet"
102-
| "report"
103-
| "script"
104-
| "serviceworker"
105-
| "sharedworker"
106-
| "style"
107-
| "track"
108-
| "video"
109-
| "worker"
110-
| "xslt",
111-
string
112-
>;
113-
114-
/**
115-
* Color to use when customizing a site's icon (for rel="mask-icon")
116-
*/
117-
color?: string;
118-
119-
/**
120-
* Whether the link is disabled
121-
*/
122-
disabled?: boolean;
123-
124-
/**
125-
* The title attribute has special semantics on this element: Title of the link; CSS style sheet set name.
126-
*/
127-
title?: string;
128-
129-
/**
130-
* Images to use in different situations, e.g., high-resolution displays,
131-
* small monitors, etc. (for rel="preload")
132-
*/
133-
imageSrcSet?: string;
134-
135-
/**
136-
* Image sizes for different page layouts (for rel="preload")
137-
*/
138-
imageSizes?: string;
139-
}
140-
141-
interface HtmlLinkPreloadImage extends HtmlLinkProps {
142-
/**
143-
* Relationship between the document containing the hyperlink and the destination resource
144-
*/
145-
rel: "preload";
146-
147-
/**
148-
* Potential destination for a preload request (for rel="preload" and rel="modulepreload")
149-
*/
150-
as: "image";
151-
152-
/**
153-
* Address of the hyperlink
154-
*/
155-
href?: string;
156-
157-
/**
158-
* Images to use in different situations, e.g., high-resolution displays,
159-
* small monitors, etc. (for rel="preload")
160-
*/
161-
imageSrcSet: string;
162-
163-
/**
164-
* Image sizes for different page layouts (for rel="preload")
165-
*/
166-
imageSizes?: string;
167-
}
168-
169-
/**
170-
* Represents a `<link>` element.
171-
*
172-
* WHATWG Specification: https://html.spec.whatwg.org/multipage/semantics.html#the-link-element
173-
*/
174-
export type HtmlLinkDescriptor =
175-
// Must have an href *unless* it's a `<link rel="preload" as="image">` with an
176-
// `imageSrcSet` and `imageSizes` props
177-
| (HtmlLinkProps & Pick<Required<HtmlLinkProps>, "href">)
178-
| (HtmlLinkPreloadImage & Pick<Required<HtmlLinkPreloadImage>, "imageSizes">)
179-
| (HtmlLinkPreloadImage &
180-
Pick<Required<HtmlLinkPreloadImage>, "href"> & { imageSizes?: never });
181-
182-
export interface PrefetchPageDescriptor
183-
extends Omit<
184-
HtmlLinkDescriptor,
185-
| "href"
186-
| "rel"
187-
| "type"
188-
| "sizes"
189-
| "imageSrcSet"
190-
| "imageSizes"
191-
| "as"
192-
| "color"
193-
| "title"
194-
> {
195-
/**
196-
* The absolute path of the page to prefetch.
197-
*/
198-
page: string;
199-
}
200-
201-
export type LinkDescriptor = HtmlLinkDescriptor | PrefetchPageDescriptor;
202-
203-
////////////////////////////////////////////////////////////////////////////////
9+
import type {
10+
HtmlLinkDescriptor,
11+
LinkDescriptor,
12+
PageLinkDescriptor,
13+
} from "../../router/links";
20414

20515
/**
20616
* Gets all the links for a set of matches. The modules are assumed to have been
@@ -296,7 +106,7 @@ async function prefetchStyleLink(
296106
////////////////////////////////////////////////////////////////////////////////
297107
export function isPageLinkDescriptor(
298108
object: any
299-
): object is PrefetchPageDescriptor {
109+
): object is PageLinkDescriptor {
300110
return object != null && typeof object.page === "string";
301111
}
302112

packages/react-router/lib/dom/ssr/routeModules.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import type {
1212

1313
import type { SerializeFrom } from "./components";
1414
import type { AppData } from "./data";
15-
import type { LinkDescriptor } from "./links";
1615
import type { EntryRoute } from "./routes";
1716
import type { DataRouteMatch } from "../../context";
17+
import type { LinkDescriptor } from "../../router/links";
1818

1919
export interface RouteModules {
2020
[routeId: string]: RouteModule | undefined;

packages/react-router/lib/server-runtime/routeModules.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import type {
88
Params,
99
} from "../router/utils";
1010
import type { AppData, AppLoadContext } from "./data";
11-
import type { LinkDescriptor } from "./links";
1211
import type { SerializeFrom } from "../dom/ssr/components";
12+
import type { LinkDescriptor } from "../router/links";
1313

1414
export interface RouteModules<RouteModule> {
1515
[routeId: string]: RouteModule | undefined;

0 commit comments

Comments
 (0)