-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nuxt): allow client components within
NuxtIsland
(#22649)
- Loading branch information
1 parent
802b3e2
commit 1b93e60
Showing
18 changed files
with
778 additions
and
107 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
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
62 changes: 62 additions & 0 deletions
62
packages/nuxt/src/app/components/nuxt-teleport-ssr-client.ts
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,62 @@ | ||
import type { Component, } from 'vue' | ||
import { Teleport, defineComponent, h } from 'vue' | ||
import { useNuxtApp } from '../nuxt' | ||
// @ts-expect-error virtual file | ||
import { paths } from '#build/components-chunk' | ||
|
||
type ExtendedComponent = Component & { | ||
__file: string, | ||
__name: string | ||
} | ||
|
||
/** | ||
* component only used with componentsIsland | ||
* this teleport the component in SSR only if it needs to be hydrated on client | ||
*/ | ||
export default defineComponent({ | ||
name: 'NuxtTeleportSsrClient', | ||
props: { | ||
to: { | ||
type: String, | ||
required: true | ||
}, | ||
nuxtClient: { | ||
type: Boolean, | ||
default: false | ||
}, | ||
/** | ||
* ONLY used in dev mode since we use build:manifest result in production | ||
* do not pass any value in production | ||
*/ | ||
rootDir: { | ||
type: String, | ||
default: null | ||
} | ||
}, | ||
setup (props, { slots }) { | ||
if (!props.nuxtClient) { return () => slots.default!() } | ||
|
||
const app = useNuxtApp() | ||
const islandContext = app.ssrContext!.islandContext! | ||
|
||
return () => { | ||
const slot = slots.default!()[0] | ||
const slotType = (slot.type as ExtendedComponent) | ||
const name = (slotType.__name || slotType.name) as string | ||
|
||
if (import.meta.dev) { | ||
const path = '_nuxt/' + paths[name] | ||
islandContext.chunks[name] = path | ||
} else { | ||
islandContext.chunks[name] = paths[name] | ||
} | ||
|
||
islandContext.propsData[props.to] = slot.props || {} | ||
|
||
return [h('div', { | ||
style: 'display: contents;', | ||
'nuxt-ssr-client': props.to | ||
}, []), h(Teleport, { to: props.to }, slot)] | ||
} | ||
} | ||
}) |
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.