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

Asset (CDN) invalidation should also get triggered on asset alteration (rotate, resize, etc). #4379

Open
nateiler opened this issue Jun 6, 2019 · 1 comment

Comments

Projects
None yet
4 participants
@nateiler
Copy link
Contributor

commented Jun 6, 2019

I noticed that asset invalidation is only triggered when an asset is deleted. However, if you edit an asset (ex: rotate it via the editor), this should also trigger invalidation.

Furthermore, in order to invalidate transforms it's probably wise to support wildcard invalidation; though this may be specific to the provider.

As an example, via AWS S3 / Cloudfront we'll need to invalidate:
path/to/asset/file.jpg and path/to/asset/*/file.jpg The /*/ catches all the image transforms

@dsmrt

This comment has been minimized.

Copy link

commented Jun 6, 2019

To add to this, here's what we are doing to hook-in via element afterSave (which targets aws/cloudfront assets specifically).

       /** @var Asset $asset */
        $asset = $event->sender;
        if (! $asset instanceof Asset) {
            return;
        }

        /** @var Volume $volume */
        $volume = $asset->getVolume();
        if (! $volume instanceof Volume) {
            return;
        }

        $subfolder = $volume->subfolder ? '/' . $volume->subfolder : '';
        $path = $subfolder .
            '/' . $asset->getPath($asset->getFilename());
        $transformerPath = $subfolder . '/' .
            //remove the filename from the path
            str_replace($asset->getFilename(), '', $asset->getPath()) .
            '*/' . $asset->getFilename();

        // using instance profile
        $client = new CloudFrontClient([
            'region' => static::AWS_DEFAULT_REGION,
            'version' => 'latest',
        ]);

        $result = $client->createInvalidation([
            'DistributionId' => $volume->cfDistributionId,
            'InvalidationBatch' => [
                'CallerReference' => 'Craft-' . StringHelper::randomString(24),
                'Paths' => [
                    'Items' => [
                        $path,
                        $transformerPath,
                    ],
                    'Quantity' => 2,
                ],
            ],
        ]);

I've tried to following the logic here \craft\awss3\Volume::invalidateCdnPath and I think the path might be missing subfolder (see my above code). Just a heads up (I'm not 100% sure though).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.