Skip to content

Commit

Permalink
fix(Hero image): preload image link (#473)
Browse files Browse the repository at this point in the history
  • Loading branch information
ReenaSingh07 authored Sep 13, 2024
1 parent a601e61 commit c6e619e
Show file tree
Hide file tree
Showing 16 changed files with 6,429 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@quintype/amp",
"version": "2.19.1",
"version": "2.19.2-fix-img-preload.3",
"description": "Quintype's AMP component library for publisher apps to create amp layouts",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
25 changes: 21 additions & 4 deletions src/atoms/image/image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { withConfig } from "../../context";
import { LightboxGallery } from "../lightbox-gallery";
import { base64FallbackImage } from "../../helpers/image-helpers";
import { Head } from "../index";
import get from "lodash.get";

export const BaseImage = ({
metadata,
Expand All @@ -16,6 +17,7 @@ export const BaseImage = ({
lightbox = true,
story,
useFallbackImage = false,
disableImgPreload = false,
...rest
}: ImageTypes) => {
const cdnImage = config.publisherConfig["cdn-image"];
Expand All @@ -42,20 +44,35 @@ export const BaseImage = ({
imgAttrs.alt = "fallback image";
}
if (!imgAttrs.src) return null;

imgAttrs.layout = "responsive";
imgAttrs.width = imgAspectRatio[0];
imgAttrs.height = imgAspectRatio[1];

const isHeroImage: boolean = get(imgAttrs, ["data-hero"], "false") === "true";
const imgSrcSet = get(imgAttrs, ["srcset"]);
const imgSrcSetUrl = imgSrcSet?.split(" ")?.[0];
const imgSrc = imgSrcSetUrl || get(imgAttrs, ["src"]);

return (
<Fragment>
<Head>
<style>{`
{isHeroImage && !disableImgPreload && imgSrc ? (
<Head>
<link rel="preload" as="image" href={imgSrc} fetchPriority="high" />
<style>{`
.hero-image img{
object-fit: contain;
}
`}</style>
</Head>
) : (
<Head>
<style>{`
.hero-image img{
object-fit: contain;
}
`}</style>
</Head>
</Head>
)}
{lightbox ? (
<Fragment>
<LightboxGallery />
Expand Down
1 change: 1 addition & 0 deletions src/atoms/image/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface ImageTypes extends Common {
lightbox?: string | boolean;
useFallbackImage?: boolean;
story?: Story;
disableImgPreload?: boolean;
}

interface ImageMetadata {
Expand Down
11 changes: 3 additions & 8 deletions src/helpers/image-helpers/image-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@ export const getImgSrcAndSrcset = ({ opts, slug, metadata, aspectRatio, cdnImage
const imgOpts = isGumlet ? { format: "auto", ...opts } : opts;
const src = focusedImagePath({ opts: imgOpts, slug, metadata, aspectRatio, cdnImage });
let srcset = "";
const srcsetOpts = [{ ...imgOpts, w: 640 }];
srcsetOpts.forEach((val, i) => {
if (i === srcsetOpts.length - 1) {
srcset += `${focusedImagePath({ opts: val, slug, metadata, aspectRatio, cdnImage })} ${val.w}w`;
} else {
srcset += `${focusedImagePath({ opts: val, slug, metadata, aspectRatio, cdnImage })} ${val.w}w, `;
}
});
const srcsetOpt = { ...imgOpts, w: 640 };
srcset += `${focusedImagePath({ opts: srcsetOpt, slug, metadata, aspectRatio, cdnImage })} ${srcsetOpt.w}w`;

return { src, srcset };
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

exports[`DateUpdated should match snapshot 1`] = `
<Component
formattedDate="about 4 years ago"
formattedDate="over 4 years ago"
prepend="Story Updated:"
/>
`;
2 changes: 1 addition & 1 deletion src/molecules/header-card/header-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const DefaultHeaderCard = ({ story, config, storyType }: HeaderCardProps)

return (
<div>
<HeroImage />
<HeroImage config={config} />
<Spacer token="xs" />
<HeaderCardContainer>
<Section section={story.sections[0]} />
Expand Down
11 changes: 8 additions & 3 deletions src/molecules/hero-image/hero-image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,25 @@ const StyledFigcaption = styled.div`
text-align: left;
padding-top: 8px;
`;
export const HeroImageBase = ({ story }: HeroImageBaseTypes) => {
export const HeroImageBase = ({ story, config }: HeroImageBaseTypes) => {
const metadata: HeroImageMetadata = get(story, "hero-image-metadata", null);
const slug: string | null = get(story, "hero-image-s3-key", null);
if (!slug || !metadata) return null;

const attribution: string | null = get(story, "hero-image-attribution", null);
const caption: string | null = get(story, "hero-image-caption", null);
const altText: string | null = get(story, "hero-image-alt-text", null);

const disableImgPreload: boolean = get(config, ["opts", "optimizeAmpHtml"], true);

return (
<>
<StyledDiv>
<Image data-hero="true" metadata={metadata} slug={slug} alt={altText || caption || attribution || ""}></Image>
<Image
data-hero={"true"}
metadata={metadata}
slug={slug}
alt={altText || caption || attribution || ""}
disableImgPreload={disableImgPreload}></Image>
</StyledDiv>
<StyledFigcaption>
{caption && <StyledCaption dangerouslySetInnerHTML={{ __html: `${caption}` + "&nbsp;" }} />}
Expand Down
2 changes: 2 additions & 0 deletions src/molecules/hero-image/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Config } from "../../types/config";
import { Story } from "../../types/story";
export interface HeroImageBaseTypes {
story: Story;
config?: Config;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ exports[`RelatedStoryCard should render default 1`] = `
>
<Component
alt="image"
disableImgPreload={true}
metadata={null}
slug={null}
useFallbackImage={true}
Expand Down
10 changes: 8 additions & 2 deletions src/molecules/related-story-card/related-story-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,17 @@ export const RelatedStoryCard = ({ story, config }: RelatedStoryCardTypes) => {
});
}
}

const disableImgPreload: boolean = get(config, ["opts", "optimizeAmpHtml"], true);
return (
<Wrapper>
<StyledAnchor href={url}>
<Image metadata={imgMetadata} slug={imgS3Key} alt={imgAltText || imgCaption || imgAttr || "image"} useFallbackImage={true} />
<Image
metadata={imgMetadata}
slug={imgS3Key}
alt={imgAltText || imgCaption || imgAttr || "image"}
useFallbackImage={true}
disableImgPreload={disableImgPreload}
/>
<Headline>{headline}</Headline>
<DateTime formattedDate={humanizedDate} />
<Spacer token="s" />
Expand Down
Loading

0 comments on commit c6e619e

Please sign in to comment.