-
Notifications
You must be signed in to change notification settings - Fork 176
Fix cloudflare env #514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix cloudflare env #514
Changes from 5 commits
d97633d
08fa891
4a47d5a
69c71d3
7c2277a
88a8675
b8197ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -82,11 +82,6 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse { | |||||
private initialHeaders?: OutgoingHttpHeaders, | ||||||
) { | ||||||
super(); | ||||||
if (initialHeaders && initialHeaders[SET_COOKIE_HEADER]) { | ||||||
this._cookies = parseCookies( | ||||||
initialHeaders[SET_COOKIE_HEADER] as string | string[], | ||||||
) as string[]; | ||||||
} | ||||||
this.once("finish", () => { | ||||||
if (!this.headersSent) { | ||||||
this.flushHeaders(); | ||||||
|
@@ -174,18 +169,29 @@ export class OpenNextNodeResponse extends Transform implements ServerResponse { | |||||
...this.headers, | ||||||
...this.initialHeaders, | ||||||
}; | ||||||
const initialCookies = parseCookies( | ||||||
(this.initialHeaders[SET_COOKIE_HEADER] as string | string[]) ?? [], | ||||||
) as string[]; | ||||||
//Do we want to filter out the cookies that are already set? | ||||||
// At the moment cookies from the middlewware will override the ones set in the route | ||||||
|
// At the moment cookies from the middlewware will override the ones set in the route | |
// At the moment cookies from the middleware will override the ones set in the route |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question, we should ask the community? In terms of flow, middleware comes before the route so I'd assume the route should have the final say in the cookies. Just my pov, but you could argue the other way depending on your use case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah we could ask the community.
Right now for headers, middleware will override route, it makes sense that it's the same for cookies. The thing with the route having the final say is that in some case ( app router for example ) there is no way to modify the headers others than by having the middleware take precedence.
There is also another option here, we could use an env variable (or an option in the config) to decide which should take priority.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah we could ask the community. Right now for headers, middleware will override route, it makes sense that it's the same for cookies. The thing with the route having the final say is that in some case ( app router for example ) there is no way to modify the headers others than by having the middleware take precedence.
There is also another option here, we could use an env variable (or an option in the config) to decide which should take priority.
I like this idea, especially in the config. However, I think that the middleware should override both cookies and headers by default
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,8 +12,16 @@ const handler: WrapperHandler< | |
> = | ||
async (handler, converter) => | ||
async (event: Request, env: Record<string, string>): Promise<Response> => { | ||
//@ts-expect-error - process is not defined in cloudflare workers | ||
globalThis.process = { env }; | ||
globalThis.process = process; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where is the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added this as a js banner in the esbuild that i did for the cloudflare worker. I'll publish a sample repo later with SST v3 with different way to deploy OpenNext on different target. |
||
|
||
// Set the environment variables | ||
// Cloudlare suggests to not override the process.env object but instead apply the values to it | ||
for (const [key, value] of Object.entries(env)) { | ||
if (typeof value === "string") { | ||
process.env[key] = value; | ||
} | ||
} | ||
|
||
const internalEvent = await converter.convertFrom(event); | ||
|
||
const response = await handler(internalEvent); | ||
|
@@ -27,4 +35,5 @@ export default { | |
wrapper: handler, | ||
name: "cloudflare", | ||
supportStreaming: true, | ||
edgeRuntime: true, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,9 @@ const wrapper: WrapperHandler = async (handler, converter) => { | |
const internalEvent = await converter.convertFrom(req); | ||
const _res: StreamCreator = { | ||
writeHeaders: (prelude) => { | ||
res.setHeader("Set-Cookie", prelude.cookies); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually it does ( I thought the same thing as well at first ). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's really weird, according to the spec: https://httpwg.org/specs/rfc6265.html#overview it shouldn't. Since the comma character may also be part of the cookie value, eg I wonder what the Edit: I tried doing comma separated Set-Cookies in my server and it doesn't work. Only the first cookie is set. But doing multiple Set-Cookie for each cookie works as expected. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will work with an array: https://nodejs.org/api/http.html#http_response_setheader_name_value There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah ok, I missed that the node response setHeader is doing the heavy lifting. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually there was an issue here ( i forgot to remove the custom wrapper on my test 🙄 ). |
||
res.writeHead(prelude.statusCode, prelude.headers); | ||
res.flushHeaders(); | ||
res.uncork(); | ||
return res; | ||
}, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.