Skip to content

Commit

Permalink
Support stale-while-revalidate and stale-if-error
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinHoutevelts committed Jul 19, 2023
1 parent e34cff5 commit d62271c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## [1.1.1] - 2023-07-19
### Added
- Support `stale-while-revalidate` and `stale-if-error` cache control header
- [https://developer.fastly.com/learning/concepts/stale](http://web.archive.org/web/20230719193134/https://developer.fastly.com/learning/concepts/stale/)

## [1.1.0] - 2022-06-28
### Added
- Add v2 upgrade guide & script
Expand Down
18 changes: 13 additions & 5 deletions src/EventSubscriber/CacheSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,19 @@ protected function setMaxAge(Response $response, array $definition)
$response->setSharedMaxAge($definition['s-maxage']);
}

if (isset($definition['wm-s-maxage'])) {
$response->headers->addCacheControlDirective(
'wm-s-maxage',
$definition['wm-s-maxage']
);
$directives = [
'wm-s-maxage',
'stale-while-revalidate',
'stale-if-error',
];

foreach ($directives as $directive) {
if (isset($definition[$directive])) {
$response->headers->addCacheControlDirective(
$directive,
$definition[$directive]
);
}
}
}
}
12 changes: 11 additions & 1 deletion src/Service/Cache/MaxAgeDecider.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public function onResponseEarly(FilterResponseEvent $event)
'maxage' => $headers->getCacheControlDirective('max-age'),
's-maxage' => $headers->getCacheControlDirective('s-maxage'),
'wm-s-maxage' => $headers->getCacheControlDirective('wm-s-maxage'),
'stale-while-revalidate' => $headers->getCacheControlDirective('stale-while-revalidate'),
'stale-if-error' => $headers->getCacheControlDirective('stale-if-error'),
],
static fn ($value) => $value !== null
);
Expand All @@ -75,6 +77,8 @@ public function getMaxage(Request $request, Response $response)
's-maxage' => $request->attributes->get('_smaxage', 0),
'maxage' => $request->attributes->get('_maxage', 0),
'wm-s-maxage' => $request->attributes->get('_wmsmaxage', null),
'stale-while-revalidate' => $request->attributes->get('_stale-while-revalidate', null),
'stale-if-error' => $request->attributes->get('_stale-if-error', null),
];
}

Expand All @@ -92,7 +96,13 @@ public function getMaxage(Request $request, Response $response)
return $explicit + $definition;
}

return $explicit + ['s-maxage' => 0, 'maxage' => 0, 'wm-s-maxage' => null];
return $explicit + [
's-maxage' => 0,
'maxage' => 0,
'wm-s-maxage' => null,
'stale-while-revalidate' => null,
'stale-if-error' => null,
];
}

protected function getMaxAgesForMainEntity()
Expand Down
5 changes: 4 additions & 1 deletion wmcontroller.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ parameters:
# maxage = client side caching duration
# s-maxage = server side caching duration (this can be drupal db or a cdn)
# wm-s-maxage = custom cache-control directive for different local cache ttl
# stale-while-revalidate indicates that caches MAY serve the response after it becomes stale, up to the indicated number of seconds.
# stale-if-error indicates that when an error is encountered, a cached stale response MAY be used to satisfy the request, up to the indicated number of seconds.
wmcontroller.cache.expiry:
# Determine max and s-max based on content-type and/or bundle.
# _default is used when no definition is available for any given bundle.
Expand All @@ -40,8 +42,9 @@ parameters:
# Client side caching for 2 minutes
# CDN caching for 5 minutes
# Local db caching for 1 hour
# Serve stale content for 1 day, while revalidating in the background
#
# article: { maxage: 120, s-maxage: 300, wm-s-maxage: 3600 }
# article: { maxage: 120, s-maxage: 300, wm-s-maxage: 3600, stale-while-revalidate: 86400, stale-if-error: 86400 }
taxonomy_term:
_default: { maxage: 120, s-maxage: 300 }

Expand Down

0 comments on commit d62271c

Please sign in to comment.