-
Notifications
You must be signed in to change notification settings - Fork 37
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
Explicitly disallow fetch() caching #102
Conversation
4cb7502
to
d81e465
Compare
For the sake of our customers, I don't feel it's worth us fighting that "this implementation is wrong" with our customers in the middle. If this hack were specific to an implementation of fetch, I'd feel differently, but given this is in the spec, and we're just being overly explicit, rather than relying on a "default" behavior, I'm more ok with it. I still firmly disagree with implementations that are choosing to cache POST, _especially_ also with an Authorization header, but there's only so much I/we can control. See context: vercel/next.js#46856 Fixes: #101 #99
d81e465
to
da8f41d
Compare
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.
Thanks!
FYI, this breaks cloudflare workers. Any thoughts on how to fix this?
|
sigh, we can just revert it. I'm not sure how we can make everything happy, and this seemed like a backwards compatible change since this field is in the fetch spec. I'm guessing the extra field can't just be ignored because of TypeScript being too strict? As a TypeScript noob, can we just have an arbitrary dict here? Since in most things, this would be ignored as extraneous. |
I don't think there's much we can do other than maybe open an issue over here? https://github.com/cloudflare/workers-sdk Beyond us reverting. |
@ryeshrimp Yikes, sorry for breaking your application. To move forward, you have two options.
import { connect } from '@planetscale/database'
import { fetch } from 'undici'
const config = {
fetch,
host: '<host>',
username: '<user>',
password: '<password>'
}
const conn = connect(config)
const results = await conn.execute('select 1 from dual')
console.log(results) |
+1 on noticing the breaking, and +1 on pinning to 1.6.0 fixing it. Glad to see I'm not alone. |
If you want to fix cloudflare workers + 1.7.0, override the fetch: (url: string, init: RequestInit<RequestInitCfProperties>) => {
delete (init as any)["cache"]; // Remove cache header
return fetch(url, init);
} |
@chocolatkey Pretty new to CF Workers, so not sure how to use the above solution. The following is code is init'ing a new CF Worker project which uses fetch.
How can I override the fetch in this scenario? Any help please? |
@rjvim I guess I wasn't specific enough in my example, the import { connect } from '@planetscale/database'
const config = {
host: 'xxx',
username: 'xxx',
password: 'xxx',
fetch: (url: string, init: RequestInit<RequestInitCfProperties>) => {
delete (init as any)["cache"]; // Remove cache header
return fetch(url, init);
}
}
const conn = connect(config) P.S. do this outside the request handling function, it will get reused between requests to the worker |
@@ -265,7 +266,8 @@ async function postJSON<T>(config: Config, url: string | URL, body = {}): Promis | |||
'Content-Type': 'application/json', | |||
'User-Agent': `database-js/${Version}`, | |||
Authorization: `Basic ${auth}` | |||
} | |||
}, | |||
cache: 'no-store' |
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.
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.
@iheanyi undici does not seem to work with cloudflare workers, reverting to 1.6.0 also produces the same error. |
Thanks @chocolatkey . For those looking for a solution, I can confirm this works. Also, as its just a couple of lines of change, reverting this - once workers fix their bugs, will be easy. |
I confirm that the issue persists and that the solution works. "P.S. do this outside the request handling function, it will get reused between requests to the worker" @rjvim Is that something we should be doing, though? Also, Planetscale said that their serverless driver uses a connection factory that provides fresh connections (I'm using that with your solution): https://github.com/planetscale/database-js#connection-factory |
Just like chocolatkey mentioned above, this works perfectly.
|
For the sake of our customers, I don't feel it's worth us fighting that "this implementation is wrong" with our customers in the middle.
If this hack were specific to an implementation of fetch, I'd feel differently, but given this is in the spec, and we're just being overly explicit, rather than relying on a "default" behavior, I'm more ok with it.
I still firmly disagree with implementations that are choosing to cache POST, especially also with an Authorization header, but there's only so much I/we can control.
See context: vercel/next.js#46856
Fixes: cloudflare/workers-sdk#101 cloudflare/workers-sdk#99