From a49d0773ad6a8d2bb27186ebcd36b5fa8127f188 Mon Sep 17 00:00:00 2001 From: Mourad EL CADI Date: Thu, 7 Jul 2022 16:27:12 +0100 Subject: [PATCH 1/2] cookies value-keys have a '; ' separator cookies have this shape ==> `Set-Cookie: =; Domain=; Secure; ` --- docs/content/2.guide/2.features/5.data-fetching.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/content/2.guide/2.features/5.data-fetching.md b/docs/content/2.guide/2.features/5.data-fetching.md index 73de0a94ba6..2134e6be358 100644 --- a/docs/content/2.guide/2.features/5.data-fetching.md +++ b/docs/content/2.guide/2.features/5.data-fetching.md @@ -216,7 +216,7 @@ export const fetchWithCookie = async (url: string, cookieName: string) => { const response = await $fetch.raw(url) if (process.server) { const cookies = Object.fromEntries( - response.headers.get('set-cookie')?.split(',').map((a) => a.split('=')) + response.headers.get('set-cookie')?.split('; ').map((a) => a.split('=')) ) if (cookieName in cookies) { useCookie(cookieName).value = cookies[cookieName] From 2d2b1ca66c61def27ae740cd1176907379f68178 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Mon, 15 Aug 2022 16:51:57 +0200 Subject: [PATCH 2/2] simplify --- .../2.guide/2.features/5.data-fetching.md | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/docs/content/2.guide/2.features/5.data-fetching.md b/docs/content/2.guide/2.features/5.data-fetching.md index a68da0f8a0c..6ac23fea678 100644 --- a/docs/content/2.guide/2.features/5.data-fetching.md +++ b/docs/content/2.guide/2.features/5.data-fetching.md @@ -212,24 +212,20 @@ 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, cookieName: string) => { - const response = await $fetch.raw(url) - if (process.server) { - const cookies = Object.fromEntries( - response.headers.get('set-cookie')?.split('; ').map((a) => a.split('=')) - ) - if (cookieName in cookies) { - useCookie(cookieName).value = cookies[cookieName] - } +export const fetchWithCookie = async (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) } - return response._data + return res._data } ``` ```vue ```