Skip to content

Commit

Permalink
fix: ensure client.fetch remains bound to instance on server (#994)
Browse files Browse the repository at this point in the history
  • Loading branch information
xuzuodong authored Apr 26, 2024
1 parent 9712cc6 commit 42951f5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
7 changes: 7 additions & 0 deletions playground/pages/fetch-from-server.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<template>
<div>{{ movies }}</div>
</template>

<script setup lang="ts">
const { data: movies } = await useFetch('/api/groq')
</script>
10 changes: 10 additions & 0 deletions playground/server/api/fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default defineEventHandler(() => {
const sanity = useSanity()

const query = groq`*[_type == "movie" && slug.current == "walle"][0] {
title,
"poster": poster.asset._ref,
"slug": slug.current
}`
return sanity.fetch(query)
})
2 changes: 1 addition & 1 deletion src/runtime/server/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const createSanityHelper = (options: ClientConfig): SanityHelper => {
return {
client,
config,
fetch: client.fetch,
fetch: client.fetch.bind(client),
setToken(token) {
config.token = token
client = createClient(config)
Expand Down
1 change: 1 addition & 0 deletions test/e2e/ssr.behaviour.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const ssrBehaviour = () => {

it('auto-imports work in server routes', async () => {
expect(await $fetch('/api/groq')).toContain('hey there')
expect(await $fetch('/api/fetch')).toEqual(expect.objectContaining({ slug: 'walle' }))
expect(await $fetch('/api/client')).toEqual(expect.objectContaining({
projectId: 'j1o4tmjp',
}))
Expand Down

0 comments on commit 42951f5

Please sign in to comment.