Skip to content

Documentation / @ember-data/request-utils / index / PolicyConfig

ts
type PolicyConfig = object;

Defined in: index.ts:803

The configuration options for the CachePolicy provided by @ember-data/request-utils.

ts
import { CachePolicy } from '@ember-data/request-utils';

new CachePolicy({
  // ... PolicyConfig Settings ... //
});

Properties

apiCacheHardExpires

ts
apiCacheHardExpires: number;

Defined in: index.ts:833

the number of milliseconds after which a request is considered expired and should be re-fetched. If a request is issued again after this time, the request will disregard the cache and wait for a fresh response from the API.

This is calculated against the date header of the response.

If your API does not provide a date header, the Fetch handler provided by @ember-data/request/fetch will automatically add it to responses if it is not present. Responses without a date header will be considered hard expired immediately.


apiCacheSoftExpires

ts
apiCacheSoftExpires: number;

Defined in: index.ts:818

the number of milliseconds after which a request is considered stale. If a request is issued again after this time, the request will respond from cache immediately while a background request is made to update the cache.

This is calculated against the date header of the response.

If your API does not provide a date header, the Fetch handler provided by @ember-data/request/fetch will automatically add it to responses if it is not present. Responses without a date header will be considered stale immediately.


constraints?

ts
optional constraints: object;

Defined in: index.ts:879

In addition to the simple time-based expiration strategy, CachePolicy supports various common server-supplied expiration strategies via headers, as well as custom expiration strategies via the isExpired function.

Requests will be validated for expiration against these constraints. If any of these constraints are not met, the request will be considered expired. If all constraints are met, the request will be considered valid and the time based expiration strategy will NOT be used.

Meeting a constraint means BOTH that the properties the constraint requires are present AND that the expiration time indicated by those properties has not been exceeded.

In other words, if the properties for a constraint are not present, this does not count either as meeting or as not meeting the constraint, the constraint simply does not apply.

The isExpired function is called with the request and should return true if the request is expired, false if it is not expired, and null if the expiration status is unknown.

In order constraints are checked:

  • isExpired function
  • ↳ (if null) X-WarpDrive-Expires header
  • ↳ (if null) Cache-Control header
  • ↳ (if null) Expires header

headers?

ts
optional headers: object;

Headers that should be checked for expiration.

headers.Cache-Control?
ts
optional headers.Cache-Control: boolean;

Whether the Cache-Control header should be checked for expiration. If true, then the max-age and s-maxage directives are used alongside the Age and Date headers to determine if the expiration time has passed.

Other directives are ignored.

'Cache-Control' will take precedence over 'Expires' if both are present and both configured to be checked.

headers.Expires?
ts
optional headers.Expires: boolean;

Whether the Expires header should be checked for expiration.

If true, then the Expires header is used to caclulate the expiration time and determine if the expiration time has passed.

'Cache-Control' will take precedence over 'Expires' if both are present.

headers.X-WarpDrive-Expires?
ts
optional headers.X-WarpDrive-Expires: boolean;

Whether the X-WarpDrive-Expires header should be checked for expiration.

If true, then the X-WarpDrive-Expires header is used to caclulate the expiration time and determine if the expiration time has passed.

This header will take precedence over 'Cache-Control' and 'Expires' if all three are present.

The header's value should be a UTC date string.

isExpired()?

ts
optional isExpired: (request) => boolean | null;

A function that should be called to determine if the request is expired.

If present, this function will be called with the request and should return true if the request is expired, false if it is not expired, and null if the expiration status is unknown.

If the function does not return null,

Parameters
request

StructuredDocument<ResourceDocument>

Returns

boolean | null


disableTestOptimization?

ts
optional disableTestOptimization: boolean;

Defined in: index.ts:846

In Testing environments, the apiCacheSoftExpires will always be false and apiCacheHardExpires will use the apiCacheSoftExpires value.

This helps reduce flakiness and produce predictably rendered results in test suites.

Requests that specifically set cacheOptions.backgroundReload = true will still be background reloaded in tests.

This behavior can be opted out of by setting this value to true.

Released under the MIT License.