Skip to content

Commit d62271c

Browse files
Support stale-while-revalidate and stale-if-error
1 parent e34cff5 commit d62271c

4 files changed

Lines changed: 33 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## Unreleased
88

9+
## [1.1.1] - 2023-07-19
10+
### Added
11+
- Support `stale-while-revalidate` and `stale-if-error` cache control header
12+
- [https://developer.fastly.com/learning/concepts/stale](http://web.archive.org/web/20230719193134/https://developer.fastly.com/learning/concepts/stale/)
13+
914
## [1.1.0] - 2022-06-28
1015
### Added
1116
- Add v2 upgrade guide & script

src/EventSubscriber/CacheSubscriber.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,19 @@ protected function setMaxAge(Response $response, array $definition)
233233
$response->setSharedMaxAge($definition['s-maxage']);
234234
}
235235

236-
if (isset($definition['wm-s-maxage'])) {
237-
$response->headers->addCacheControlDirective(
238-
'wm-s-maxage',
239-
$definition['wm-s-maxage']
240-
);
236+
$directives = [
237+
'wm-s-maxage',
238+
'stale-while-revalidate',
239+
'stale-if-error',
240+
];
241+
242+
foreach ($directives as $directive) {
243+
if (isset($definition[$directive])) {
244+
$response->headers->addCacheControlDirective(
245+
$directive,
246+
$definition[$directive]
247+
);
248+
}
241249
}
242250
}
243251
}

src/Service/Cache/MaxAgeDecider.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public function onResponseEarly(FilterResponseEvent $event)
4949
'maxage' => $headers->getCacheControlDirective('max-age'),
5050
's-maxage' => $headers->getCacheControlDirective('s-maxage'),
5151
'wm-s-maxage' => $headers->getCacheControlDirective('wm-s-maxage'),
52+
'stale-while-revalidate' => $headers->getCacheControlDirective('stale-while-revalidate'),
53+
'stale-if-error' => $headers->getCacheControlDirective('stale-if-error'),
5254
],
5355
static fn ($value) => $value !== null
5456
);
@@ -75,6 +77,8 @@ public function getMaxage(Request $request, Response $response)
7577
's-maxage' => $request->attributes->get('_smaxage', 0),
7678
'maxage' => $request->attributes->get('_maxage', 0),
7779
'wm-s-maxage' => $request->attributes->get('_wmsmaxage', null),
80+
'stale-while-revalidate' => $request->attributes->get('_stale-while-revalidate', null),
81+
'stale-if-error' => $request->attributes->get('_stale-if-error', null),
7882
];
7983
}
8084

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

95-
return $explicit + ['s-maxage' => 0, 'maxage' => 0, 'wm-s-maxage' => null];
99+
return $explicit + [
100+
's-maxage' => 0,
101+
'maxage' => 0,
102+
'wm-s-maxage' => null,
103+
'stale-while-revalidate' => null,
104+
'stale-if-error' => null,
105+
];
96106
}
97107

98108
protected function getMaxAgesForMainEntity()

wmcontroller.services.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ parameters:
3030
# maxage = client side caching duration
3131
# s-maxage = server side caching duration (this can be drupal db or a cdn)
3232
# wm-s-maxage = custom cache-control directive for different local cache ttl
33+
# stale-while-revalidate indicates that caches MAY serve the response after it becomes stale, up to the indicated number of seconds.
34+
# 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.
3335
wmcontroller.cache.expiry:
3436
# Determine max and s-max based on content-type and/or bundle.
3537
# _default is used when no definition is available for any given bundle.
@@ -40,8 +42,9 @@ parameters:
4042
# Client side caching for 2 minutes
4143
# CDN caching for 5 minutes
4244
# Local db caching for 1 hour
45+
# Serve stale content for 1 day, while revalidating in the background
4346
#
44-
# article: { maxage: 120, s-maxage: 300, wm-s-maxage: 3600 }
47+
# article: { maxage: 120, s-maxage: 300, wm-s-maxage: 3600, stale-while-revalidate: 86400, stale-if-error: 86400 }
4548
taxonomy_term:
4649
_default: { maxage: 120, s-maxage: 300 }
4750

0 commit comments

Comments
 (0)