Cache key for complex filtered /api/content queries exceeds KV 512-byte key limit (414)
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 76/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- typescript
- Domain
- backend-api-design, databases
Research direction
Start with packages/core/src/plugins/cache/services/cache.ts and trace how generateKey() builds KV keys; compare this with the filtered query key construction in packages/core/src/routes/api.ts. Reproduce with a multi-clause where filter and confirm the key stays within KV’s 512-byte limit while remaining stable for the same query.
Written by the indexing model from the issue text.
Description
Summary
Observed during an E2E run against test.sonicjs.com with wrangler tail attached:
(error) KV cache read error: Error: KV GET failed: 414 UTF-8 encoded length of 667 exceeds key length limit of 512.
(error) KV cache write error: Error: KV PUT failed: 414 UTF-8 encoded length of 667 exceeds key length limit of 512.
Triggered by requests like:
GET /api/content?collection=blog_post&where={"or":[{"field":"status","operator":"not_equals","value":"published"},{"field":"slug","operator":"equals","value":"..."},{"field":"slug","operator":"equals","value":"..."}]}&limit=100
Root cause
packages/core/src/routes/api.ts builds cache keys by JSON-stringifying the parsed filter object together with the compiled SQL, with no length cap or hashing:
const cacheKey = cache.generateKey('content-filtered', JSON.stringify({ filter: normalizedFilter, query: queryResult.sql }))
CacheService.generateKey() (packages/core/src/plugins/cache/services/cache.ts:427) just concatenates namespace + type + identifier as-is — no truncation or hashing:
generateKey(type: string, identifier: string): string {
return generateCacheKey(this.config.namespace, type, identifier)
}
For a where filter with multiple OR clauses (common in E2E assertions, and plausible in real admin/public API usage with complex filters), the resulting key easily exceeds Cloudflare KV's 512-byte key limit, producing a 414 from the KV API on both read and write.
Impact
- Silent cache-miss behavior for any sufficiently complex filtered content query — not a crash (caught and logged), but defeats caching entirely for those queries, and spams error-level logs on every request matching the pattern.
- Likely affects real traffic with complex filters/searches, not just E2E — anyone with a multi-clause OR filter on
/api/content(or the collection-scoped equivalent) hits this.
Suggested fix
Hash long identifiers (e.g. SHA-256 truncated, or similar) before building the KV key when the combined key would exceed KV's limit, in generateCacheKey/CacheService.generateKey, rather than relying on the raw JSON-stringified filter+SQL.
Repro
GET /api/content?collection=<any>&where=<a filter with 2+ OR/AND clauses so the stringified filter+SQL exceeds ~480 bytes>- Check
wrangler tailforKV cache read/write error: ... 414 ... exceeds key length limit of 512.
Found incidentally while investigating #1194 (plugin wiring cross-request promise drop) — unrelated root cause, filing separately.
- Dominant language
- TypeScript
- Stars
- 1.8k
- Forks
- 219
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 67
Getting set up
- Ships a Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from SonicJs-Org/sonicjs
-
bug(admin/users): server error returns bare alert HTML causing black-shape visual artifactPossibly taken @lane711 claimed this today. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
SonicJs-Org/sonicjs#1211 ·
Maintainers usually reply within 1 day
-
ResendProvider cannot send on Workers: unbound `fetch` stored as an instance property throws "Illegal invocation"Possibly taken @lane711 claimed this 23 days ago. Openarea: email bug priority: P1
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SonicJs-Org/sonicjs#1099 ·
Maintainers usually reply within 1 day
-
Email plugin settings are never read: loadDbEmailSettings queries a v2 `plugins` table that does not exist in v3Possibly taken @lane711 claimed this 23 days ago. Openarea: email bug priority: P1
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
SonicJs-Org/sonicjs#1098 ·
Maintainers usually reply within 1 day
-
area: documentation bug documentation priority: P2
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SonicJs-Org/sonicjs#1012 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SonicJs-Org/sonicjs#471 ·
Maintainers usually reply within 1 day
All issues in SonicJs-Org/sonicjs
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
AloisSeckar/elrh-cosca#36 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 73/100
cursor/community-plugins#572 ·
Maintainers usually reply within 1 day
-
[Plugin] calendarOpen
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
MUKE-coder/grit#228 ·
-
bug status:needs-triage
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
dongdongbh/Mindwtr#1587 ·
Maintainers usually reply within 1 day