-
-
Notifications
You must be signed in to change notification settings - Fork 814
feat(cache): add allowQuery option to filter query params in cache key
#4078
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
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 |
|---|---|---|
|
|
@@ -38,4 +38,11 @@ export interface CachedEventHandlerOptions extends Omit< | |
| > { | ||
| headersOnly?: boolean; | ||
| varies?: string[] | readonly string[]; | ||
| /** | ||
| * List of query string parameter names that will be considered for caching. | ||
| * - If undefined, all query parameters are included in the cache key. | ||
| * - If an empty array `[]`, all query parameters are ignored (only pathname is used for caching). | ||
| * - If a list of parameter names, only those parameters are included in the cache key. | ||
| */ | ||
| allowQuery?: string[] | readonly string[]; | ||
|
Comment on lines
+41
to
+47
Contributor
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. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Verify how `allowQuery` values are incorporated into the cache key.
# Expected result: the implementation uses `searchParams.get(key)`,
# which confirms only one value per query name is considered.
sed -n '201,216p' src/runtime/internal/cache.tsRepository: nitrojs/nitro Length of output: 607 JSDoc must clarify that only the first value per query parameter is used in the cache key. The implementation uses 🤖 Prompt for AI Agents |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { defineCachedHandler } from "nitro/cache"; | ||
|
|
||
| export default defineCachedHandler( | ||
| (event) => { | ||
| return { | ||
| timestamp: Date.now(), | ||
| }; | ||
| }, | ||
| { swr: true, maxAge: 60, allowQuery: ["q"] } | ||
| ); |
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.
Preserve repeated allowed query values in the cache key.
Line 208 only reads the first value with
searchParams.get(), so?tag=a&tag=band?tag=acollapse to the same cache entry whentagis allowed. That can serve the wrong cached response for multi-value filters.🐛 Proposed fix
const params = new URLSearchParams(); for (const key of opts.allowQuery) { - const value = event.url.searchParams.get(key); - if (value !== null) { - params.set(key, value); + for (const value of event.url.searchParams.getAll(key)) { + params.append(key, value); } }📝 Committable suggestion
🤖 Prompt for AI Agents