Skip to content

[fix] - Cache keys implementation #8

Open
GabrielHorbach wants to merge 1 commit intoHarperFast:mainfrom
GabrielHorbach:fix/cache-key-functionality
Open

[fix] - Cache keys implementation #8
GabrielHorbach wants to merge 1 commit intoHarperFast:mainfrom
GabrielHorbach:fix/cache-key-functionality

Conversation

@GabrielHorbach
Copy link
Copy Markdown

Cache Key Implementation Fix

Problem

After migrating from @layer0/core to @harperdb/http-router, the application experienced cache-related issues when using custom cache keys:

  • Missing HTML content (banners, breadcrumbs) after navigation
  • React hydration errors (#310)
  • Incomplete API responses causing .replace() failures
  • Cascading failures affecting all pages after one page broke

Root Cause: The cache key generation in @harperdb/http-router was fundamentally different from @layer0/core's proven implementation, causing cache key collisions where different requests generated the same cache key (serving wrong content) or same requests generated different cache keys (cache misses, serving stale content).

Solution

Updated the cache key generation in @harperdb/http-router/index.js to match @layer0/core's approach:

  1. Query Parameter Processing: Changed from only adding whitelisted params to starting with ALL params and deleting non-whitelisted ones (matching excludeAllQueryParametersExcept behavior)

  2. Default Headers: Always include host and accept-encoding headers in cache key (matching getDefaultHeaders)

  3. Deterministic Format: Switched from string concatenation to JSON.stringify with sorted arrays of [key, value] pairs, ensuring consistent ordering regardless of parameter order

  4. Cookie Parsing: Improved parsing to handle values containing = characters

  5. Pathname Validation: Added validation to ensure pathname is always present and valid

@GabrielHorbach GabrielHorbach changed the title FIX: Cache keys implementation [fix] - Cache keys implementation Dec 30, 2025
Copy link
Copy Markdown
Contributor

@jjohnson-hdb jjohnson-hdb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use of native harper logger is preferred but looks good to me otherwise

Comment thread index.js
if (!pathname || pathname === '') {
// Fallback: use full URL path as last resort
const urlPath = new URL(request.url, `http://${request.headers.get('host') || 'localhost'}`).pathname;
console.warn('[CACHE KEY WARNING] Empty pathname, using URL path:', urlPath);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.warn('[CACHE KEY WARNING] Empty pathname, using URL path:', urlPath);
logger.warn('[CACHE KEY WARNING] Empty pathname, using URL path:', urlPath);

Comment thread index.js

// Validate cache key is not empty - critical safety check
if (!cacheKeyString || cacheKeyString === '{}' || cacheKeyString === 'null') {
console.error('[CACHE KEY ERROR] Invalid cache key generated:', {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.error('[CACHE KEY ERROR] Invalid cache key generated:', {
logger.error('[CACHE KEY ERROR] Invalid cache key generated:', {

Comment thread index.js

// Debug logging
if (process.env.DEBUG_CACHE_KEYS === 'true') {
console.log('[CACHE KEY]', {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log('[CACHE KEY]', {
logger.info('[CACHE KEY]', {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants