Skip to content
Myr

fix(jsx): align link hoisting and dedupe with React 19 (#4792)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(jsx): align link hoisting and dedupe with React 19 * ci: apply automated fixes * test(jsx): fix expected output for link element deduplication * fix: remove unused argument from shouldDeDupeByKey Co-authored-by: Yusuke Wada <yusuke@kamawada.com> * test: Add test to check behavior of uncovered conditional branches ---------

authored by Taku Amano · committed by GitHub · parent 5086956 · Browse files · 5 files+575 added−23 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Co-authored-by: Yusuke Wada <yusuke@kamawada.com>

Diff stats

5 files+575 added−23 removed

  • tests:+485 added−0 removed
  • other:+90 added−23 removed

5 files+575 added−23 removed

modified:src/jsx/dom/intrinsic-element/components.test.tsx+298 added
311 unmodified lines
312 await new Promise((resolve) => setTimeout(resolve))313 await Promise.resolve()314 expect(root.innerHTML).toBe('<div><div>Content</div><button>Show</button></div>')315 })316317 describe('React 19 compatibility', () => {318 type LinkSignatureInput = {319 rel: string320 href: string321 hrefLang?: string322 type?: string323 title?: string324 media?: string325 as?: string326 crossOrigin?: string327 sizes?: string328 dataPrecedence?: string329 }330331 const sig = (v: LinkSignatureInput) =>332 [333 v.rel,334 v.href,335 v.hrefLang ?? '',336 v.type ?? '',337 v.title ?? '',338 v.media ?? '',339 v.as ?? '',340 v.crossOrigin ?? '',341 v.sizes ?? '',342 v.dataPrecedence ?? '',343 ].join('|')344345 const headLinkSignatures = () =>346 Array.from(document.head.querySelectorAll('link')).map((el) =>347 sig({348 rel: el.getAttribute('rel') ?? '',349 href: el.getAttribute('href') ?? '',350 hrefLang: el.getAttribute('hreflang') ?? '',351 type: el.getAttribute('type') ?? '',352 title: el.getAttribute('title') ?? '',353 media: el.getAttribute('media') ?? '',354 as: el.getAttribute('as') ?? '',355 crossOrigin: el.getAttribute('crossorigin') ?? '',356 sizes: el.getAttribute('sizes') ?? '',357 dataPrecedence: el.getAttribute('data-precedence') ?? '',358 })359 )360361 const assertHeadLinks = (362 node: unknown,363 expected: string[],364 expectedRootInnerHTML?: string365 ) => {366 render(node as never, root)367 expect(headLinkSignatures()).toEqual(expected)368 if (expectedRootInnerHTML !== undefined) {369 expect(root.innerHTML).toBe(expectedRootInnerHTML)370 }371 }372373 it('should keep canonical and alternates in source order', () => {374 assertHeadLinks(375 <div>376 <link rel='canonical' href='https://example.com/en/about' />377 <link rel='alternate' hrefLang='en' href='https://example.com/en/about' />378 <link rel='alternate' hrefLang='ja' href='https://example.com/ja/about' />379 </div>,380 [381 sig({ rel: 'canonical', href: 'https://example.com/en/about' }),382 sig({ rel: 'alternate', href: 'https://example.com/en/about', hrefLang: 'en' }),383 sig({ rel: 'alternate', href: 'https://example.com/ja/about', hrefLang: 'ja' }),384 ]385 )386 })387388 it('should keep alternate-canonical-alternate order', () => {389 assertHeadLinks(390 <div>391 <link rel='alternate' hrefLang='en' href='https://example.com/en/about' />392 <link rel='canonical' href='https://example.com/en/about' />393 <link rel='alternate' hrefLang='ja' href='https://example.com/ja/about' />394 </div>,395 [396 sig({ rel: 'alternate', href: 'https://example.com/en/about', hrefLang: 'en' }),397 sig({ rel: 'canonical', href: 'https://example.com/en/about' }),398 sig({ rel: 'alternate', href: 'https://example.com/ja/about', hrefLang: 'ja' }),399 ]400 )401 })402403 it('should not de-duplicate canonical links', () => {404 assertHeadLinks(405 <div>406 <link rel='canonical' href='https://example.com/en/about' />407 <link rel='canonical' href='https://example.com/en/about' />408 </div>,409 [410 sig({ rel: 'canonical', href: 'https://example.com/en/about' }),411 sig({ rel: 'canonical', href: 'https://example.com/en/about' }),412 ]413 )414 })415416 it('should not de-duplicate alternate links', () => {417 assertHeadLinks(418 <div>419 <link rel='alternate' hrefLang='en' href='https://example.com/en/about' />420 <link rel='alternate' hrefLang='en' href='https://example.com/en/about' />421 </div>,422 [423 sig({ rel: 'alternate', href: 'https://example.com/en/about', hrefLang: 'en' }),424 sig({ rel: 'alternate', href: 'https://example.com/en/about', hrefLang: 'en' }),425 ]426 )427 })428429 it('should de-duplicate stylesheet with precedence', () => {430 assertHeadLinks(431 <div>432 <link rel='stylesheet' href='/style.css' precedence='default' />433 <link rel='stylesheet' href='/style.css' precedence='default' />434 </div>,435 [sig({ rel: 'stylesheet', href: '/style.css', dataPrecedence: 'default' })]436 )437 })438439 it('should not de-duplicate stylesheet against preload with same href', () => {440 assertHeadLinks(441 <div>442 <link rel='preload' href='/style.css' as='style' />443 <link rel='stylesheet' href='/style.css' precedence='default' />444 <link rel='stylesheet' href='/style.css' precedence='default' />445 </div>,446 [447 sig({ rel: 'stylesheet', href: '/style.css', dataPrecedence: 'default' }),448 sig({ rel: 'preload', href: '/style.css', as: 'style' }),449 ]450 )451 })452453 it('should keep stylesheet links without precedence in root', () => {454 assertHeadLinks(455 <div>456 <link rel='stylesheet' href='/style.css' />457 <link rel='stylesheet' href='/style.css' />458 </div>,459 [],460 '<div><link rel="stylesheet" href="/style.css"><link rel="stylesheet" href="/style.css"></div>'461 )462 })463464 it('should keep different stylesheets with same precedence', () => {465 assertHeadLinks(466 <div>467 <link rel='stylesheet' href='/a.css' precedence='default' />468 <link rel='stylesheet' href='/b.css' precedence='default' />469 </div>,470 [471 sig({ rel: 'stylesheet', href: '/a.css', dataPrecedence: 'default' }),472 sig({ rel: 'stylesheet', href: '/b.css', dataPrecedence: 'default' }),473 ]474 )475 })476477 it('should not de-duplicate preload links', () => {478 assertHeadLinks(479 <div>480 <link rel='preload' href='/font.woff2' as='font' crossOrigin='' />481 <link rel='preload' href='/font.woff2' as='font' crossOrigin='' />482 </div>,483 [484 sig({ rel: 'preload', href: '/font.woff2', as: 'font', crossOrigin: '' }),485 sig({ rel: 'preload', href: '/font.woff2', as: 'font', crossOrigin: '' }),486 ]487 )488 })489490 it('should not de-duplicate modulepreload links', () => {491 assertHeadLinks(492 <div>493 <link rel='modulepreload' href='/module.js' />494 <link rel='modulepreload' href='/module.js' />495 </div>,496 [497 sig({ rel: 'modulepreload', href: '/module.js' }),498 sig({ rel: 'modulepreload', href: '/module.js' }),499 ]500 )501 })502503 it('should keep links from two components', () => {504 const Head = () => (505 <>506 <link rel='canonical' href='https://example.com/en/about' />507 <link rel='alternate' hrefLang='en' href='https://example.com/en/about' />508 <link rel='alternate' hrefLang='ja' href='https://example.com/ja/about' />509 </>510 )511 const Body = () => (512 <>513 <link rel='canonical' href='https://example.com/en/about' />514 <link rel='alternate' hrefLang='en' href='https://example.com/en/about' />515 <link rel='alternate' hrefLang='ja' href='https://example.com/ja/about' />516 </>517 )518 assertHeadLinks(519 <div>520 <Head />521 <Body />522 </div>,523 [524 sig({ rel: 'canonical', href: 'https://example.com/en/about' }),525 sig({ rel: 'alternate', href: 'https://example.com/en/about', hrefLang: 'en' }),526 sig({ rel: 'alternate', href: 'https://example.com/ja/about', hrefLang: 'ja' }),527 sig({ rel: 'canonical', href: 'https://example.com/en/about' }),528 sig({ rel: 'alternate', href: 'https://example.com/en/about', hrefLang: 'en' }),529 sig({ rel: 'alternate', href: 'https://example.com/ja/about', hrefLang: 'ja' }),530 ]531 )532 })533534 it('should hoist from deep nested component and keep duplicates', () => {535 const Nested = () => (536 <div>537 <div>538 <link rel='canonical' href='https://example.com/en/about' />539 </div>540 </div>541 )542 assertHeadLinks(543 <div>544 <link rel='canonical' href='https://example.com/en/about' />545 <Nested />546 </div>,547 [548 sig({ rel: 'canonical', href: 'https://example.com/en/about' }),549 sig({ rel: 'canonical', href: 'https://example.com/en/about' }),550 ]551 )552 })553554 it('should keep mixed links and de-duplicate only stylesheet', () => {555 assertHeadLinks(556 <div>557 <link rel='canonical' href='https://example.com/en/about' />558 <link rel='alternate' hrefLang='en' href='https://example.com/en/about' />559 <link rel='stylesheet' href='/style.css' precedence='default' />560 <link rel='preload' href='/font.woff2' as='font' crossOrigin='' />561 <link rel='canonical' href='https://example.com/en/about' />562 <link rel='alternate' hrefLang='en' href='https://example.com/en/about' />563 <link rel='stylesheet' href='/style.css' precedence='default' />564 <link rel='preload' href='/font.woff2' as='font' crossOrigin='' />565 </div>,566 [567 sig({ rel: 'stylesheet', href: '/style.css', dataPrecedence: 'default' }),568 sig({ rel: 'canonical', href: 'https://example.com/en/about' }),569 sig({ rel: 'alternate', href: 'https://example.com/en/about', hrefLang: 'en' }),570 sig({ rel: 'preload', href: '/font.woff2', as: 'font', crossOrigin: '' }),571 sig({ rel: 'canonical', href: 'https://example.com/en/about' }),572 sig({ rel: 'alternate', href: 'https://example.com/en/about', hrefLang: 'en' }),573 sig({ rel: 'preload', href: '/font.woff2', as: 'font', crossOrigin: '' }),574 ]575 )576 })577 })578 })579580 describe('style element', () => {581 it('should be inserted into head', () => {
398 unmodified lines
980 expect(onLoad).not.toBeCalled()981 expect(onError).toBeCalledTimes(1)982 })983984 it('should not register load handlers when the tag has no de-duplication key', () => {985 const addEventListener = vi.fn<HTMLElement['addEventListener']>()986 const onLoad = vi.fn()987 const onError = vi.fn()988989 const App = () => {990 return (991 <div>992 <title993 ref={(e: HTMLTitleElement) => {994 if (!e) {995 return996 }997 const originalAddEventListener = e.addEventListener.bind(e)998 addEventListener.mockImplementation((...args) =>999 originalAddEventListener(...args)1000 )1001 e.addEventListener = addEventListener1002 }}1003 onLoad={onLoad}1004 onError={onError}1005 >1006 Document Title1007 </title>1008 Content1009 </div>1010 )1011 }1012 render(<App />, root)1013 expect(document.head.innerHTML).toBe('<title>Document Title</title>')1014 expect(root.innerHTML).toBe('<div>Content</div>')1015 expect(addEventListener).not.toBeCalled()1016 expect(onLoad).not.toBeCalled()1017 expect(onError).not.toBeCalled()1018 })10191020 it('should be blocked by blocking attribute', async () => {1021 const Component = () => {1022 return (1023 <Suspense fallback={<div>Loading...</div>}>
321 unmodified lines
modified:src/jsx/dom/intrinsic-element/components.ts+51 added-11 removed
1import type { Props } from '../../base'2import { useContext } from '../../context'3import { use, useCallback, useMemo, useState } from '../../hooks'4import { dataPrecedenceAttr, deDupeKeyMap, domRenderers } from '../../intrinsic-element/common'4import {5 dataPrecedenceAttr,6 deDupeKeyMap,7 domRenderers,8 isStylesheetLinkWithPrecedence,9 shouldDeDupeByKey,10} from '../../intrinsic-element/common'11import type { IntrinsicElements } from '../../intrinsic-elements'12import type { FC, JSXNode, PropsWithChildren, RefObject } from '../../types'13import { FormContext, registerAction } from '../hooks'14import type { PreserveNodeType } from '../render'
62 unmodified lines
77 let element: HTMLElement | null = null78 let created = false7980 const deDupeKeys = deDupeKeyMap[tag]81 const deDupeByKey = shouldDeDupeByKey(tag, supportSort)82 const isDeDupeCandidateLink = (e: HTMLElement) =>83 e.getAttribute('rel') === 'stylesheet' && e.getAttribute(dataPrecedenceAttr) !== null84 let existingElements: NodeListOf<HTMLElement> | undefined = undefined76 if (deDupeKeys.length > 0) {85 if (deDupeByKey) {86 const tags = head.querySelectorAll<HTMLElement>(tag)87 LOOP: for (const e of tags) {88 if (tag === 'link' && !isDeDupeCandidateLink(e)) {89 continue90 }79 for (const key of deDupeKeyMap[tag]) {91 for (const key of deDupeKeys) {92 if (e.getAttribute(key) === props[key]) {93 element = e94 break LOOP95 }
11 unmodified lines
107 for (const key of deDupeKeys) {108 if (props[key] !== undefined) {109 e.setAttribute(key, props[key] as string)110 }99 // eslint-disable-next-line @typescript-eslint/no-explicit-any100 if ((props as any).rel) {101 e.setAttribute('rel', props.rel)102 }111 }112 if (props.rel) {113 e.setAttribute('rel', props.rel)114 }115 return e116 })()117 }118 } else {
6 unmodified lines
125 }126127 const insert = useCallback(128 (e: HTMLElement) => {118 if (deDupeKeys.length > 0) {129 if (deDupeByKey) {130 if (tag === 'link' && precedence !== undefined) {131 let found = false132 for (const existingElement of head.querySelectorAll<HTMLElement>(tag)) {133 const existingPrecedence = existingElement.getAttribute(dataPrecedenceAttr)134 if (existingPrecedence === null) {135 head.insertBefore(e, existingElement)136 return137 }138 if (found && existingPrecedence !== precedence) {139 head.insertBefore(e, existingElement)140 return141 }142 if (existingPrecedence === precedence) {143 found = true144 }145 }146147 // if sentinel is not found, append to the end148 head.appendChild(e)149 return150 }151152 let found = false153 for (const existingElement of head.querySelectorAll<HTMLElement>(tag)) {154 if (found && existingElement.getAttribute(dataPrecedenceAttr) !== precedence) {155 head.insertBefore(e, existingElement)
5 unmodified lines
161 }162163 // if sentinel is not found, append to the end164 head.appendChild(e)165 } else if (tag === 'link') {166 if (!head.contains(e)) {167 head.appendChild(e)168 }169 } else if (existingElements) {170 let found = false171 for (const existingElement of existingElements!) {172 if (existingElement === e) {
10 unmodified lines
183 }184 existingElements = undefined185 }186 },150 [precedence]187 [deDupeByKey, precedence, tag]188 )189190 const ref = composeRef(props.ref, (e: HTMLElement) => {191 const key = deDupeKeys[0]
8 unmodified lines
200201 if (!onError && !onLoad) {202 return203 }204 if (!key) {205 return206 }207208 let promise = (blockingPromiseMap[e.getAttribute(key) as string] ||= new Promise<Event>(209 (resolve, reject) => {210 e.addEventListener('load', resolve)
10 unmodified lines
221 })222223 if (supportBlocking && blocking === 'render') {224 const key = deDupeKeyMap[tag][0]185 if (props[key]) {225 if (key && props[key]) {226 const value = props[key]227 const promise = (blockingPromiseMap[value] ||= new Promise<Event>((resolve, reject) => {228 insert(element as HTMLElement)229 element!.addEventListener('load', resolve)
77 unmodified lines
307 type: 'link',308 ref: props.ref,309 } as unknown as JSXNode310 }271 return documentMetadataTag('link', props, 1, 'precedence' in props, true)311 return documentMetadataTag('link', props, 1, isStylesheetLinkWithPrecedence(props), true)312}313314export const meta: FC<PropsWithChildren> = (props) => {315 return documentMetadataTag('meta', props, undefined, false, false)
123 unmodified lines
modified:src/jsx/intrinsic-element/common.ts+12 added
modified:src/jsx/intrinsic-element/components.test.tsx+187 added
113 unmodified lines
114 expect(template.toString()).toBe(115 '<html><head></head><body><link rel="stylesheet" href="style1.css"/><h1>World</h1></body></html>'116 )117 })118119 describe('React 19 compatibility', () => {120 const headContent = (html: string) => html.match(/<head>(.*)<\/head>/)?.[1] ?? ''121 const bodyContent = (html: string) => html.match(/<body>(.*)<\/body>/)?.[1] ?? ''122 const renderDocument = (children: unknown) =>123 (124 <html>125 <head></head>126 <body>{children}</body>127 </html>128 ).toString()129 const assertHeadAndBody = (children: unknown, expectedHead: string, expectedBody = '') => {130 const output = renderDocument(children)131 expect(headContent(output)).toBe(expectedHead)132 expect(bodyContent(output)).toBe(expectedBody)133 }134135 const canonical = () => <link rel='canonical' href='https://example.com/en/about' />136 const alternateEn = () => (137 <link rel='alternate' hrefLang='en' href='https://example.com/en/about' />138 )139 const alternateJa = () => (140 <link rel='alternate' hrefLang='ja' href='https://example.com/ja/about' />141 )142143 it('should keep canonical and alternates in source order', () => {144 assertHeadAndBody(145 <>146 {canonical()}147 {alternateEn()}148 {alternateJa()}149 </>,150 '<link rel="canonical" href="https://example.com/en/about"/><link rel="alternate" hrefLang="en" href="https://example.com/en/about"/><link rel="alternate" hrefLang="ja" href="https://example.com/ja/about"/>'151 )152 })153154 it('should keep alternate-canonical-alternate order', () => {155 assertHeadAndBody(156 <>157 {alternateEn()}158 {canonical()}159 {alternateJa()}160 </>,161 '<link rel="alternate" hrefLang="en" href="https://example.com/en/about"/><link rel="canonical" href="https://example.com/en/about"/><link rel="alternate" hrefLang="ja" href="https://example.com/ja/about"/>'162 )163 })164165 it('should not de-dupe canonical links', () => {166 assertHeadAndBody(167 <>168 {canonical()}169 {canonical()}170 </>,171 '<link rel="canonical" href="https://example.com/en/about"/><link rel="canonical" href="https://example.com/en/about"/>'172 )173 })174175 it('should not de-dupe alternate links', () => {176 assertHeadAndBody(177 <>178 {alternateEn()}179 {alternateEn()}180 </>,181 '<link rel="alternate" hrefLang="en" href="https://example.com/en/about"/><link rel="alternate" hrefLang="en" href="https://example.com/en/about"/>'182 )183 })184185 it('should de-dupe stylesheet with precedence', () => {186 assertHeadAndBody(187 <>188 <link rel='stylesheet' href='/style.css' precedence='default' />189 <link rel='stylesheet' href='/style.css' precedence='default' />190 </>,191 '<link rel="stylesheet" href="/style.css" data-precedence="default"/>'192 )193 })194195 it('should not de-dupe stylesheet against preload with same href', () => {196 assertHeadAndBody(197 <>198 <link rel='preload' href='/style.css' as='style' />199 <link rel='stylesheet' href='/style.css' precedence='default' />200 <link rel='stylesheet' href='/style.css' precedence='default' />201 </>,202 '<link rel="stylesheet" href="/style.css" data-precedence="default"/><link rel="preload" href="/style.css" as="style"/>'203 )204 })205206 it('should not hoist stylesheet without precedence', () => {207 assertHeadAndBody(208 <>209 <link rel='stylesheet' href='/style.css' />210 <link rel='stylesheet' href='/style.css' />211 </>,212 '',213 '<link rel="stylesheet" href="/style.css"/><link rel="stylesheet" href="/style.css"/>'214 )215 })216217 it('should keep different stylesheets with same precedence', () => {218 assertHeadAndBody(219 <>220 <link rel='stylesheet' href='/a.css' precedence='default' />221 <link rel='stylesheet' href='/b.css' precedence='default' />222 </>,223 '<link rel="stylesheet" href="/a.css" data-precedence="default"/><link rel="stylesheet" href="/b.css" data-precedence="default"/>'224 )225 })226227 it('should not de-dupe preload links', () => {228 assertHeadAndBody(229 <>230 <link rel='preload' href='/font.woff2' as='font' crossOrigin='' />231 <link rel='preload' href='/font.woff2' as='font' crossOrigin='' />232 </>,233 '<link rel="preload" href="/font.woff2" as="font" crossorigin=""/><link rel="preload" href="/font.woff2" as="font" crossorigin=""/>'234 )235 })236237 it('should not de-dupe modulepreload links', () => {238 assertHeadAndBody(239 <>240 <link rel='modulepreload' href='/module.js' />241 <link rel='modulepreload' href='/module.js' />242 </>,243 '<link rel="modulepreload" href="/module.js"/><link rel="modulepreload" href="/module.js"/>'244 )245 })246247 it('should keep links from two components', () => {248 const Head = () => (249 <>250 <link rel='canonical' href='https://example.com/en/about' />251 <link rel='alternate' hrefLang='en' href='https://example.com/en/about' />252 <link rel='alternate' hrefLang='ja' href='https://example.com/ja/about' />253 </>254 )255 const Body = () => (256 <>257 <link rel='canonical' href='https://example.com/en/about' />258 <link rel='alternate' hrefLang='en' href='https://example.com/en/about' />259 <link rel='alternate' hrefLang='ja' href='https://example.com/ja/about' />260 </>261 )262 assertHeadAndBody(263 <>264 <Head />265 <Body />266 </>,267 '<link rel="canonical" href="https://example.com/en/about"/><link rel="alternate" hrefLang="en" href="https://example.com/en/about"/><link rel="alternate" hrefLang="ja" href="https://example.com/ja/about"/><link rel="canonical" href="https://example.com/en/about"/><link rel="alternate" hrefLang="en" href="https://example.com/en/about"/><link rel="alternate" hrefLang="ja" href="https://example.com/ja/about"/>'268 )269 })270271 it('should hoist from deep nested component and keep duplicates', () => {272 const Nested = () => (273 <div>274 <div>275 <link rel='canonical' href='https://example.com/en/about' />276 </div>277 </div>278 )279 assertHeadAndBody(280 <>281 <link rel='canonical' href='https://example.com/en/about' />282 <Nested />283 </>,284 '<link rel="canonical" href="https://example.com/en/about"/><link rel="canonical" href="https://example.com/en/about"/>',285 '<div><div></div></div>'286 )287 })288289 it('should keep mixed links and de-dupe only stylesheet', () => {290 assertHeadAndBody(291 <>292 <link rel='canonical' href='https://example.com/en/about' />293 <link rel='alternate' hrefLang='en' href='https://example.com/en/about' />294 <link rel='stylesheet' href='/style.css' precedence='default' />295 <link rel='preload' href='/font.woff2' as='font' crossOrigin='' />296 <link rel='canonical' href='https://example.com/en/about' />297 <link rel='alternate' hrefLang='en' href='https://example.com/en/about' />298 <link rel='stylesheet' href='/style.css' precedence='default' />299 <link rel='preload' href='/font.woff2' as='font' crossOrigin='' />300 </>,301 '<link rel="stylesheet" href="/style.css" data-precedence="default"/><link rel="canonical" href="https://example.com/en/about"/><link rel="alternate" hrefLang="en" href="https://example.com/en/about"/><link rel="preload" href="/font.woff2" as="font" crossorigin=""/><link rel="canonical" href="https://example.com/en/about"/><link rel="alternate" hrefLang="en" href="https://example.com/en/about"/><link rel="preload" href="/font.woff2" as="font" crossorigin=""/>'302 )303 })304 })305 })306307 describe('meta element', () => {308 it('should be hoisted meta tag', async () => {
278 unmodified lines
modified:src/jsx/intrinsic-element/components.ts+27 added-12 removed
5 unmodified lines
6import { PERMALINK } from '../constants'7import { useContext } from '../context'8import type { IntrinsicElements } from '../intrinsic-elements'9import type { FC, PropsWithChildren } from '../types'10import { dataPrecedenceAttr, deDupeKeyMap } from './common'10import {11 dataPrecedenceAttr,12 deDupeKeyMap,13 isStylesheetLinkWithPrecedence,14 shouldDeDupeByKey,15} from './common'1617const metaTagMap: WeakMap<18 object,19 Record<string, [string, Props, string | undefined][]>
14 unmodified lines
34 const tags = (map[tagName] ||= [])3536 let duped = false37 const deDupeKeys = deDupeKeyMap[tagName]33 if (deDupeKeys.length > 0) {38 const deDupeByKey = shouldDeDupeByKey(tagName, precedence !== undefined)39 if (deDupeByKey) {40 LOOP: for (const [, tagProps] of tags) {41 if (42 tagName === 'link' &&43 !(tagProps.rel === 'stylesheet' && tagProps[dataPrecedenceAttr] !== undefined)44 ) {45 continue46 }47 for (const key of deDupeKeys) {48 if ((tagProps?.[key] ?? null) === props?.[key]) {49 duped = true50 break LOOP
3 unmodified lines
54 }5556 if (duped) {57 buffer[0] = buffer[0].replaceAll(tag, '')46 } else if (deDupeKeys.length > 0) {58 } else if (deDupeByKey || tagName === 'link') {59 tags.push([tag, props, precedence])60 } else {61 tags.unshift([tag, props, precedence])62 }6364 if (buffer[0].indexOf('</head>') !== -1) {65 let insertTags54 if (precedence === undefined) {55 insertTags = tags.map(([tag]) => tag)56 } else {66 if (tagName === 'link' || precedence !== undefined) {67 const precedences: string[] = []68 insertTags = tags59 .map(([tag, , precedence]) => {60 let order = precedences.indexOf(precedence as string)69 .map(([tag, , tagPrecedence], index) => {70 if (tagPrecedence === undefined) {71 return [tag, Number.MAX_SAFE_INTEGER, index] as [string, number, number]72 }73 let order = precedences.indexOf(tagPrecedence as string)74 if (order === -1) {62 precedences.push(precedence as string)75 precedences.push(tagPrecedence as string)76 order = precedences.length - 177 }65 return [tag, order] as [string, number]78 return [tag, order, index] as [string, number, number]79 })67 .sort((a, b) => a[1] - b[1])80 .sort((a, b) => a[1] - b[1] || a[2] - b[2])81 .map(([tag]) => tag)82 } else {83 insertTags = tags.map(([tag]) => tag)84 }8586 insertTags.forEach((tag) => {87 buffer[0] = buffer[0].replaceAll(tag, '')
77 unmodified lines
165 (props.rel === 'stylesheet' && (!('precedence' in props) || 'disabled' in props))166 ) {167 return returnWithoutSpecialBehavior('link', children, props)168 }154 return documentMetadataTag('link', children, props, 'precedence' in props)169 return documentMetadataTag('link', children, props, isStylesheetLinkWithPrecedence(props))170}171export const meta: FC<PropsWithChildren> = ({ children, ...props }) => {172 const nameSpaceContext = getNameSpaceContext()173 if (nameSpaceContext && useContext(nameSpaceContext) === 'head') {
37 unmodified lines