dnscms: version image URLs by file hash so media can be cached as immutable
This commit is contained in:
4 files changed
+122
-5
No files matched your search
@@ -4,6 +4,7 @@ from django.utils.translation import gettext_lazy as _
|
||||
from grapple.models import (
|
||||
GraphQLString,
|
||||
)
|
||||
from grapple.utils import get_media_item_url
|
||||
from wagtail.images.models import AbstractImage, AbstractRendition, Image
|
||||
from wagtail.search import index
|
||||
|
||||
@@ -44,7 +45,20 @@ class CustomImage(AbstractImage):
|
||||
("choose_image", "Can choose image"),
|
||||
]
|
||||
|
||||
@property
|
||||
def versioned_url(self):
|
||||
# Original files keep their upload name, so deleting an image and
|
||||
# uploading another with the same name reuses the URL. The content hash
|
||||
# makes the URL change with the file, which lets the media proxy and
|
||||
# Next's image optimizer cache it as immutable.
|
||||
url = get_media_item_url(self)
|
||||
if not self.file_hash:
|
||||
return url
|
||||
return f"{url}?v={self.file_hash[:8]}"
|
||||
|
||||
graphql_fields = [
|
||||
# Replaces Grapple's own `url` field and resolver
|
||||
GraphQLString("url", source="versioned_url", required=True),
|
||||
GraphQLString("alt"),
|
||||
GraphQLString("attribution"),
|
||||
]
|
||||
|
||||
Reference in new issue
Block a user