Skip to content

Custom event listener for EVENT_DEFINE_URL & EVENT_DEFINE_THUMB_URL #394

@birdkiwi

Description

@birdkiwi

Is your feature request related to a problem? Please describe.

I would like to add get param with timestamp to every asset url, including CP, but I cannot do this in my module, because ImageOptimize rewrites urls later.

My code in the module

Event::on(
    Asset::class,
    Asset::EVENT_DEFINE_URL,
    static function (DefineAssetUrlEvent $event) {
        /** @var Asset $asset */
        $asset = $event->sender;
        $event->url = "{$event->url}?t={$asset->dateUpdated->getTimestamp()}";
    }
);

Describe the solution you would like

I would like to see my changes in urls.

Describe alternatives you have considered

Maybe we can register custom ImageOptimize events while making changes in EVENT_DEFINE_URL & EVENT_DEFINE_THUMB_URL? Let's say ImageTransformer::EVENT_DEFINE_URL & ImageTransformer::EVENT_DEFINE_THUMB_URL

Additional context

Initial problem is that we have cache age set to one year in Amazon S3 storage. Once client "replace file" asset with the same filename through CP, browser cache keeps showing the previous file until manually refreshing browser cache. I decided to add dateUpdated timestamp to the end of the URL so it will not show cached version once replaced. But I have a problem described above. Maybe you can suggest another better solution.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions