// snippets/Loading/SSR-Hydration-Data-Analysis.js | sha256:eb254bf7a31cac66 | https://github.com/nucliweb/webperf-snippets/blob/main/snippets/Loading/SSR-Hydration-Data-Analysis.js (()=>{function e(e){if(null==e||Number.isNaN(e))return"-";if(0===e)return"0 B";const t=["B","KB","MB","GB"],s=Math.max(0,Math.min(Math.floor(Math.log(e)/Math.log(1024)),t.length-1));return(e/Math.pow(1024,s)).toFixed(1)+" "+t[s]}const t=[],s=Array.from(document.querySelectorAll("script:not([src])")).filter(e=>e.innerHTML.trim().length>0);[{name:"Next.js",selector:"#__NEXT_DATA__",type:"json",threshold:131072,docs:"https://nextjs.org/docs/messages/large-page-data"},{name:"Nuxt 3",selector:"#__NUXT_DATA__",type:"json-array",threshold:102400,docs:"https://nuxt.com/docs/api/composables/use-hydration"},{name:"Nuxt 2",pattern:/window\.__NUXT__\s*=/,type:"script",threshold:102400,docs:"https://nuxtjs.org/"},{name:"Remix",selector:"#__remixContext",type:"script",threshold:102400,docs:"https://remix.run/docs/en/main/guides/performance"},{name:"Gatsby",selector:"#___gatsby",pattern:/window\.___/,type:"script",threshold:102400,docs:"https://www.gatsbyjs.com/docs/"},{name:"SvelteKit",selector:"[data-sveltekit-hydrate]",type:"json",threshold:102400,docs:"https://kit.svelte.dev/docs/performance"},{name:"Astro",selector:"astro-island",type:"props",threshold:51200,docs:"https://docs.astro.build/en/concepts/islands/"}].forEach(e=>{let n=[],r="",o=0;if(e.selector){const t=document.querySelector(e.selector);t&&(n=[t],r=t.innerHTML||t.textContent||"",o=new Blob([r]).size)}if(e.pattern&&0===n.length&&s.forEach(t=>{e.pattern.test(t.innerHTML)&&(n.push(t),r=t.innerHTML,o+=new Blob([r]).size)}),"Astro"===e.name){const e=document.querySelectorAll("astro-island");if(e.length>0){n=Array.from(e);let t=0;e.forEach(e=>{const s=e.getAttribute("props");s&&(t+=new Blob([s]).size)}),o=t,r=`${e.length} islands with props`}}n.length>0&&t.push({...e,elements:n,content:r,size:o,exceedsThreshold:o>e.threshold})});const n=new Set(t.flatMap(e=>e.elements)),r=s.filter(e=>!n.has(e)).reduce((e,t)=>e+new Blob([t.innerHTML]).size,0);if(0===t.length)return{script:"SSR-Hydration-Data-Analysis",status:"ok",count:0,details:{frameworksFound:0,totalHydrationBytes:0,otherInlineBytes:r},items:[],issues:[]};t.forEach(e=>{});const o=t.reduce((e,t)=>e+t.size,0),a=[],i=[];return t.forEach(t=>{if(t.exceedsThreshold&&i.push({code:"large-hydration-data",severity:"warning",message:`${t.name} hydration data (${Math.round(t.size/1024)} KB) exceeds ${Math.round(t.threshold/1024)} KB threshold`,target:t.name}),"json"===t.type&&t.content)try{const e=JSON.parse(t.content);if("Next.js"===t.name&&e.props){const t=e.props?.pageProps||{},s=new Blob([JSON.stringify(t)]).size,n=Object.keys(t);if(n.length>0){const e=n.map(e=>({key:e,size:new Blob([JSON.stringify(t[e])]).size}));e.sort((e,t)=>t.size-e.size),e.slice(0,10).forEach(e=>{const t=(e.size/s*100).toFixed(1);"█".repeat(Math.min(Math.round(parseFloat(t)/5),20))})}const r=n.filter(e=>{const s=t[e];return Array.isArray(s)&&s.length>50}),o=(e,t=0)=>t>5||"object"==typeof e&&null!==e&&Object.values(e).some(e=>o(e,t+1));o(t);const l=/password|secret|token|apikey|api_key|private/i,c=JSON.stringify(t);l.test(c);const h=[],p=(e,t,s)=>{if(!("object"!=typeof e||null===e||s>4))for(const n of Object.keys(e)){const r=t?`${t}.${n}`:n;l.test(n)&&h.push(r),p(e[n],r,s+1)}};p(t,"",0),n.forEach(e=>{const s=[];r.includes(e)&&s.push("large-array"),o(t[e])&&s.push("deeply-nested"),h.some(t=>t===e||t.startsWith(`${e}.`))&&s.push("sensitive-key"),a.push({framework:"Next.js",kind:"prop",name:e,sizeBytes:new Blob([JSON.stringify(t[e])]).size,flags:s,detail:null})}),r.length>0&&i.push({code:"large-arrays",severity:"warning",message:`pageProps has large arrays (${r.join(", ")}). Consider pagination.`}),o(t)&&i.push({code:"deep-nesting",severity:"warning",message:"pageProps contains deeply nested data (more than 5 levels)."}),h.length>0&&i.push({code:"sensitive-keys",severity:"warning",message:`pageProps has keys that look sensitive (${h.slice(0,5).join(", ")}). Everything in pageProps is sent to every visitor.`}),0===r.length&&!o(t)&&l.test(c)}}catch{}"Astro"===t.name&&(t.elements.map((t,s)=>{const n=t.getAttribute("props"),r=t.getAttribute("client")||Array.from(t.attributes).find(e=>e.name.startsWith("client:"))?.name.replace("client:","")||"unknown";return{"#":s+1,Component:t.getAttribute("component-url")?.split("/").pop()||"Unknown",Client:r,"Props Size":n?e(new Blob([n]).size):"0 B"}}),t.elements.forEach(e=>{const t=e.getAttribute("props");a.push({framework:"Astro",kind:"island",name:e.getAttribute("component-url")?.split("/").pop()||"Unknown",sizeBytes:t?new Blob([t]).size:0,flags:[],detail:e.getAttribute("client")||Array.from(e.attributes).find(e=>e.name.startsWith("client:"))?.name.replace("client:","")||"unknown"})})),t.elements.forEach((e,t)=>{})}),t.some(e=>e.exceedsThreshold)&&t.find(e=>"Next.js"===e.name),{script:"SSR-Hydration-Data-Analysis",status:"ok",count:t.length,details:{frameworksFound:t.length,totalHydrationBytes:o,otherInlineBytes:r,hasExceedingThreshold:t.some(e=>e.exceedsThreshold),frameworks:t.map(e=>({name:e.name,sizeBytes:e.size,thresholdBytes:e.threshold,exceedsThreshold:e.exceedsThreshold}))},items:a.sort((e,t)=>t.sizeBytes-e.sizeBytes).slice(0,30),issues:i}})();