How can we pass a unique request ID (stored in cookies) to the backend on every API call — while still keeping the API fully cacheable and not breaking shared caching? #85898
-
SummaryI want to attach a unique request ID to every outbound API call made from the backend. The challenge: getting this ID from cookies requires calling So what would be the workaround — or is it even possible — to keep all APIs fully cacheable while still sending a unique ID with every request? For Example: import { cookies } from "next/headers";
const cookieStore = await cookies();
const deviceUniqueId = cookieStore?.get("uniqueId")?.value
await fetch(url, {
headers: {
'x-trace-id': deviceUniqueId
}
});Additional informationNo response ExampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
|
We'll soon have updated examples (literally resolving some internal comments on my updated document) In this case, you can refactor. import { cookies } from "next/headers";
async function CookieSection() {
const cookieStore = await cookies();
const deviceUniqueId = cookieStore?.get("uniqueId")?.value
const data = await fetchWithUniqueId(deviceUniqueId ?? 'no-unique-id-found');
return <>SomeJSX</>
}
export default function Page() {
return <Suspense fallback={<p>Some meaningful UI</p>}><CookieSection /></Suspense>
}And then do: async function fetchWithUniqueDeviceId(deviceId: string) {
'use cache'
const response = await fetch(url, {
headers: {
'x-trace-id': deviceUniqueId
}
});
return response // idk what you do with this await response.text() or .json()
}This way you can runtime cache the results of your function. |
Beta Was this translation helpful? Give feedback.
We'll soon have updated examples (literally resolving some internal comments on my updated document)
In this case, you can refactor.
And then do: