Use smaller, PNG image for meta image

This ensures it's not too large, but also crawlers are more likely to
support PNG than webp
This commit is contained in:
Jake Howard 2022-09-03 16:21:24 +01:00
parent be009dcb37
commit eaa5b063f4
Signed by: jake
GPG Key ID: 57AFB45680EDD477
2 changed files with 17 additions and 15 deletions

View File

@ -26,10 +26,13 @@ class LMOTFYSerializer(serializers.ModelSerializer):
def get_full_url(self, page: BlogPostPage) -> str:
return page.get_full_url(request=self.context["request"])
def get_image(self, page: BlogPostPage) -> str:
hero_image_url = page.hero_image_url
def get_image(self, page: BlogPostPage) -> str | None:
image_url = page.get_meta_image_url(request=self.context["request"])
if isinstance(hero_image_url, str) and hero_image_url[0] == "/":
return self.context["request"].build_absolute_uri(hero_image_url)
if not image_url:
return None
return hero_image_url
if image_url[0] == "/":
return self.context["request"].build_absolute_uri(image_url)
return image_url

View File

@ -128,27 +128,26 @@ class BaseContentPage(BasePage, MetadataMixin):
def plain_text(self) -> str:
return extract_text(self.content_html)
@cached_property
def hero_image_url(self) -> Optional[str]:
def hero_url(self, unsplash_size: str, wagtail_image_spec: str) -> Optional[str]:
if self.hero_unsplash_photo_id is not None:
return self.hero_unsplash_photo.get_image_urls()["regular"]
return self.hero_unsplash_photo.get_image_urls()[unsplash_size]
elif self.hero_image_id is not None:
return generate_image_url(self.hero_image, "width-1200")
return generate_image_url(self.hero_image, wagtail_image_spec)
return None
@cached_property
def hero_image_url(self) -> Optional[str]:
return self.hero_url("full", "width-2000")
@cached_property
def list_image_url(self) -> Optional[str]:
if self.hero_unsplash_photo_id is not None:
return self.hero_unsplash_photo.get_image_urls()["small"]
elif self.hero_image_id is not None:
return generate_image_url(self.hero_image, "width-400")
return None
return self.hero_url("small", "width-400")
def get_meta_url(self) -> str:
return self.full_url
def get_meta_image_url(self, request: HttpRequest) -> Optional[str]:
return self.hero_image_url
return self.hero_url("regular", "width-1000|format-png")
def get_meta_title(self) -> str:
return self.html_title