Skip to content

Commit

Permalink
feat(nuxt): expose refresh on islands and server components (#24261)
Browse files Browse the repository at this point in the history
  • Loading branch information
huang-julien committed Dec 14, 2023
1 parent 1711c33 commit 17b5ed9
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
6 changes: 6 additions & 0 deletions docs/3.api/1.components/8.nuxt-island.md
Expand Up @@ -50,3 +50,9 @@ Every slot is interactive since the parent component is the one providing it.
Some slots are reserved to `NuxtIsland` for special cases.

- `#fallback`: Specify the content to be rendered before the island loads (if the component is lazy) or if `NuxtIsland` fails to fetch the component.

## Ref

- `refresh()`
- **type**: `() => Promise<void>`
- **description**: force refetch the server component by refetching it.
6 changes: 5 additions & 1 deletion packages/nuxt/src/app/components/nuxt-island.ts
Expand Up @@ -46,7 +46,7 @@ export default defineComponent({
default: () => undefined
}
},
async setup (props, { slots }) {
async setup (props, { slots, expose }) {
const error = ref<unknown>(null)
const config = useRuntimeConfig()
const nuxtApp = useNuxtApp()
Expand Down Expand Up @@ -160,6 +160,10 @@ export default defineComponent({
}
}

expose({
refresh: () => fetchComponent(true)
})

if (import.meta.hot) {
import.meta.hot.on(`nuxt-server-component:${props.name}`, () => {
fetchComponent(true)
Expand Down
13 changes: 10 additions & 3 deletions packages/nuxt/src/components/runtime/server-component.ts
@@ -1,4 +1,4 @@
import { defineComponent, h } from 'vue'
import { defineComponent, h, ref } from 'vue'
import NuxtIsland from '#app/components/nuxt-island'

/*@__NO_SIDE_EFFECTS__*/
Expand All @@ -7,12 +7,19 @@ export const createServerComponent = (name: string) => {
name,
inheritAttrs: false,
props: { lazy: Boolean },
setup (props, { attrs, slots }) {
setup (props, { attrs, slots, expose }) {
const islandRef = ref<null | typeof NuxtIsland>(null)

expose({
refresh: () => islandRef.value?.refresh()
})

return () => {
return h(NuxtIsland, {
name,
lazy: props.lazy,
props: attrs
props: attrs,
ref: islandRef
}, slots)
}
}
Expand Down
33 changes: 32 additions & 1 deletion test/nuxt/nuxt-island.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it, vi } from 'vitest'
import { h } from 'vue'
import { h, nextTick } from 'vue'
import { mountSuspended } from '@nuxt/test-utils/runtime'
import { createServerComponent } from '../../packages/nuxt/src/components/runtime/server-component'
import { createSimpleRemoteIslandProvider } from '../fixtures/remote-provider'
Expand Down Expand Up @@ -65,4 +65,35 @@ describe('runtime server component', () => {

await server.close()
})

it('force refresh', async () => {
let count = 0
const stubFetch = vi.fn(() => {
count++
return {
id: '123',
html: `<div>${count}</div>`,
state: {},
head: {
link: [],
style: []
},
json() {
return this
}
}
})
vi.stubGlobal('fetch', stubFetch)

const component = await mountSuspended(createServerComponent('dummyName'))
expect(fetch).toHaveBeenCalledOnce()

expect(component.html()).toBe('<div>1</div>')

await component.vm.$.exposed!.refresh()
expect(fetch).toHaveBeenCalledTimes(2)
await nextTick()
expect(component.html()).toBe('<div>2</div>')
vi.mocked(fetch).mockRestore()
})
})

0 comments on commit 17b5ed9

Please sign in to comment.