Framework7 Ajax request caching ignoring query‑string variations
0 reputation · 21 Jul 2022, 10:52 UTC
0 reputation · 21 Jul 2022, 10:52 UTC
Goal: ensure that cached Ajax responses differ when request URL includes different query strings or when relevant HTTP headers change, so that UI reflects up-to-date data.
Uncertainty: default cache key uses only the base URL string, ignoring query string and headers; developers can supply a custom cacheKey function, but it is unclear whether this can be applied globally for all app.request.get calls or must be passed per request, and whether Framework7 provides a built‑in option to automatically incorporate query string or header values into the cache key.
Can a custom cacheKey function be set globally for all app.request.get calls, or must it be supplied individually per request?
Does Framework7 offer a configuration option to include query‑string parameters or specific response headers in the default cache key composition?
34275 reputation · 21 Jul 2022, 15:57 UTC
No global cacheKey configuration exists. You must supply a cacheKey function in the options object of each app.request.get (or app.request.post, etc.) call. There is no app.request.cacheKey or similar app-level setting.
Query strings are already part of the default cache key. The built-in key is the full request URL including the query string, so /api/users?page=1 and /api/users?page=2 are cached separately without any extra code. There is no built-in option to fold specific request or response headers into the key; header-aware caching requires a per-request cacheKey function.
Framework7’s request cache is an in-memory Map keyed by the resolved cacheKey string. The default resolver simply uses the final URL (base + query string). Because the key is computed before the request fires, a custom cacheKey function receives only the request options—it cannot read response headers such as ETag or Last-Modified.
To avoid repeating a custom key function across the codebase, create a helper that returns a pre-configured options object:
function cachedRequestOptions(overrides = {}) {
return {
cache: true, // or a TTL in ms, e.g. 5 * 60 * 1000
cacheKey: (opts) => {
// Example: include a custom header value if present
const headerPart = opts.headers?.['X-Cache-Version'] ?? '';
return `${opts.url}::${headerPart}`;
},
...overrides
};
}
// Usage
app.request.get('/api/items', cachedRequestOptions({ headers: { 'X-Cache-Version': 'v2' } }));
app.request.get('/api/items?page=2', cachedRequestOptions()); // different URL → different key automaticallySpread the helper’s return value into each call. This keeps the key logic centralized while respecting Framework7’s per-request design.
cacheKey parameter signature.cacheKey function to verify it is invoked per request and receives the expected options object.app.request.cache (a Map) in the console to see the actual keys stored./api/test?id=1 and /api/test?id=2) without a custom cacheKey; confirm they create two distinct cache entries.Are you targeting Framework 7 or Framework 8? The caching API has not changed between these major versions, but confirming the exact version lets me point you to the correct documentation section if you need to verify the cacheKey signature yourself.
Use comments to ask for clarification. Post a solution as an answer.
34,275 reputation · 21 Jul 2022, 12:18 UTC
Framework7’s app.request.setup lets you define default options for all subsequent app.request calls. If you pass a cacheKey function in setup, it will be used for every request that has caching enabled, eliminating the need to repeat the function on each app.request.get call.
Because the default is applied globally, any request that turns on cache: true will inherit the custom key. If you need caching only for specific endpoints, either disable the global cacheKey or add a cacheKey: null override on those calls.
The built‑in key is the final URL—including query parameters—so /api/items?page=1 and /api/items?page=2 are cached separately without extra code.