mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-18 17:35:57 +00:00
docs: get event before running async function (#8861)
This commit is contained in:
parent
84c26fa6fb
commit
571512833d
@ -238,9 +238,8 @@ The example below adds the request headers to an isomorphic `$fetch` call to ens
|
||||
|
||||
```vue
|
||||
<script setup>
|
||||
const { data } = await useFetch('/api/me', {
|
||||
headers: useRequestHeaders(['cookie'])
|
||||
})
|
||||
const headers = useRequestHeaders(['cookie'])
|
||||
const { data } = await useFetch('/api/me', { headers })
|
||||
</script>
|
||||
```
|
||||
|
||||
@ -262,11 +261,11 @@ Here is a list of common headers that are NOT to be proxied:
|
||||
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.
|
||||
|
||||
```ts [composables/fetch.ts]
|
||||
export const fetchWithCookie = async (url: string) => {
|
||||
export const fetchWithCookie = async (event: H3Event, url: string) => {
|
||||
const res = await $fetch.raw(url)
|
||||
const cookies = (res.headers.get('set-cookie') || '').split(',')
|
||||
for (const cookie of cookies) {
|
||||
appendHeader(useRequestEvent(), 'set-cookie', cookie)
|
||||
appendHeader(event, 'set-cookie', cookie)
|
||||
}
|
||||
return res._data
|
||||
}
|
||||
@ -275,7 +274,8 @@ export const fetchWithCookie = async (url: string) => {
|
||||
```vue
|
||||
<script setup lang="ts">
|
||||
// This composable will automatically pass cookies to the client
|
||||
const result = await fetchWithCookie('/api/with-cookie')
|
||||
const event = useRequestEvent()
|
||||
const result = await fetchWithCookie(event, '/api/with-cookie')
|
||||
onMounted(() => console.log(document.cookie))
|
||||
</script>
|
||||
```
|
||||
|
Loading…
Reference in New Issue
Block a user