This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nuxt): experimental server component islands (#5689)
Co-authored-by: Pooya Parsa <[email protected]>
- Loading branch information
Showing
23 changed files
with
533 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { createBlock, defineComponent, h, Teleport } from 'vue' | ||
|
||
// @ts-ignore | ||
import * as islandComponents from '#build/components.islands.mjs' | ||
import { createError } from '#app' | ||
|
||
export default defineComponent({ | ||
props: { | ||
context: { | ||
type: Object as () => { name: string, props?: Record<string, any> }, | ||
required: true | ||
} | ||
}, | ||
async setup (props) { | ||
// TODO: https://github.com/vuejs/core/issues/6207 | ||
const component = islandComponents[props.context.name] | ||
|
||
if (!component) { | ||
throw createError({ | ||
statusCode: 404, | ||
statusMessage: `Island component not found: ${JSON.stringify(component)}` | ||
}) | ||
} | ||
|
||
if (typeof component === 'object') { | ||
await component.__asyncLoader?.() | ||
} | ||
|
||
return () => [ | ||
createBlock(Teleport as any, { to: 'nuxt-island' }, [h(component || 'span', props.context.props)]) | ||
] | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { defineComponent, createStaticVNode, computed, ref, watch } from 'vue' | ||
import { debounce } from 'perfect-debounce' | ||
import { hash } from 'ohash' | ||
import type { MetaObject } from '@nuxt/schema' | ||
// eslint-disable-next-line import/no-restricted-paths | ||
import type { NuxtIslandResponse } from '../../core/runtime/nitro/renderer' | ||
import { useHead, useNuxtApp } from '#app' | ||
|
||
const pKey = '_islandPromises' | ||
|
||
export default defineComponent({ | ||
name: 'NuxtIsland', | ||
props: { | ||
name: { | ||
type: String, | ||
required: true | ||
}, | ||
props: { | ||
type: Object, | ||
default: () => undefined | ||
}, | ||
context: { | ||
type: Object, | ||
default: () => ({}) | ||
} | ||
}, | ||
async setup (props) { | ||
const nuxtApp = useNuxtApp() | ||
const hashId = computed(() => hash([props.name, props.props, props.context])) | ||
const html = ref<string>('') | ||
const cHead = ref<MetaObject>({ link: [], style: [] }) | ||
useHead(cHead) | ||
|
||
function _fetchComponent () { | ||
// TODO: Validate response | ||
return $fetch<NuxtIslandResponse>(`/__nuxt_island/${props.name}:${hashId.value}`, { | ||
params: { | ||
...props.context, | ||
props: props.props ? JSON.stringify(props.props) : undefined | ||
} | ||
}) | ||
} | ||
|
||
async function fetchComponent () { | ||
nuxtApp[pKey] = nuxtApp[pKey] || {} | ||
if (!nuxtApp[pKey][hashId.value]) { | ||
nuxtApp[pKey][hashId.value] = _fetchComponent().finally(() => { | ||
delete nuxtApp[pKey][hashId.value] | ||
}) | ||
} | ||
const res: NuxtIslandResponse = await nuxtApp[pKey][hashId.value] | ||
cHead.value.link = res.head.link | ||
cHead.value.style = res.head.style | ||
html.value = res.html | ||
} | ||
|
||
if (process.server || !nuxtApp.isHydrating) { | ||
await fetchComponent() | ||
} | ||
|
||
if (process.client) { | ||
watch(props, debounce(fetchComponent, 100)) | ||
} | ||
|
||
return () => createStaticVNode(html.value, 1) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.