Paolo of Lost Pages
CDCloudflare Developers
•Created by Paolo of Lost Pages on 5/7/2024 in #pages-help
context item seems not to persist from middleware to middleware and from middleware to function
I found this behaviour trying to set up a session object and attach it to the context in the root middleware, expecting to see it bubble up the middleware chain and eventually into the "leaf" function. Tagging @HardlyWorkin' after asking consent.
In the root middleware I attach a function to context and add a field:
import { HCContextSetup } from "./libs/HorseCollar";
async function setupHorsecollarSession(context){
console.log("setting up HC in the root middleware");
context.hc = "saddled";
console.log("we are still in the root middleware: is the context saddled? [printing context.hc]", context.hc);
const res = await context.next();
return res;
}
// export const onRequest = [setupHorsecollarSession,errorHandling];
export const onRequest = [setupHorsecollarSession];
Then, in the /api/ middleware:
async function authentication(context) {
console.log("we are in the /api/ middleware: is the context saddled? [printing context.hc]", context.hc);
return context.next();
}
export const onRequest = [authentication];
Then in the /api/user/token function:
export async function onRequestGet(context) {
console.log("we are in the /api/user/token onRequestGet function: is the context saddled? [printing context.hc]", context.hc);
return new Response("foo ["+context.hc+"]", { status: 200 });
}
output in next message
44 replies
CDCloudflare Developers
•Created by Paolo of Lost Pages on 4/23/2024 in #pages-help
set cookie and redirect: Can't modify immutable headers.
I'm trying to set a session cookie and redirect with a 302 but appending the headers throws this exception:
Can't modify immutable headers.
TypeError: Can't modify immutable headers.
let res = Response.redirect(url.origin+"/my/",302);
res.headers.append("Set-Cookie", "sid=" + user.sid + "; Path=/; Secure; Max-Age=32000000; HttpOnly");
5 replies
CDCloudflare Developers
•Created by Paolo of Lost Pages on 2/9/2024 in #pages-help
jest and Response
I'm using Jest to write tests a Functions app I'm writing.
How do I get the Response text?
1 replies
CDCloudflare Developers
•Created by Paolo of Lost Pages on 1/18/2024 in #pages-help
change pages functions watch directories in wrangler
When running pages functions locally wrangler watches only the functions directories. Is there a way to make it watch other directories too? [posted by mistake in Workers originally]
5 replies
CDCloudflare Developers
•Created by Paolo of Lost Pages on 1/17/2024 in #workers-help
change pages functions watch directories in wrangler
When running pages functions locally wrangler watches only the functions directories. Is there a way to make it watch other directories too?
1 replies
CDCloudflare Developers
•Created by Paolo of Lost Pages on 1/17/2024 in #pages-help
Enriching EventContext
What is the best way to enrich EventContext?
For security reason we need to a lot of checks during the middleware, for example to check for CSRF, sessions/login, and so on.
It would be really helpful to be able to pass more data down the next function/middleware. This cannot be done directly with headers in a safe way. What's the best way to do so? what's a safe way to enrich the context?
Related: https://developers.cloudflare.com/pages/functions/api-reference/ lists some fields that are not described.
14 replies