Skip to content

Commit

Permalink
feat(nuxt): add error handling in NuxtIsland
Browse files Browse the repository at this point in the history
  • Loading branch information
huang-julien committed Feb 14, 2024
1 parent 18063b2 commit 00ed376
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
8 changes: 8 additions & 0 deletions docs/3.api/1.components/8.nuxt-island.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,11 @@ Some slots are reserved to `NuxtIsland` for special cases.
- `refresh()`
- **type**: `() => Promise<void>`
- **description**: force refetch the server component by refetching it.

## Events

- `error`
- **parameters**:
- **error**:
- **type**: `unknown`
- **description**: emitted when when `NuxtIsland` fails to fetch the new island.
6 changes: 4 additions & 2 deletions packages/nuxt/src/app/components/nuxt-island.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ export default defineComponent({
default: false
}
},
async setup (props, { slots, expose }) {
emits: ['error'],
async setup (props, { slots, expose, emit }) {
let canTeleport = import.meta.server
const teleportKey = ref(0)
const key = ref(0)
Expand Down Expand Up @@ -207,11 +208,12 @@ export default defineComponent({
}
} catch (e) {
error.value = e
emit('error', e)
}
}

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

if (import.meta.hot) {
Expand Down
8 changes: 6 additions & 2 deletions packages/nuxt/src/components/runtime/server-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export const createServerComponent = (name: string) => {
name,
inheritAttrs: false,
props: { lazy: Boolean },
setup (props, { attrs, slots, expose }) {
emits: ['error'],
setup (props, { attrs, slots, expose, emit }) {
const islandRef = ref<null | typeof NuxtIsland>(null)

expose({
Expand All @@ -19,7 +20,10 @@ export const createServerComponent = (name: string) => {
name,
lazy: props.lazy,
props: attrs,
ref: islandRef
ref: islandRef,
onError: (err) => {
emit('error', err)
}
}, slots)
}
}
Expand Down
27 changes: 25 additions & 2 deletions test/nuxt/nuxt-island.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,29 @@ describe('runtime server component', () => {
expect(component.html()).toBe('<div>2</div>')
vi.mocked(fetch).mockReset()
})


it('expect NuxtIsland to emit an error', async () => {
const stubFetch = vi.fn(() => {
throw new Error('fetch error')
})

vi.stubGlobal('fetch', stubFetch)

const wrapper = await mountSuspended(createServerComponent('ErrorServerComponent'), {
props: {
name: 'Error',
props: {
force: true
}
},
attachTo: 'body'
})

expect(fetch).toHaveBeenCalledOnce()
expect(wrapper.emitted('error')).toHaveLength(1)
vi.mocked(fetch).mockReset()
})
})


Expand Down Expand Up @@ -166,7 +189,7 @@ describe('client components', () => {
expect(fetch).toHaveBeenCalledOnce()

expect(wrapper.html()).toMatchInlineSnapshot(`
"<div data-island-uid="3">hello<div data-island-uid="3" data-island-component="Client-12345">
"<div data-island-uid="4">hello<div data-island-uid="4" data-island-component="Client-12345">
<div>client component</div>
</div>
</div>
Expand All @@ -192,7 +215,7 @@ describe('client components', () => {
await wrapper.vm.$.exposed!.refresh()
await nextTick()
expect(wrapper.html()).toMatchInlineSnapshot( `
"<div data-island-uid="3">hello<div>
"<div data-island-uid="4">hello<div>
<div>fallback</div>
</div>
</div>"
Expand Down

0 comments on commit 00ed376

Please sign in to comment.