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

Commit 2d2b1ca

Browse files
committed
simplify
1 parent 12784ef commit 2d2b1ca

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

docs/content/2.guide/2.features/5.data-fetching.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -212,24 +212,20 @@ Here is a list of common headers that are NOT to be proxied:
212212
If you want to pass on/proxy cookies in the other direction, from an internal request back to the client, you will need to handle this yourself.
213213

214214
```ts [composables/fetch.ts]
215-
export const fetchWithCookie = async (url: string, cookieName: string) => {
216-
const response = await $fetch.raw(url)
217-
if (process.server) {
218-
const cookies = Object.fromEntries(
219-
response.headers.get('set-cookie')?.split('; ').map((a) => a.split('='))
220-
)
221-
if (cookieName in cookies) {
222-
useCookie(cookieName).value = cookies[cookieName]
223-
}
215+
export const fetchWithCookie = async (url: string) => {
216+
const res = await $fetch.raw(url)
217+
const cookies = (res.headers.get('set-cookie') || '').split(',')
218+
for (const cookie of cookies) {
219+
appendHeader(useRequestEvent(), 'set-cookie', cookie)
224220
}
225-
return response._data
221+
return res._data
226222
}
227223
```
228224

229225
```vue
230226
<script setup lang="ts">
231-
// This composable will automatically pass on a cookie of our choice.
232-
const result = await fetchWithCookie("/api/with-cookie", "test")
227+
// This composable will automatically pass cookies to the client
228+
const result = await fetchWithCookie("/api/with-cookie")
233229
onMounted(() => console.log(document.cookie))
234230
</script>
235231
```

0 commit comments

Comments
 (0)