2023-12-19 12:21:29 +00:00
|
|
|
import type { Component } from 'vue'
|
|
|
|
import { Fragment, Teleport, computed, createStaticVNode, createVNode, defineComponent, getCurrentInstance, h, nextTick, onMounted, ref, toRaw, watch } from 'vue'
|
2022-11-24 12:24:14 +00:00
|
|
|
import { debounce } from 'perfect-debounce'
|
|
|
|
import { hash } from 'ohash'
|
2023-05-01 22:55:24 +00:00
|
|
|
import { appendResponseHeader } from 'h3'
|
2023-03-10 08:01:21 +00:00
|
|
|
import { useHead } from '@unhead/vue'
|
2023-05-15 22:43:53 +00:00
|
|
|
import { randomUUID } from 'uncrypto'
|
2023-07-18 15:07:35 +00:00
|
|
|
import { joinURL, withQuery } from 'ufo'
|
2023-07-05 09:48:01 +00:00
|
|
|
import type { FetchResponse } from 'ofetch'
|
2023-12-19 12:21:29 +00:00
|
|
|
import { join } from 'pathe'
|
2023-06-25 16:38:15 +00:00
|
|
|
|
2022-11-24 12:24:14 +00:00
|
|
|
// eslint-disable-next-line import/no-restricted-paths
|
|
|
|
import type { NuxtIslandResponse } from '../../core/runtime/nitro/renderer'
|
2023-10-30 21:05:02 +00:00
|
|
|
import { useNuxtApp, useRuntimeConfig } from '../nuxt'
|
|
|
|
import { prerenderRoutes, useRequestEvent } from '../composables/ssr'
|
2023-05-15 22:43:53 +00:00
|
|
|
import { getFragmentHTML, getSlotProps } from './utils'
|
2022-11-24 12:24:14 +00:00
|
|
|
|
2023-07-31 12:01:50 +00:00
|
|
|
// @ts-expect-error virtual file
|
2023-12-19 12:21:29 +00:00
|
|
|
import { remoteComponentIslands, selectiveClient } from '#build/nuxt.config.mjs'
|
2023-07-31 12:01:50 +00:00
|
|
|
|
2022-11-24 12:24:14 +00:00
|
|
|
const pKey = '_islandPromises'
|
2023-05-15 22:43:53 +00:00
|
|
|
const SSR_UID_RE = /nuxt-ssr-component-uid="([^"]*)"/
|
|
|
|
const UID_ATTR = /nuxt-ssr-component-uid(="([^"]*)")?/
|
|
|
|
const SLOTNAME_RE = /nuxt-ssr-slot-name="([^"]*)"/g
|
|
|
|
const SLOT_FALLBACK_RE = /<div nuxt-slot-fallback-start="([^"]*)"[^>]*><\/div>(((?!<div nuxt-slot-fallback-end[^>]*>)[\s\S])*)<div nuxt-slot-fallback-end[^>]*><\/div>/g
|
2022-11-24 12:24:14 +00:00
|
|
|
|
2023-05-22 20:25:04 +00:00
|
|
|
let id = 0
|
2023-08-07 22:03:40 +00:00
|
|
|
const getId = import.meta.client ? () => (id++).toString() : randomUUID
|
2023-05-22 20:25:04 +00:00
|
|
|
|
2023-12-19 12:21:29 +00:00
|
|
|
const components = import.meta.client ? new Map<string, Component>() : undefined
|
|
|
|
|
|
|
|
async function loadComponents (source = '/', paths: Record<string, string>) {
|
|
|
|
const promises = []
|
|
|
|
|
|
|
|
for (const component in paths) {
|
|
|
|
if (!(components!.has(component))) {
|
|
|
|
promises.push((async () => {
|
|
|
|
const chunkSource = join(source, paths[component])
|
|
|
|
const c = await import(/* @vite-ignore */ chunkSource).then(m => m.default || m)
|
|
|
|
components!.set(component, c)
|
|
|
|
})())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await Promise.all(promises)
|
|
|
|
}
|
|
|
|
|
|
|
|
function emptyPayload () {
|
|
|
|
return {
|
|
|
|
chunks: {},
|
|
|
|
props: {},
|
|
|
|
teleports: {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-24 12:24:14 +00:00
|
|
|
export default defineComponent({
|
|
|
|
name: 'NuxtIsland',
|
|
|
|
props: {
|
|
|
|
name: {
|
|
|
|
type: String,
|
|
|
|
required: true
|
|
|
|
},
|
2023-07-31 08:51:09 +00:00
|
|
|
lazy: Boolean,
|
2022-11-24 12:24:14 +00:00
|
|
|
props: {
|
|
|
|
type: Object,
|
|
|
|
default: () => undefined
|
|
|
|
},
|
|
|
|
context: {
|
|
|
|
type: Object,
|
|
|
|
default: () => ({})
|
2023-07-30 21:00:41 +00:00
|
|
|
},
|
|
|
|
source: {
|
|
|
|
type: String,
|
|
|
|
default: () => undefined
|
2023-12-19 12:21:29 +00:00
|
|
|
},
|
|
|
|
dangerouslyLoadClientComponents: {
|
|
|
|
type: Boolean,
|
|
|
|
default: false
|
2022-11-24 12:24:14 +00:00
|
|
|
}
|
|
|
|
},
|
2023-12-14 11:07:54 +00:00
|
|
|
async setup (props, { slots, expose }) {
|
2023-12-19 12:21:29 +00:00
|
|
|
const key = ref(0)
|
|
|
|
const canLoadClientComponent = computed(() => selectiveClient && (props.dangerouslyLoadClientComponents || !props.source))
|
2023-07-30 21:00:41 +00:00
|
|
|
const error = ref<unknown>(null)
|
2023-07-18 15:07:35 +00:00
|
|
|
const config = useRuntimeConfig()
|
2022-11-24 12:24:14 +00:00
|
|
|
const nuxtApp = useNuxtApp()
|
2023-09-28 07:36:13 +00:00
|
|
|
const filteredProps = computed(() => props.props ? Object.fromEntries(Object.entries(props.props).filter(([key]) => !key.startsWith('data-v-'))) : {})
|
|
|
|
const hashId = computed(() => hash([props.name, filteredProps.value, props.context, props.source]))
|
2023-03-20 21:47:06 +00:00
|
|
|
const instance = getCurrentInstance()!
|
2023-01-20 12:10:58 +00:00
|
|
|
const event = useRequestEvent()
|
2023-12-19 12:21:29 +00:00
|
|
|
|
2023-07-05 09:48:01 +00:00
|
|
|
// TODO: remove use of `$fetch.raw` when nitro 503 issues on windows dev server are resolved
|
2023-08-07 22:03:40 +00:00
|
|
|
const eventFetch = import.meta.server ? event.fetch : import.meta.dev ? $fetch.raw : globalThis.fetch
|
2023-05-15 22:43:53 +00:00
|
|
|
const mounted = ref(false)
|
|
|
|
onMounted(() => { mounted.value = true })
|
2023-06-14 09:09:27 +00:00
|
|
|
|
2023-07-12 06:28:22 +00:00
|
|
|
function setPayload (key: string, result: NuxtIslandResponse) {
|
|
|
|
nuxtApp.payload.data[key] = {
|
|
|
|
__nuxt_island: {
|
|
|
|
key,
|
2023-08-07 22:03:40 +00:00
|
|
|
...(import.meta.server && import.meta.prerender)
|
2023-07-12 06:28:22 +00:00
|
|
|
? {}
|
2023-12-19 12:21:29 +00:00
|
|
|
: { params: { ...props.context, props: props.props ? JSON.stringify(props.props) : undefined } },
|
|
|
|
result: {
|
|
|
|
chunks: result.chunks,
|
|
|
|
props: result.props,
|
|
|
|
teleports: result.teleports
|
|
|
|
}
|
2023-07-12 06:28:22 +00:00
|
|
|
},
|
|
|
|
...result
|
|
|
|
}
|
|
|
|
}
|
2023-12-19 12:21:29 +00:00
|
|
|
// needs to be non-reactive because we don't want to trigger re-renders
|
|
|
|
// at hydration, we only retrieve props/chunks/teleports from payload. See the reviver at nuxt\src\app\plugins\revive-payload.client.ts
|
|
|
|
// If not hydrating, fetchComponent() will set it
|
|
|
|
const rawPayload = nuxtApp.isHydrating ? toRaw(nuxtApp.payload.data)?.[`${props.name}_${hashId.value}`] ?? emptyPayload() : emptyPayload()
|
|
|
|
|
|
|
|
const nonReactivePayload: Pick<NuxtIslandResponse, 'chunks'| 'props' | 'teleports'> = {
|
|
|
|
chunks: rawPayload.chunks,
|
|
|
|
props: rawPayload.props,
|
|
|
|
teleports: rawPayload.teleports
|
|
|
|
}
|
2023-07-12 06:28:22 +00:00
|
|
|
|
2023-07-31 08:51:09 +00:00
|
|
|
const ssrHTML = ref<string>('')
|
2023-12-19 12:21:29 +00:00
|
|
|
|
2023-08-07 22:03:40 +00:00
|
|
|
if (import.meta.client) {
|
2023-12-19 12:21:29 +00:00
|
|
|
ssrHTML.value = getFragmentHTML(instance.vnode?.el ?? null, true)?.join('') || ''
|
2023-07-12 06:28:22 +00:00
|
|
|
}
|
2023-12-19 12:21:29 +00:00
|
|
|
|
2023-06-27 09:38:40 +00:00
|
|
|
const slotProps = computed(() => getSlotProps(ssrHTML.value))
|
2023-09-05 10:27:00 +00:00
|
|
|
const uid = ref<string>(ssrHTML.value.match(SSR_UID_RE)?.[1] ?? getId())
|
2023-06-27 09:38:40 +00:00
|
|
|
const availableSlots = computed(() => [...ssrHTML.value.matchAll(SLOTNAME_RE)].map(m => m[1]))
|
2023-01-20 12:10:58 +00:00
|
|
|
|
2023-05-15 22:43:53 +00:00
|
|
|
const html = computed(() => {
|
|
|
|
const currentSlots = Object.keys(slots)
|
2023-12-19 12:21:29 +00:00
|
|
|
let html = ssrHTML.value
|
|
|
|
|
|
|
|
if (import.meta.client && !canLoadClientComponent.value) {
|
|
|
|
for (const [key, value] of Object.entries(nonReactivePayload.teleports || {})) {
|
|
|
|
html = html.replace(new RegExp(`<div [^>]*nuxt-ssr-client="${key}"[^>]*>`), (full) => {
|
|
|
|
return full + value
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return html.replace(SLOT_FALLBACK_RE, (full, slotName, content) => {
|
2023-05-15 22:43:53 +00:00
|
|
|
// remove fallback to insert slots
|
|
|
|
if (currentSlots.includes(slotName)) {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
return content
|
|
|
|
})
|
|
|
|
})
|
2023-12-19 12:21:29 +00:00
|
|
|
|
2023-05-15 22:43:53 +00:00
|
|
|
function setUid () {
|
2023-05-22 20:25:04 +00:00
|
|
|
uid.value = ssrHTML.value.match(SSR_UID_RE)?.[1] ?? getId() as string
|
2023-05-15 22:43:53 +00:00
|
|
|
}
|
2023-12-19 12:21:29 +00:00
|
|
|
|
2023-02-27 19:02:11 +00:00
|
|
|
const cHead = ref<Record<'link' | 'style', Array<Record<string, string>>>>({ link: [], style: [] })
|
2022-11-24 12:24:14 +00:00
|
|
|
useHead(cHead)
|
|
|
|
|
2023-07-04 04:21:27 +00:00
|
|
|
async function _fetchComponent (force = false) {
|
2023-06-19 22:06:46 +00:00
|
|
|
const key = `${props.name}_${hashId.value}`
|
2023-12-19 12:21:29 +00:00
|
|
|
|
|
|
|
if (nuxtApp.payload.data[key]?.html && !force) { return nuxtApp.payload.data[key] }
|
2023-06-14 09:09:27 +00:00
|
|
|
|
2023-10-20 15:58:02 +00:00
|
|
|
const url = remoteComponentIslands && props.source ? new URL(`/__nuxt_island/${key}.json`, props.source).href : `/__nuxt_island/${key}.json`
|
2023-07-30 21:00:41 +00:00
|
|
|
|
2023-08-07 22:03:40 +00:00
|
|
|
if (import.meta.server && import.meta.prerender) {
|
2023-01-20 12:10:58 +00:00
|
|
|
// Hint to Nitro to prerender the island component
|
2023-10-19 22:44:45 +00:00
|
|
|
nuxtApp.runWithContext(() => prerenderRoutes(url))
|
2023-01-20 12:10:58 +00:00
|
|
|
}
|
2022-11-24 12:24:14 +00:00
|
|
|
// TODO: Validate response
|
2023-07-18 15:07:35 +00:00
|
|
|
// $fetch handles the app.baseURL in dev
|
2023-09-08 19:48:42 +00:00
|
|
|
const r = await eventFetch(withQuery(((import.meta.dev && import.meta.client) || props.source) ? url : joinURL(config.app.baseURL ?? '', url), {
|
2023-06-25 16:38:15 +00:00
|
|
|
...props.context,
|
|
|
|
props: props.props ? JSON.stringify(props.props) : undefined
|
|
|
|
}))
|
2023-08-07 22:03:40 +00:00
|
|
|
const result = import.meta.server || !import.meta.dev ? await r.json() : (r as FetchResponse<NuxtIslandResponse>)._data
|
2023-06-25 16:38:15 +00:00
|
|
|
// TODO: support passing on more headers
|
2023-08-07 22:03:40 +00:00
|
|
|
if (import.meta.server && import.meta.prerender) {
|
2023-06-25 16:38:15 +00:00
|
|
|
const hints = r.headers.get('x-nitro-prerender')
|
|
|
|
if (hints) {
|
2023-10-19 22:44:45 +00:00
|
|
|
appendResponseHeader(event, 'x-nitro-prerender', hints)
|
2022-11-24 12:24:14 +00:00
|
|
|
}
|
2023-06-25 16:38:15 +00:00
|
|
|
}
|
2023-07-12 06:28:22 +00:00
|
|
|
setPayload(key, result)
|
2023-06-14 09:09:27 +00:00
|
|
|
return result
|
2022-11-24 12:24:14 +00:00
|
|
|
}
|
2023-12-19 12:21:29 +00:00
|
|
|
|
2023-07-04 04:21:27 +00:00
|
|
|
async function fetchComponent (force = false) {
|
2022-11-24 12:24:14 +00:00
|
|
|
nuxtApp[pKey] = nuxtApp[pKey] || {}
|
2023-05-15 22:43:53 +00:00
|
|
|
if (!nuxtApp[pKey][uid.value]) {
|
2023-07-04 04:21:27 +00:00
|
|
|
nuxtApp[pKey][uid.value] = _fetchComponent(force).finally(() => {
|
2023-05-15 22:43:53 +00:00
|
|
|
delete nuxtApp[pKey]![uid.value]
|
2022-11-24 12:24:14 +00:00
|
|
|
})
|
|
|
|
}
|
2023-07-30 21:00:41 +00:00
|
|
|
try {
|
|
|
|
const res: NuxtIslandResponse = await nuxtApp[pKey][uid.value]
|
|
|
|
cHead.value.link = res.head.link
|
|
|
|
cHead.value.style = res.head.style
|
|
|
|
ssrHTML.value = res.html.replace(UID_ATTR, () => {
|
|
|
|
return `nuxt-ssr-component-uid="${getId()}"`
|
|
|
|
})
|
|
|
|
key.value++
|
|
|
|
error.value = null
|
2023-12-19 12:21:29 +00:00
|
|
|
|
|
|
|
if (selectiveClient && import.meta.client) {
|
|
|
|
if (canLoadClientComponent.value && res.chunks) {
|
|
|
|
await loadComponents(props.source, res.chunks)
|
|
|
|
}
|
|
|
|
nonReactivePayload.props = res.props
|
|
|
|
}
|
|
|
|
nonReactivePayload.teleports = res.teleports
|
|
|
|
nonReactivePayload.chunks = res.chunks
|
|
|
|
|
2023-08-07 22:03:40 +00:00
|
|
|
if (import.meta.client) {
|
2023-07-30 21:00:41 +00:00
|
|
|
// must await next tick for Teleport to work correctly with static node re-rendering
|
|
|
|
await nextTick()
|
|
|
|
}
|
|
|
|
setUid()
|
|
|
|
} catch (e) {
|
|
|
|
error.value = e
|
2023-05-15 22:43:53 +00:00
|
|
|
}
|
2022-11-24 12:24:14 +00:00
|
|
|
}
|
|
|
|
|
2023-12-14 11:07:54 +00:00
|
|
|
expose({
|
|
|
|
refresh: () => fetchComponent(true)
|
|
|
|
})
|
|
|
|
|
2023-07-04 04:21:27 +00:00
|
|
|
if (import.meta.hot) {
|
|
|
|
import.meta.hot.on(`nuxt-server-component:${props.name}`, () => {
|
|
|
|
fetchComponent(true)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-07 22:03:40 +00:00
|
|
|
if (import.meta.client) {
|
2023-07-04 04:21:27 +00:00
|
|
|
watch(props, debounce(() => fetchComponent(), 100))
|
2022-11-24 12:24:14 +00:00
|
|
|
}
|
|
|
|
|
2023-08-07 22:03:40 +00:00
|
|
|
if (import.meta.client && !nuxtApp.isHydrating && props.lazy) {
|
2023-07-31 08:51:09 +00:00
|
|
|
fetchComponent()
|
2023-08-28 16:46:49 +00:00
|
|
|
} else if (import.meta.server || !nuxtApp.isHydrating || !nuxtApp.payload.serverRendered) {
|
2023-01-14 01:13:48 +00:00
|
|
|
await fetchComponent()
|
2023-12-19 12:21:29 +00:00
|
|
|
} else if (selectiveClient && canLoadClientComponent.value && nonReactivePayload.chunks) {
|
|
|
|
await loadComponents(props.source, nonReactivePayload.chunks)
|
2023-01-14 01:13:48 +00:00
|
|
|
}
|
2023-04-20 21:41:20 +00:00
|
|
|
|
2023-05-15 22:43:53 +00:00
|
|
|
return () => {
|
2023-12-19 12:21:29 +00:00
|
|
|
if (!html.value || error.value) {
|
|
|
|
return [slots.fallback?.({ error: error.value }) ?? createVNode('div')]
|
2023-07-30 21:00:41 +00:00
|
|
|
}
|
2023-05-15 22:43:53 +00:00
|
|
|
const nodes = [createVNode(Fragment, {
|
|
|
|
key: key.value
|
2023-07-31 08:51:09 +00:00
|
|
|
}, [h(createStaticVNode(html.value || '<div></div>', 1))])]
|
2023-12-19 12:21:29 +00:00
|
|
|
|
|
|
|
if (uid.value && (mounted.value || nuxtApp.isHydrating || import.meta.server) && html.value) {
|
2023-05-15 22:43:53 +00:00
|
|
|
for (const slot in slots) {
|
|
|
|
if (availableSlots.value.includes(slot)) {
|
2023-08-07 22:03:40 +00:00
|
|
|
nodes.push(createVNode(Teleport, { to: import.meta.client ? `[nuxt-ssr-component-uid='${uid.value}'] [nuxt-ssr-slot-name='${slot}']` : `uid=${uid.value};slot=${slot}` }, {
|
2023-05-15 22:43:53 +00:00
|
|
|
default: () => (slotProps.value[slot] ?? [undefined]).map((data: any) => slots[slot]?.(data))
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
2023-12-19 12:21:29 +00:00
|
|
|
if (import.meta.server) {
|
|
|
|
for (const [id, html] of Object.entries(nonReactivePayload.teleports ?? {})) {
|
|
|
|
nodes.push(createVNode(Teleport, { to: `uid=${uid.value};client=${id}` }, {
|
|
|
|
default: () => [createStaticVNode(html, 1)]
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (selectiveClient && import.meta.client && canLoadClientComponent.value) {
|
|
|
|
for (const [id, props] of Object.entries(nonReactivePayload.props ?? {})) {
|
|
|
|
const component = components!.get(id.split('-')[0])!
|
|
|
|
const vnode = createVNode(Teleport, { to: `[nuxt-ssr-component-uid='${uid.value}'] [nuxt-ssr-client="${id}"]` }, {
|
|
|
|
default: () => {
|
|
|
|
return [h(component, props)]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
nodes.push(vnode)
|
|
|
|
}
|
|
|
|
}
|
2023-05-15 22:43:53 +00:00
|
|
|
}
|
|
|
|
return nodes
|
2023-04-20 21:41:20 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-15 22:43:53 +00:00
|
|
|
})
|