Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

feat(nuxt): remove wrapper from client only components #6165

Merged
merged 23 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
c7dea42
fix(nuxt): make client only redefine the component instead of wrappin…
huang-julien Jul 26, 2022
25580cb
fix(nuxt): use ctx instead of state to avoid vue Error
huang-julien Jul 26, 2022
f77d169
refactor(nuxt): remove unused import
huang-julien Jul 26, 2022
e5b668f
fix(nuxt): wrap the render with h()
huang-julien Jul 26, 2022
7bb336c
Merge remote-tracking branch 'origin/main' into fix/create-client-onl…
danielroe Jul 27, 2022
a4b7150
refactor: use collision-resistant name and pass all attrs
danielroe Jul 27, 2022
9975293
test: add client-only component to fixture using ref + expose
danielroe Jul 27, 2022
d3fbb3c
fix: register callback before calling setup
danielroe Jul 27, 2022
0f87fd3
fix: refactor wholly into setup function
danielroe Jul 27, 2022
fb8798c
Merge branch 'main' into fix/create-client-only-wrapper
danielroe Jul 27, 2022
c963ba8
fix(nuxt): pass setup result to component render
huang-julien Jul 27, 2022
1a904b1
Update packages/nuxt/src/app/components/client-only.mjs
danielroe Jul 27, 2022
b08a875
fix(nuxt): conditionnal render on build-dev for client-only.mjs
Jul 28, 2022
8df797a
fix(nuxt): use render() and return setup result only in dev
huang-julien Jul 28, 2022
fea482e
fix(nuxt): keep original component structure for createClientOnly()
huang-julien Jul 28, 2022
294c077
fix(nuxt): fix inheritAttrs and handle template in createClientOnly
Jul 29, 2022
0431072
fix(nuxt): fix attrs inheritance
Jul 29, 2022
cacf259
Merge remote-tracking branch 'origin/main' into fix/create-client-onl…
danielroe Jul 29, 2022
860c716
refactor: pass other render args as is without naming
pi0 Aug 2, 2022
0bf46dd
use ctx to read prop
pi0 Aug 2, 2022
986bc42
refactor: rename state to __mounted__
pi0 Aug 2, 2022
3ff8bbb
Merge branch 'main' into fix/create-client-only-wrapper
pi0 Aug 2, 2022
4d400c7
revert state name
pi0 Aug 2, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions packages/nuxt/src/app/components/client-only.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ export default defineComponent({
})

export function createClientOnly (component) {
const { setup } = component
return defineComponent({
name: 'ClientOnlyWrapper',
inheritAttrs: false,
setup (_props, { attrs, slots }) {
...component,
setup (props, ctx) {
const mounted = ref(false)
onMounted(() => { mounted.value = true })
danielroe marked this conversation as resolved.
Show resolved Hide resolved
return () => {
if (mounted.value) {
return h(component, attrs, slots)
}
return h('div', { class: attrs.class, style: attrs.style })
}

return Promise.resolve(setup?.(props, ctx) || {})
.then((setupState) => {
const render = setupState && typeof setupState === 'function' ? res : component.render
danielroe marked this conversation as resolved.
Show resolved Hide resolved
return (ctx, cache, props, _, data, options) =>
mounted.value ? h(render(ctx, cache, props, setupState, data, options)) : h('div', ctx.$attrs)
})
}
})
}
3 changes: 3 additions & 0 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ describe('pages', () => {
expect(html).toContain('Composable | template: auto imported from ~/components/template.ts')
// should import components
expect(html).toContain('This is a custom component with a named export.')
// should apply attributes to client-only components
expect(html).toContain('<div style="color:red;" class="client-only"></div>')
// should register global components automatically
expect(html).toContain('global component registered automatically')
expect(html).toContain('global component via suffix')

Expand Down
17 changes: 17 additions & 0 deletions test/fixtures/basic/components/ClientWrapped.client.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<script setup lang="ts">
function exposedFunc () {
console.log('ok')
}

defineExpose({ exposedFunc })

await new Promise(resolve => setTimeout(resolve, 300))

onMounted(() => { console.log('mounted') })
</script>

<template>
<div>
client-only component
</div>
</template>
6 changes: 6 additions & 0 deletions test/fixtures/basic/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<CustomComponent />
<component :is="`test${'-'.toString()}global`" />
<component :is="`with${'-'.toString()}suffix`" />
<ClientWrapped ref="clientRef" style="color: red;" class="client-only" />
</div>
</template>

Expand All @@ -26,4 +27,9 @@ const config = useRuntimeConfig()

const foo = useFoo()
const bar = useBar()
const clientRef = ref()

onMounted(() => {
clientRef.value.exposedFunc()
})
</script>