UNPKG

@tanstack/solid-router

Version:

Modern and scalable routing for Solid applications

1 lines 4.03 kB
{"version":3,"file":"Scripts.cjs","names":["Solid","isServer","Asset","useRouter","AnyRouteMatch","RouterManagedTag","Scripts","router","nonce","options","ssr","getAssetScripts","matches","Array","assetScripts","manifest","match","scripts","routes","routeId","asset","push","tag","attrs","children","getScripts","map","flat","filter","Boolean","script","activeMatches","createMemo","stores","get","renderScripts","ReturnType","allScripts","serverSsr","serverBufferedScript","takeBufferedScripts","unshift","_$memo","_$createComponent"],"sources":["../../src/Scripts.tsx"],"sourcesContent":["import * as Solid from 'solid-js'\nimport { isServer } from '@tanstack/router-core/isServer'\nimport { Asset } from './Asset'\nimport { useRouter } from './useRouter'\nimport type { AnyRouteMatch, RouterManagedTag } from '@tanstack/router-core'\n\nexport const Scripts = () => {\n const router = useRouter()\n const nonce = router.options.ssr?.nonce\n\n const getAssetScripts = (matches: Array<AnyRouteMatch>) => {\n const assetScripts: Array<RouterManagedTag> = []\n const manifest = router.ssr?.manifest\n\n if (!manifest) {\n return []\n }\n\n for (const match of matches) {\n const scripts = manifest.routes[match.routeId]?.scripts\n\n if (!scripts) {\n continue\n }\n\n for (const asset of scripts) {\n assetScripts.push({\n tag: 'script',\n attrs: { ...asset.attrs, nonce },\n children: asset.children,\n })\n }\n }\n\n return assetScripts\n }\n\n const getScripts = (matches: Array<AnyRouteMatch>): Array<RouterManagedTag> =>\n (\n matches\n .map((match) => match.scripts!)\n .flat(1)\n .filter(Boolean) as Array<RouterManagedTag>\n ).map(\n ({ children, ...script }) =>\n ({\n tag: 'script',\n attrs: {\n ...script,\n nonce,\n },\n children,\n }) satisfies RouterManagedTag,\n )\n\n const activeMatches = Solid.createMemo(() => router.stores.matches.get())\n const assetScripts = Solid.createMemo(() => getAssetScripts(activeMatches()))\n const scripts = Solid.createMemo(() => getScripts(activeMatches()))\n\n return renderScripts(router, scripts(), assetScripts())\n}\n\nfunction renderScripts(\n router: ReturnType<typeof useRouter>,\n scripts: Array<RouterManagedTag>,\n assetScripts: Array<RouterManagedTag>,\n) {\n const allScripts = [...scripts, ...assetScripts] as Array<RouterManagedTag>\n\n if ((isServer ?? router.isServer) && router.serverSsr) {\n const serverBufferedScript = router.serverSsr.takeBufferedScripts()\n if (serverBufferedScript) {\n allScripts.unshift(serverBufferedScript)\n }\n }\n\n return (\n <>\n {allScripts.map((asset) => (\n <Asset {...asset} />\n ))}\n </>\n )\n}\n"],"mappings":";;;;;;;;AAMA,IAAaM,gBAAgB;CAC3B,MAAMC,SAASJ,kBAAAA,UAAU;CACzB,MAAMK,QAAQD,OAAOE,QAAQC,KAAKF;CAElC,MAAMG,mBAAmBC,YAAkC;EACzD,MAAME,eAAwC,CAAA;EAC9C,MAAMC,WAAWR,OAAOG,KAAKK;EAE7B,IAAI,CAACA,UACH,OAAO,CAAA;EAGT,KAAK,MAAMC,SAASJ,SAAS;GAC3B,MAAMK,UAAUF,SAASG,OAAOF,MAAMG,UAAUF;GAEhD,IAAI,CAACA,SACH;GAGF,KAAK,MAAMG,SAASH,SAClBH,aAAaO,KAAK;IAChBC,KAAK;IACLC,OAAO;KAAE,GAAGH,MAAMG;KAAOf;IAAM;IAC/BgB,UAAUJ,MAAMI;GAClB,CAAC;EAEL;EAEA,OAAOV;CACT;CAEA,MAAMW,cAAcb,YAEhBA,QACGc,KAAKV,UAAUA,MAAMC,OAAQ,EAC7BU,KAAK,CAAC,EACNC,OAAOC,OAAO,EACjBH,KACC,EAAEF,UAAU,GAAGM,cACb;EACCR,KAAK;EACLC,OAAO;GACL,GAAGO;GACHtB;EACF;EACAgB;CACF,EACJ;CAEF,MAAMO,gBAAgB/B,SAAMgC,iBAAiBzB,OAAO0B,OAAOrB,QAAQsB,IAAI,CAAC;CACxE,MAAMpB,eAAed,SAAMgC,iBAAiBrB,gBAAgBoB,cAAc,CAAC,CAAC;CAG5E,OAAOI,cAAc5B,QAFLP,SAAMgC,iBAAiBP,WAAWM,cAAc,CAAC,CAEpCd,EAAQ,GAAGH,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