import { appRootTag } from "nuxt/internal/renderer-config"; //#region src/runtime/server/renderer/islands.ts const ROOT_NODE_REGEX = new RegExp(`^<${appRootTag}[^>]*>([\\s\\S]*)<\\/${appRootTag}>$`); /** * remove the root node from the html body */ function getServerComponentHTML(body) { return body.match(ROOT_NODE_REGEX)?.[1] || body; } const SSR_SLOT_TELEPORT_MARKER = /^uid=([^;]*);slot=(.*)$/; const SSR_CLIENT_TELEPORT_MARKER = /^uid=([^;]*);client=(.*)$/; const SSR_CLIENT_SLOT_MARKER = /^island-slot=([^;]*);(.*)$/; function getSlotIslandResponse(ssrContext) { if (!ssrContext.islandContext || !Object.keys(ssrContext.islandContext.slots).length) return; const response = {}; for (const [name, slot] of Object.entries(ssrContext.islandContext.slots)) response[name] = { ...slot, fallback: ssrContext.teleports?.[`island-fallback=${name}`] }; return response; } function getClientIslandResponse(ssrContext) { if (!ssrContext.islandContext || !Object.keys(ssrContext.islandContext.components).length) return; const response = {}; for (const [clientUid, component] of Object.entries(ssrContext.islandContext.components)) { let html = ssrContext.teleports?.[clientUid]?.replaceAll("", "") || ""; if (!html && ssrContext.teleports) for (const [key, value] of Object.entries(ssrContext.teleports)) { const [, , componentUid] = key.match(SSR_CLIENT_TELEPORT_MARKER) ?? []; if (componentUid === clientUid) { html = value.replaceAll("", ""); break; } } response[clientUid] = { ...component, html, slots: getComponentSlotTeleport(clientUid, ssrContext.teleports ?? {}) }; } return response; } function getComponentSlotTeleport(clientUid, teleports) { const entries = Object.entries(teleports); const slots = {}; for (const [key, value] of entries) { const match = key.match(SSR_CLIENT_SLOT_MARKER); if (match) { const [, id, slot] = match; if (!slot || clientUid !== id) continue; slots[slot] = value; } } return slots; } const ISLAND_TELEPORT_RELOCATE_SCRIPT = `(()=>{for(const t of document.querySelectorAll('template[data-island-uid]')){const u=t.getAttribute('data-island-uid'),s=t.getAttribute('data-island-slot'),c=t.getAttribute('data-island-component'),a=document.querySelector('[data-island-uid="'+u+'"]'+(s!==null?'[data-island-slot="'+s+'"]':'[data-island-component="'+c+'"]'));if(a){a.insertBefore(t.content,a.firstChild)}t.remove()}})()`; /** * Emit each island teleport as an inert `