-
Notifications
You must be signed in to change notification settings - Fork 9
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
preflight - cache GET
and OPTIONS
requests separately
#136
Comments
Or.. maybe it'd be better to let the user create a suffix for the cache key by exposing the CurrentAdditions
Futureexport const cacher = (cache: ResponseCache, id: (ctx: RequestContext) => string): Middleware => async (ctx, next) => {
const reqURL = new URL(ctx.request.url)
const key = `${reqURL.pathname}${reqURL.search}-${JSON.stringify(ctx.state)}-${id(ctx)}` |
Excellent catch and thanks for the detailed issue. The request method should definitely be in the I also like the idea of the optional key generator argument. Suggested fix:export const cacher = (cache: ResponseCache, keyGen?: (ctx: RequestContext) => string): Middleware => async (ctx, next) => {
const key = keyGen
? keyGen(ctx)
: `${ctx.request.method}-${ctx.request.url.pathname}${ctx.request.url.search}-${JSON.stringify(ctx.state)}` Would you be interested in submitting a PR for this? Would be great to have you as a contributer :) |
Yea, sure. @sebringrose Are there any specific formatting guidelines for this repo? |
Basic formatting guidelines: (not enforced by linter, just habits I picked up from Deno docs)
You'll see requested changes on the PR. I realised we can define the default keygen function outside of the cacher for a minor performance boost. |
The
cacher()
middleware uses the same headers for both the preflight (OPTIONS
) and main (GET
) requests when theroute
is the same.I think adding
ctx.request.method
to the key would fix the issue.https://github.com/sebringrose/peko/blob/836e830e1c2daa738b8d71a12bec999bd4c5ea6b/middleware/cacher.ts#L11
The text was updated successfully, but these errors were encountered: