Skip to content
Open
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
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -448,3 +448,4 @@
- zeromask1337
- zheng-chuang
- zxTomw
- uozer7050
26 changes: 22 additions & 4 deletions packages/react-router/lib/dom/ssr/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -256,26 +256,36 @@ export interface LinksProps {
* @returns A collection of React elements for [`<link>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link)
* tags
*/
// Persistent set of hrefs so that CSS/JS links are not removed when
// routes unmount. This ensures lazy-loaded CSS persists across route changes.
const persistentHrefs = new Set<string>();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be WeakSet<object>?


export function Links({ nonce }: LinksProps): React.JSX.Element {
let { isSpaMode, manifest, routeModules, criticalCss } =
useFrameworkContext();
let { errors, matches: routerMatches } = useDataRouterStateContext();

// Get the active route matches (slice matches on error boundaries, etc.)
let matches = getActiveMatches(routerMatches, errors, isSpaMode);

// Compute the keyed links for all active matches. These include CSS modules,
// prefetch links, and other assets. Memoized to avoid unnecessary recalculations.
let keyedLinks = React.useMemo(
() => getKeyedLinksForMatches(matches, routeModules, manifest),
[matches, routeModules, manifest],
);

return (
<>
{/* Inject critical CSS as <style> if available */}
{typeof criticalCss === "string" ? (
<style
{...{ [CRITICAL_CSS_DATA_ATTRIBUTE]: "" }}
dangerouslySetInnerHTML={{ __html: criticalCss }}
/>
) : null}

{/* Inject critical CSS as <link> if object form */}
{typeof criticalCss === "object" ? (
<link
{...{ [CRITICAL_CSS_DATA_ATTRIBUTE]: "" }}
Expand All @@ -284,13 +294,21 @@ export function Links({ nonce }: LinksProps): React.JSX.Element {
nonce={nonce}
/>
) : null}
{keyedLinks.map(({ key, link }) =>
isPageLinkDescriptor(link) ? (

{/* Render all keyed links (CSS, prefetch, etc.) */}
{keyedLinks.map(({ key, link }) => {
// If this href has already been injected, skip it to avoid duplicates.
// This preserves lazy-loaded route CSS even when the route unmounts.
if (!link.href || persistentHrefs.has(link.href)) return null;

persistentHrefs.add(link.href);

return isPageLinkDescriptor(link) ? (
<PrefetchPageLinks key={key} nonce={nonce} {...link} />
) : (
<link key={key} nonce={nonce} {...link} />
),
)}
);
})}
</>
);
}
Expand Down