@mmstack/resource 20.0.3 → 20.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -244,7 +244,17 @@ function setCacheContext(ctx = new HttpContext(), opt) {
244
244
  function getCacheContext(ctx) {
245
245
  return ctx.get(CACHE_CONTEXT);
246
246
  }
247
- function parseCacheControlHeader(req) {
247
+ function parseCacheControlHeader(req, ignoreCacheControl = false) {
248
+ if (ignoreCacheControl) {
249
+ return {
250
+ noStore: false,
251
+ noCache: false,
252
+ mustRevalidate: false,
253
+ immutable: false,
254
+ maxAge: null,
255
+ staleWhileRevalidate: null,
256
+ };
257
+ }
248
258
  const header = req.headers.get('Cache-Control');
249
259
  let sMaxAge = null;
250
260
  const directives = {
@@ -322,30 +332,25 @@ function parseCacheControlHeader(req) {
322
332
  };
323
333
  return directives;
324
334
  }
325
- function resolveTimings(cacheControl, staleTime, ttl) {
326
- const timings = {
327
- staleTime,
328
- ttl,
329
- };
335
+ function resolveTimings(cacheControl, optStaleTime, optTTL) {
336
+ let staleTime = optStaleTime;
337
+ let ttl = optTTL;
330
338
  if (cacheControl.immutable)
331
339
  return {
332
340
  staleTime: Infinity,
333
341
  ttl: Infinity,
334
342
  };
343
+ if (cacheControl.maxAge !== null)
344
+ ttl = cacheControl.maxAge * 1000;
345
+ if (cacheControl.staleWhileRevalidate !== null)
346
+ staleTime = cacheControl.staleWhileRevalidate * 1000;
335
347
  // if no-cache is set, we must always revalidate
336
348
  if (cacheControl.noCache || cacheControl.mustRevalidate)
337
- timings.staleTime = 0;
338
- if (cacheControl.staleWhileRevalidate !== null)
339
- timings.staleTime = cacheControl.staleWhileRevalidate;
340
- if (cacheControl.maxAge !== null)
341
- timings.ttl = cacheControl.maxAge * 1000;
342
- // if stale-while-revalidate is set, we must revalidate after that time at the latest, but we can still serve the stale data
343
- if (cacheControl.staleWhileRevalidate !== null) {
344
- const ms = cacheControl.staleWhileRevalidate * 1000;
345
- if (timings.staleTime === undefined || timings.staleTime > ms)
346
- timings.staleTime = ms;
349
+ staleTime = 0;
350
+ if (ttl !== undefined && staleTime !== undefined && ttl < staleTime) {
351
+ staleTime = ttl;
347
352
  }
348
- return timings;
353
+ return { staleTime, ttl };
349
354
  }
350
355
  /**
351
356
  * Creates an `HttpInterceptorFn` that implements caching for HTTP requests. This interceptor
@@ -397,12 +402,19 @@ function createCacheInterceptor(allowedMethods = ['GET', 'HEAD', 'OPTIONS']) {
397
402
  if (lastModified) {
398
403
  req = req.clone({ setHeaders: { 'If-Modified-Since': lastModified } });
399
404
  }
405
+ if (opt.bustBrowserCache) {
406
+ req = req.clone({
407
+ setParams: { _cb: Date.now().toString() },
408
+ });
409
+ }
400
410
  return next(req).pipe(tap((event) => {
401
411
  if (event instanceof HttpResponse && event.ok) {
402
- const cacheControl = parseCacheControlHeader(event);
412
+ const cacheControl = parseCacheControlHeader(event, opt.ignoreCacheControl);
403
413
  if (cacheControl.noStore)
404
414
  return;
405
415
  const { staleTime, ttl } = resolveTimings(cacheControl, opt.staleTime, opt.ttl);
416
+ if (opt.ttl === 0)
417
+ return; // no point
406
418
  cache.store(key, event, staleTime, ttl);
407
419
  }
408
420
  }), map((event) => {
@@ -852,6 +864,10 @@ function queryResource(request, options) {
852
864
  return null;
853
865
  return hashFn(r);
854
866
  });
867
+ const bustBrowserCache = typeof options?.cache === 'object' &&
868
+ options.cache.bustBrowserCache === true;
869
+ const ignoreCacheControl = typeof options?.cache === 'object' &&
870
+ options.cache.ignoreCacheControl === true;
855
871
  const cachedRequest = options?.cache
856
872
  ? computed(() => {
857
873
  const r = stableRequest();
@@ -863,6 +879,8 @@ function queryResource(request, options) {
863
879
  staleTime,
864
880
  ttl,
865
881
  key: cacheKey() ?? hashFn(r),
882
+ bustBrowserCache,
883
+ ignoreCacheControl,
866
884
  }),
867
885
  };
868
886
  })