addCachePolicy method Null safety

void addCachePolicy(
  1. CachePolicy policy,
  2. bool shouldApplyToPath(
    1. String path
    )
)

Add a cache policy for file paths that return true for shouldApplyToPath.

When this instance serves a file, the headers determined by policy will be applied to files whose path returns true for shouldApplyToPath.

If a path would meet the criteria for multiple shouldApplyToPath functions added to this instance, the policy added earliest to this instance will be applied.

For example, the following adds a set of cache policies that will apply 'Cache-Control: no-cache, no-store' to '.widget' files, and 'Cache-Control: public' for any other files:

    fileController.addCachePolicy(const CachePolicy(preventCaching: true),
      (p) => p.endsWith(".widget"));
    fileController.addCachePolicy(const CachePolicy(),
      (p) => true);

Whereas the following incorrect example would apply 'Cache-Control: public' to '.widget' files because the first policy would always apply to it and the second policy would be ignored:

    fileController.addCachePolicy(const CachePolicy(),
      (p) => true);
    fileController.addCachePolicy(const CachePolicy(preventCaching: true),
      (p) => p.endsWith(".widget"));

Note that the 'Last-Modified' header is always applied to a response served from this instance.

Implementation

void addCachePolicy(
  CachePolicy policy,
  bool Function(String path) shouldApplyToPath,
) {
  _policyPairs.add(_PolicyPair(policy, shouldApplyToPath));
}