Skip to content

Commit

Permalink
Clear homepage cache when post is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaucau committed Nov 29, 2021
1 parent 1e227e6 commit 03fe459
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Middleware/LSCachePurgeMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
$purgeList = $this->settings->get('acpl-lscache.purge_on_discussion_update');
if (!empty($purgeList)) {
$purgeList = explode("\n", $purgeList);
// Get only valid items
$purgeList = array_filter($purgeList, fn($item) => Str::startsWith($item, ['/', 'tag=']));
$purgeParams = array_merge($purgeParams, $purgeList);
}

// If this is a post update, we don't need to clear the home page cache
if ($routeName !== 'posts.update') {
$isPostUpdate = $routeName === 'posts.update';
if (($isPostUpdate && Arr::has($body, 'data.attributes.isHidden')) || !$isPostUpdate) {
array_push($purgeParams, 'tag=default', 'tag=index', 'tag=discussions.index');
}
}
Expand All @@ -80,9 +82,13 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
}
}

if (!$isDiscussion && Str::endsWith($routeName, ['.create', '.update', '.delete'])) {
if (Str::endsWith($routeName, ['.create', '.update', '.delete'])) {
$rootRouteName = LSCache::extractRootRouteName($routeName);
array_push($purgeParams, "tag=$rootRouteName.index");

// discussions.index is handled earlier
if (!$isDiscussion) {
array_push($purgeParams, "tag=$rootRouteName.index");
}

if (!empty($params) && !empty($params['id'])) {
array_push($purgeParams, "tag=$rootRouteName{$params['id']}");
Expand Down

0 comments on commit 03fe459

Please sign in to comment.