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
richburdonOP8mo 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
richburdonOP8mo 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/@[email protected]/node_modules/@cloudflare/workers-types/index.ts" for import "Response"
✘ [ERROR] No matching export in "../../node_modules/.pnpm/@[email protected]/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/[email protected]/index.ts
richburdon
richburdonOP8mo 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
richburdonOP8mo 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
richburdonOP8mo 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
richburdonOP8mo 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
richburdonOP8mo 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.
Hello, I’m Allie!
Just as a sanity check, if you start a new project with the following tsconfig.json:
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"lib": ["esnext"],
"types": ["@cloudflare/workers-types/experimental"],
"isolatedModules": true,
"noEmit": true,
"strict": true,
"esModuleInterop": true,
},
"include": [
"src",
"types"
]
}
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"lib": ["esnext"],
"types": ["@cloudflare/workers-types/experimental"],
"isolatedModules": true,
"noEmit": true,
"strict": true,
"esModuleInterop": true,
},
"include": [
"src",
"types"
]
}
, and then a src/index.ts with the following, do you see any type errors?
export default {
async fetch(req: Request): Promise<Response> {
console.lof(req.cf);
return new Response("Hello from TS Worker!");
}
}
export default {
async fetch(req: Request): Promise<Response> {
console.lof(req.cf);
return new Response("Hello from TS Worker!");
}
}
richburdon
richburdonOP8mo ago
it'll take me a moment to set that up but i'll try that out -- do you mind if i ping you back on this.
Hello, I’m Allie!
Sounds good
richburdon
richburdonOP8mo ago
hi @HardlyWorkin' i figured out the issue was that I had"lib": ["ESNext", "DOM"] in my tsconfig and the "DOM" lib overrides the CF types (for DOM Request/Response). if i now remove that then fetch is undefined so i'm still tracking that down.
Hello, I’m Allie!
Oh yeah, that’ll do it
richburdon
richburdonOP8mo ago
the way around is to have a separate tsconfi.spec.json for the tests (e.g., for clients which require fetch). thanks again for the help.
Want results from more Discord servers?
Add your server