Skip to content

Commit 239039d

Browse files
feat(react-router): add nonce prop to Links & PrefetchPageLinks components (#14048)
1 parent 0d70250 commit 239039d

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

.changeset/giant-ligers-mix.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router": minor
3+
---
4+
5+
Add `nonce` prop to `Links` & `PrefetchPageLinks`

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,20 @@ function getActiveMatches(
214214

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

217+
/**
218+
* Props for the {@link Links} component.
219+
*
220+
* @category Types
221+
*/
222+
export interface LinksProps {
223+
/**
224+
* A [`nonce`](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes/nonce)
225+
* attribute to render on the [`<link>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link)
226+
* element
227+
*/
228+
nonce?: string | undefined;
229+
}
230+
217231
/**
218232
* Renders all the [`<link>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link)
219233
* tags created by the route module's [`links`](../../start/framework/route-module#links)
@@ -237,10 +251,12 @@ export const CRITICAL_CSS_DATA_ATTRIBUTE = "data-react-router-critical-css";
237251
* @public
238252
* @category Components
239253
* @mode framework
254+
* @param props Props
255+
* @param {LinksProps.nonce} props.nonce n/a
240256
* @returns A collection of React elements for [`<link>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link)
241257
* tags
242258
*/
243-
export function Links(): React.JSX.Element {
259+
export function Links({ nonce }: LinksProps): React.JSX.Element {
244260
let { isSpaMode, manifest, routeModules, criticalCss } =
245261
useFrameworkContext();
246262
let { errors, matches: routerMatches } = useDataRouterStateContext();
@@ -265,13 +281,14 @@ export function Links(): React.JSX.Element {
265281
{...{ [CRITICAL_CSS_DATA_ATTRIBUTE]: "" }}
266282
rel="stylesheet"
267283
href={criticalCss.href}
284+
nonce={nonce}
268285
/>
269286
) : null}
270287
{keyedLinks.map(({ key, link }) =>
271288
isPageLinkDescriptor(link) ? (
272-
<PrefetchPageLinks key={key} {...link} />
289+
<PrefetchPageLinks key={key} nonce={nonce} {...link} />
273290
) : (
274-
<link key={key} {...link} />
291+
<link key={key} nonce={nonce} {...link} />
275292
),
276293
)}
277294
</>
@@ -463,7 +480,7 @@ function PrefetchPageLinksImpl({
463480
{keyedPrefetchLinks.map(({ key, link }) => (
464481
// these don't spread `linkProps` because they are full link descriptors
465482
// already with their own props
466-
<link key={key} {...link} />
483+
<link key={key} nonce={linkProps.nonce} {...link} />
467484
))}
468485
</>
469486
);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ export interface PageLinkDescriptor
183183
| "color"
184184
| "title"
185185
> {
186+
/**
187+
* A [`nonce`](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes/nonce)
188+
* attribute to render on the [`<link>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link)
189+
* element
190+
*/
191+
nonce?: string | undefined;
186192
/**
187193
* The absolute path of the page to prefetch, e.g. `/absolute/path`.
188194
*/

0 commit comments

Comments
 (0)