Requests suddenly having no headers

Hello, I'm working on some functions that read a specific header, and I'm suddenly realizing that no headers are getting to my code. This was working fine earlier, and it's just suddenly not and I'm not sure what has changed I should note I'm using SvelteKit, but afaik SvelteKit doesnt change the actual request object in any way Here is the code I have in my hooks.server.ts that is working fine in dev and was working fine on CF too until about 2 hours ago
const data = {
site: "[redacted]",
ua: event.request.headers.get("User-Agent"),
url: event.url,
id
};
if(!data.ua) {
console.log({headers: event.request.headers.keys()})
}
const data = {
site: "[redacted]",
ua: event.request.headers.get("User-Agent"),
url: event.url,
id
};
if(!data.ua) {
console.log({headers: event.request.headers.keys()})
}
Then this appears in the tail:
{
"headers": {}
}
{
"headers": {}
}
And, of course, data.ua is null. I find it very odd that this was all working fine, then suddenly it's not. Any ideas about what might be causing this?
5 Replies
Chaika
Chaika2y ago
Your log won't work, .keys() just returns an iterator, which isn't enuramble/usable by log/etc, use something like
console.log({headers: Object.fromEntries(event.request.headers)})
console.log({headers: Object.fromEntries(event.request.headers)})
https://developers.cloudflare.com/workers/examples/logging-headers/
Logging headers to console · Cloudflare Workers docs
Use a Map if you need to log a Headers object to the console:
ajgeiss0702
ajgeiss0702OP2y ago
ah, whoops. It's now showing some headers, but only 3. (if im not mistaken, there should at least be the cf-specific headers, right?)
{
"headers": {
"accept": "*/*",
"accept-language": "en-US,en;q=0.5",
"cookie": "id=xxxx"
}
}
{
"headers": {
"accept": "*/*",
"accept-language": "en-US,en;q=0.5",
"cookie": "id=xxxx"
}
}
Chaika
Chaika2y ago
There should be more headers yea, I haven't used Sveltekit hooks before myself so I'm not sure exactly how they work, only thing I can think is testing in non-local if you're not already, and looking into the event object. If the request object is untransformed it should have everything, perhaps there is event.headers or something else, not sure
ajgeiss0702
ajgeiss0702OP2y ago
the sveltekit docs claim that it is the "original" request object
ajgeiss0702
ajgeiss0702OP2y ago
there does not appear to be an event.headers https://kit.svelte.dev/docs/types#public-types-requestevent It works perfectly fine when running a sveltekit dev server. However, when deploying to pages it has the described behaviour. The weird thing is that is was working fine earlier today when deployed The issue ended up being due to (my mishandling of) sveltekit's handling of fetch inside server requests The initial request would have all the correct headers, but then all of the internal sub-requests would just have the 3 headers sent above. I ended up only trying to read the user-agent header if the host header is present (which it should be for all non-internal requests), then the issue is fixed!
Want results from more Discord servers?
Add your server