Skip to content

Commit

Permalink
fix(ssr): throw on error in query
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Oct 31, 2024
1 parent 1dad29b commit 58b7f69
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
79 changes: 79 additions & 0 deletions src/ssr.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* @vitest-environment node
*/
import { describe, it, expect, vi } from 'vitest'
import { createSSRApp, defineComponent, onErrorCaptured } from 'vue'
import { renderToString, ssrRenderComponent, ssrRenderSuspense } from '@vue/server-renderer'
import type { UseQueryOptions } from './query-options'
import { isSpy } from '../test/utils'
import { useQuery } from './use-query'
import { PiniaColada } from './pinia-colada'
import { createPinia } from 'pinia'

describe('SSR', () => {
function renderApp<TResult = number, TError = Error>({
options = {},
appSetup,
}: {
options?: Partial<UseQueryOptions<TResult>>
appSetup?: () => void
} = {}) {
const query = options.query ? (isSpy(options.query) ? options.query : vi.fn(options.query)) : vi.fn(async () => 42)

const InnerComp = defineComponent({
render: () => null,
setup() {
const useQueryResult = useQuery<TResult, TError>({
key: ['key'],
...options,
// @ts-expect-error: generic unmatched but types work
query,
})
return {
...useQueryResult,
}
},
})
const App = defineComponent({
ssrRender(ctx: any, push: any, _parent: any) {
ssrRenderSuspense(push, {
default: () => {
push(ssrRenderComponent(InnerComp, null, null, _parent))
},
// @ts-expect-error: Vue type error?
_: 1 /* STABLE */,
})
},

setup() {
appSetup?.()
return {}
},
})

const app = createSSRApp(App)
const pinia = createPinia()
app.use(pinia)
app.use(PiniaColada, {})

return {
app,
query,
pinia,
}
}

it('works', async () => {
const spy = vi.fn()
const { app, query } = renderApp({
appSetup() {
onErrorCaptured(spy)
},
})
query.mockRejectedValueOnce(new Error('ko'))
expect(await renderToString(app)).toMatchInlineSnapshot(`"<!---->"`)
expect(spy).toHaveBeenCalledTimes(1)
})

// it('can avoid ')
})
2 changes: 1 addition & 1 deletion src/use-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export function useQuery<TResult, TError = ErrorDefault>(
if (hasCurrentInstance) {
// only happens on server, app awaits this
onServerPrefetch(async () => {
if (toValue(enabled)) await refresh()
if (toValue(enabled)) await refresh(true)
})
}

Expand Down

0 comments on commit 58b7f69

Please sign in to comment.