xergic
xergic
NNuxt
Created by xergic on 5/30/2023 in #❓・help
Forward cookie to SSR useFetch on Cloudflare Workers
Hi, I'm deploying my Nuxt app to CF Workers and need to forward auth cookie to my API on SSR using useFetch.
<script setup>
const { data } = useCustomFetch('/some-route'}
</script>
<script setup>
const { data } = useCustomFetch('/some-route'}
</script>
useCustomFetch composable looks something like this (from the docs: https://nuxt.com/docs/getting-started/data-fetching#passing-headers-and-cookies):
export const useCustomFetch = async (endpoint: string) {
const baseURL = useRuntimeConfig().public.baseUrl
const headers = useRequestHeaders(['cookie'])
const { data, error, refresh } = await useFetch(endpoint, {
baseURL,
headers,
})

return {
data,
error,
refresh,
}
}
export const useCustomFetch = async (endpoint: string) {
const baseURL = useRuntimeConfig().public.baseUrl
const headers = useRequestHeaders(['cookie'])
const { data, error, refresh } = await useFetch(endpoint, {
baseURL,
headers,
})

return {
data,
error,
refresh,
}
}
The cookie is set on client side. It works when using client side fetch but not on SSR... I tried it with setting credentials: 'include' in useFetch options, but that does not work on CF Workers as this option is not implemented and apparently not needed (more on that here: https://github.com/cloudflare/workers-sdk/issues/2514). Am I doing it wrong? Any help would be greatly appreciated.
1 replies