Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x] Add UrlInvalidated event to fire after a url has been invalidated #8902

Merged

Conversation

ryanmitchell
Copy link
Contributor

@ryanmitchell ryanmitchell commented Oct 31, 2023

This PR adds a UrlInvalidated event which fires after a URL has been removed from the static cache. This allows a developer to write some logic as a result, for example re-caching the URL.

eg.

<?php

namespace App\Listeners;

use Illuminate\Support\Facades\Http;
use Statamic\Events\UrlInvalidated;

class UrlInvalidatedListener
{
    public function handle(UrlInvalidated $event)
    {
        Http::get($event->url);
    }
}

@ryanmitchell ryanmitchell changed the title [4.x] Add UrlInvalidated event to fire after an url has been invalidated [4.x] Add UrlInvalidated event to fire after a url has been invalidated Oct 31, 2023
Copy link
Member

@duncanmcclean duncanmcclean left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works great for me!

Copy link
Member

@jasonvarga jasonvarga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made the url property just concatenate domain and url.

Doing url($url) didn't work because it always based it on whatever the current request.

So if you've got site configured on multiple domains or subdomains, and you invalidate it while on one of them, the url would be wrong for the other.

'sites' => [
  'default' => ['url' => 'http://domain.com'],
  'another' => ['url' => 'http://another.com'],
],

While in the CP (which is domain.com) and you invalidate an entry in another, the event would have this:

UrlInvalidated {
  domain: 'http://another.com',
  url: '/my-entry',
  fullUrl: 'http://domain.com/my-entry', // wrong
}

Since the event has the domain and url already, I just combined them.

UrlInvalidated {
  url: 'http://another.com/my-entry'
}

The domain and relative url aren't really important so I removed those. If someone needs them we can add them later.

@jasonvarga jasonvarga merged commit 0af41aa into statamic:4.x Nov 22, 2023
19 checks passed
@ryanmitchell ryanmitchell deleted the feature/fire-event-after-url-invalidation branch November 22, 2023 16:43
@ryanmitchell
Copy link
Contributor Author

Cool. Thanks for the changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants