Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/giant-ligers-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router": minor
---

Add `nonce` prop to `Links` & `PrefetchPageLinks`
25 changes: 21 additions & 4 deletions packages/react-router/lib/dom/ssr/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,20 @@ function getActiveMatches(

export const CRITICAL_CSS_DATA_ATTRIBUTE = "data-react-router-critical-css";

/**
* Props for the {@link Links} component.
*
* @category Types
*/
export interface LinksProps {
/**
* A [`nonce`](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes/nonce)
* attribute to render on the [`<link>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link)
* element
*/
nonce?: string | undefined;
}

/**
* Renders all the [`<link>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link)
* tags created by the route module's [`links`](../../start/framework/route-module#links)
Expand All @@ -237,10 +251,12 @@ export const CRITICAL_CSS_DATA_ATTRIBUTE = "data-react-router-critical-css";
* @public
* @category Components
* @mode framework
* @param props Props
* @param {LinksProps.nonce} props.nonce n/a
* @returns A collection of React elements for [`<link>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link)
* tags
*/
export function Links(): React.JSX.Element {
export function Links({ nonce }: LinksProps): React.JSX.Element {
let { isSpaMode, manifest, routeModules, criticalCss } =
useFrameworkContext();
let { errors, matches: routerMatches } = useDataRouterStateContext();
Expand All @@ -265,13 +281,14 @@ export function Links(): React.JSX.Element {
{...{ [CRITICAL_CSS_DATA_ATTRIBUTE]: "" }}
rel="stylesheet"
href={criticalCss.href}
nonce={nonce}
/>
) : null}
{keyedLinks.map(({ key, link }) =>
isPageLinkDescriptor(link) ? (
<PrefetchPageLinks key={key} {...link} />
<PrefetchPageLinks key={key} nonce={nonce} {...link} />
) : (
<link key={key} {...link} />
<link key={key} nonce={nonce} {...link} />
),
)}
</>
Expand Down Expand Up @@ -463,7 +480,7 @@ function PrefetchPageLinksImpl({
{keyedPrefetchLinks.map(({ key, link }) => (
// these don't spread `linkProps` because they are full link descriptors
// already with their own props
<link key={key} {...link} />
<link key={key} nonce={linkProps.nonce} {...link} />
))}
</>
);
Expand Down
6 changes: 6 additions & 0 deletions packages/react-router/lib/router/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ export interface PageLinkDescriptor
| "color"
| "title"
> {
/**
* A [`nonce`](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes/nonce)
* attribute to render on the [`<link>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link)
* element
*/
nonce?: string | undefined;
/**
* The absolute path of the page to prefetch, e.g. `/absolute/path`.
*/
Expand Down