[facebook/react] Bug: `` in RSC gets duplicated in SSR html
Sponsored Content
### ROOT CAUSE
The duplicate font preload tags issue occurs because the `fonts` array is being built multiple times during SSR rendering, leading to multiple entries for the same font. The current code does not check for duplicates before generating the preload tags, resulting in duplicate `` tags in the output HTML.
### CODE FIX
Modify the `ReactDOMServer` to use a Set to track unique fonts and prevent duplicates. Specifically, update the `getPreloadTags` function to ensure each font is only added once.
```javascript
// In ReactDOMServer, modify the getPreloadTags function:
function getPreloadTags() {
const tags = [];
// ... existing code for other tags
// For fonts, use a Set to avoid duplicates
const fontTags = [];
const fonts = getFonts(); // Assume getFonts() returns an array of font objects
const fontFamilies = new Set();
fonts.forEach(font => {
// Use font family or URL as a unique identifier
const key = font.family || font.url;
if (!fontFamilies.has(key)) {
fontFamilies.add(key);
fontTags.push(``);
}
});
tags.push(...fontTags); // Add font tags to the overall tags
return tags;
}
```
This change ensures that each font is only processed once, eliminating duplicate preload tags.