{"version":3,"file":"Asset.cjs","sources":["../../src/Asset.tsx"],"sourcesContent":["import * as React from 'react'\nimport { isServer } from '@tanstack/router-core/isServer'\nimport { useRouter } from './useRouter'\nimport { useHydrated } from './ClientOnly'\nimport type { RouterManagedTag } from '@tanstack/router-core'\n\ninterface ScriptAttrs {\n  [key: string]: string | boolean | undefined\n  src?: string\n  suppressHydrationWarning?: boolean\n}\n\nexport function Asset({\n  tag,\n  attrs,\n  children,\n  nonce,\n}: RouterManagedTag & { nonce?: string }): React.ReactElement | null {\n  switch (tag) {\n    case 'title':\n      return (\n        <title {...attrs} suppressHydrationWarning>\n          {children}\n        </title>\n      )\n    case 'meta':\n      return <meta {...attrs} suppressHydrationWarning />\n    case 'link':\n      return <link {...attrs} nonce={nonce} suppressHydrationWarning />\n    case 'style':\n      return (\n        <style\n          {...attrs}\n          dangerouslySetInnerHTML={{ __html: children as string }}\n          nonce={nonce}\n        />\n      )\n    case 'script':\n      return <Script attrs={attrs}>{children}</Script>\n    default:\n      return null\n  }\n}\n\nfunction Script({\n  attrs,\n  children,\n}: {\n  attrs?: ScriptAttrs\n  children?: string\n}) {\n  const router = useRouter()\n  const hydrated = useHydrated()\n  const dataScript =\n    typeof attrs?.type === 'string' &&\n    attrs.type !== '' &&\n    attrs.type !== 'text/javascript' &&\n    attrs.type !== 'module'\n\n  if (\n    process.env.NODE_ENV !== 'production' &&\n    attrs?.src &&\n    typeof children === 'string' &&\n    children.trim().length\n  ) {\n    console.warn(\n      '[TanStack Router] <Script> received both `src` and `children`. The `children` content will be ignored. Remove `children` or remove `src`.',\n    )\n  }\n\n  React.useEffect(() => {\n    if (dataScript) return\n\n    if (attrs?.src) {\n      const normSrc = (() => {\n        try {\n          const base = document.baseURI || window.location.href\n          return new URL(attrs.src, base).href\n        } catch {\n          return attrs.src\n        }\n      })()\n      const existingScript = Array.from(\n        document.querySelectorAll('script[src]'),\n      ).find((el) => (el as HTMLScriptElement).src === normSrc)\n\n      if (existingScript) {\n        return\n      }\n\n      const script = document.createElement('script')\n\n      for (const [key, value] of Object.entries(attrs)) {\n        if (\n          key !== 'suppressHydrationWarning' &&\n          value !== undefined &&\n          value !== false\n        ) {\n          script.setAttribute(\n            key,\n            typeof value === 'boolean' ? '' : String(value),\n          )\n        }\n      }\n\n      document.head.appendChild(script)\n\n      return () => {\n        if (script.parentNode) {\n          script.parentNode.removeChild(script)\n        }\n      }\n    }\n\n    if (typeof children === 'string') {\n      const typeAttr =\n        typeof attrs?.type === 'string' ? attrs.type : 'text/javascript'\n      const nonceAttr =\n        typeof attrs?.nonce === 'string' ? attrs.nonce : undefined\n      const existingScript = Array.from(\n        document.querySelectorAll('script:not([src])'),\n      ).find((el) => {\n        if (!(el instanceof HTMLScriptElement)) return false\n        const sType = el.getAttribute('type') ?? 'text/javascript'\n        const sNonce = el.getAttribute('nonce') ?? undefined\n        return (\n          el.textContent === children &&\n          sType === typeAttr &&\n          sNonce === nonceAttr\n        )\n      })\n\n      if (existingScript) {\n        return\n      }\n\n      const script = document.createElement('script')\n      script.textContent = children\n\n      if (attrs) {\n        for (const [key, value] of Object.entries(attrs)) {\n          if (\n            key !== 'suppressHydrationWarning' &&\n            value !== undefined &&\n            value !== false\n          ) {\n            script.setAttribute(\n              key,\n              typeof value === 'boolean' ? '' : String(value),\n            )\n          }\n        }\n      }\n\n      document.head.appendChild(script)\n\n      return () => {\n        if (script.parentNode) {\n          script.parentNode.removeChild(script)\n        }\n      }\n    }\n\n    return undefined\n  }, [attrs, children, dataScript])\n\n  // --- Server rendering ---\n  if (isServer ?? router.isServer) {\n    if (attrs?.src) {\n      return <script {...attrs} suppressHydrationWarning />\n    }\n\n    if (typeof children === 'string') {\n      return (\n        <script\n          {...attrs}\n          dangerouslySetInnerHTML={{ __html: children }}\n          suppressHydrationWarning\n        />\n      )\n    }\n\n    return null\n  }\n\n  // --- Client rendering ---\n\n  // Data scripts (e.g. application/ld+json) are rendered in the tree;\n  // the useEffect intentionally skips them.\n  if (dataScript && typeof children === 'string') {\n    return (\n      <script\n        {...attrs}\n        suppressHydrationWarning\n        dangerouslySetInnerHTML={{ __html: children }}\n      />\n    )\n  }\n\n  // During hydration (before useEffect has fired), render the script element\n  // to match the server-rendered HTML and avoid structural hydration mismatches.\n  // After hydration, return null — the useEffect handles imperative injection.\n  if (!hydrated) {\n    if (attrs?.src) {\n      return <script {...attrs} suppressHydrationWarning />\n    }\n    if (typeof children === 'string') {\n      return (\n        <script\n          {...attrs}\n          dangerouslySetInnerHTML={{ __html: children }}\n          suppressHydrationWarning\n        />\n      )\n    }\n  }\n\n  return null\n}\n"],"names":["jsx","useRouter","useHydrated","React","isServer"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAYO,SAAS,MAAM;AAAA,EACpB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAAqE;AACnE,UAAQ,KAAA;AAAA,IACN,KAAK;AACH,4CACG,SAAA,EAAO,GAAG,OAAO,0BAAwB,MACvC,UACH;AAAA,IAEJ,KAAK;AACH,aAAOA,2BAAAA,IAAC,QAAA,EAAM,GAAG,OAAO,0BAAwB,MAAC;AAAA,IACnD,KAAK;AACH,4CAAQ,QAAA,EAAM,GAAG,OAAO,OAAc,0BAAwB,MAAC;AAAA,IACjE,KAAK;AACH,aACEA,2BAAAA;AAAAA,QAAC;AAAA,QAAA;AAAA,UACE,GAAG;AAAA,UACJ,yBAAyB,EAAE,QAAQ,SAAA;AAAA,UACnC;AAAA,QAAA;AAAA,MAAA;AAAA,IAGN,KAAK;AACH,aAAOA,2BAAAA,IAAC,QAAA,EAAO,OAAe,SAAA,CAAS;AAAA,IACzC;AACE,aAAO;AAAA,EAAA;AAEb;AAEA,SAAS,OAAO;AAAA,EACd;AAAA,EACA;AACF,GAGG;AACD,QAAM,SAASC,UAAAA,UAAA;AACf,QAAM,WAAWC,WAAAA,YAAA;AACjB,QAAM,aACJ,OAAO,OAAO,SAAS,YACvB,MAAM,SAAS,MACf,MAAM,SAAS,qBACf,MAAM,SAAS;AAEjB,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