mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-19 01:45:53 +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
|
```vue
|
||||||
<script setup>
|
<script setup>
|
||||||
const { data } = await useFetch('/api/me', {
|
const headers = useRequestHeaders(['cookie'])
|
||||||
headers: useRequestHeaders(['cookie'])
|
const { data } = await useFetch('/api/me', { headers })
|
||||||
})
|
|
||||||
</script>
|
</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.
|
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]
|
```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 res = await $fetch.raw(url)
|
||||||
const cookies = (res.headers.get('set-cookie') || '').split(',')
|
const cookies = (res.headers.get('set-cookie') || '').split(',')
|
||||||
for (const cookie of cookies) {
|
for (const cookie of cookies) {
|
||||||
appendHeader(useRequestEvent(), 'set-cookie', cookie)
|
appendHeader(event, 'set-cookie', cookie)
|
||||||
}
|
}
|
||||||
return res._data
|
return res._data
|
||||||
}
|
}
|
||||||
@ -275,7 +274,8 @@ export const fetchWithCookie = async (url: string) => {
|
|||||||
```vue
|
```vue
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
// This composable will automatically pass cookies to the client
|
// 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))
|
onMounted(() => console.log(document.cookie))
|
||||||
</script>
|
</script>
|
||||||
```
|
```
|
||||||
|
Loading…
Reference in New Issue
Block a user