env.ASSETS.fetch doesn't return additional headers

I have a Pages project with a worker (advanced config) that attempts to add a cookie via adding headers to the env.ASSETS.fetch call
return await env.ASSETS.fetch(request, {
headers: {
'Set-Cookie': `${cookie}=${jwt}; Secure`
}
});
return await env.ASSETS.fetch(request, {
headers: {
'Set-Cookie': `${cookie}=${jwt}; Secure`
}
});
the headers are not returned. to sanity check if i return the following ther are:
return new Response(null, {
headers: {
'Set-Cookie': `${cookie}=${jwt}; Secure`
}
});
return new Response(null, {
headers: {
'Set-Cookie': `${cookie}=${jwt}; Secure`
}
});
to workaround i could either redirect (with the cookies), but what i'm attempting above seems reasonable so i wonder if there's an API bug?
21 Replies
Hello, I’m Allie!
Not quite. Instead of returning a Response with a set-cookie header, your code is sending the request to the ASSETS binding with a set-cookie header. You want
let res = await env.ASSETS.fetch(request);
res = new Response(res.body, res);
res.headers.set("set-cookie", `${cookie}=${jwt}; Secure`);
return res;
let res = await env.ASSETS.fetch(request);
res = new Response(res.body, res);
res.headers.set("set-cookie", `${cookie}=${jwt}; Secure`);
return res;
richburdon
richburdon5mo ago
hi thanks for the quick response. for that i got:
TS2345: Argument of type ReadableStream<any> is not assignable to parameter of type BodyInit
TS2345: Argument of type ReadableStream<any> is not assignable to parameter of type BodyInit
i shouldn't need to await the body? related (TS issue) @cloudflare/workers-types doesn't export a Response type (it does for Request, etc.) so the native Node Response seems differnet from the one that the worker implements/provides?
richburdon
richburdon5mo ago
oh. could it be that that's been updated recently? the code isn't open source right? i'm pretty user the export was missing a couple of days ago. (but you're right it's there now). well, it still doesn't compile
✘ [ERROR] No matching export in "../../node_modules/.pnpm/@cloudflare+workers-types@4.20240502.0/node_modules/@cloudflare/workers-types/index.ts" for import "Response"
✘ [ERROR] No matching export in "../../node_modules/.pnpm/@cloudflare+workers-types@4.20240502.0/node_modules/@cloudflare/workers-types/index.ts" for import "Response"
Hello, I’m Allie!
It is. I don't remember where the generator code lives, but the package is here: https://github.com/cloudflare/workerd/tree/main/npm/workers-types
GitHub
workerd/npm/workers-types at main · cloudflare/workerd
The JavaScript / Wasm runtime that powers Cloudflare Workers - cloudflare/workerd
Hello, I’m Allie!
Not sure why it is throwing that. Here's that exact file, and I do see export declare class Response extends Body {... https://cdn.jsdelivr.net/npm/@cloudflare/workers-types@4.20240502.0/index.ts
richburdon
richburdon5mo ago
right. i see that. ⛅️ wrangler 3.53.0 (update available 3.53.1) does my tsconfig look okay?
"compilerOptions": {
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"rootDir": ".",
"outDir": "../../dist",
"types": [
"@cloudflare/workers-types",
]
}
"compilerOptions": {
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"rootDir": ".",
"outDir": "../../dist",
"types": [
"@cloudflare/workers-types",
]
}
it's strange -- my IDE was reporting Response as missing last week, so i commented out the import and used casts.
import {
type ExportedHandlerFetchHandler,
type Fetcher,
type Request as WorkerRequest,
// Response
} from '@cloudflare/workers-types';
import {
type ExportedHandlerFetchHandler,
type Fetcher,
type Request as WorkerRequest,
// Response
} from '@cloudflare/workers-types';
now the IDE isn't complaing but the build fails, so it feels like something has updated recentely and is out of sync
Hello, I’m Allie!
If you have the types in your tsconfig.json, then you shouldn't need to import any types at all
richburdon
richburdon5mo ago
I've imported the CF Request since i wanted to access the typed cf property
Hello, I’m Allie!
request.cf? That should be typed automatically too
richburdon
richburdon5mo ago
src/auth.ts:75:31 - error TS2339: Property 'cf' does not exist on type 'Request'.
Hello, I’m Allie!
Is that a Node/DOM Request too?
richburdon
richburdon5mo ago
that's the main request handler. (request: Request, env: Env, c)
Hello, I’m Allie!
Yeah, I think it is using a DOM/Node Request, rather than the CF Request Per your tsconfig it should use the CF request, but iirc some depencies can override your config(for some dumb reason)
richburdon
richburdon5mo ago
ok well my workaround is (request as any).cf; it's a pity since the CF TS system is really nice. i'll investigate what might be happening with our tsconfig that might be interfering with this. thanks very much for your help.
Want results from more Discord servers?
Add your server