Skip to content

Commit

Permalink
⚡ Lazy-loading date-fns: sparer ~10kB first-load (#2058)
Browse files Browse the repository at this point in the history
  • Loading branch information
KenAJoh authored Jun 19, 2023
1 parent ed7aafc commit 9f59e67
Show file tree
Hide file tree
Showing 22 changed files with 558 additions and 520 deletions.
138 changes: 71 additions & 67 deletions aksel.nav.no/website/components/layout/WithSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,84 +75,88 @@ export const WithSidebar = ({
<>
<Header />
<div className="bg-bg-default">
<div className="mx-auto mt-6 mb-24 flex w-full max-w-screen-2xl gap-6">
<div className="mx-auto mb-24 mt-6 flex w-full max-w-screen-2xl gap-6">
<Sidebar kategori={pageType.type} links={sidebar} />
<div className="relative w-full">
<main
tabIndex={-1}
id="hovedinnhold"
className="min-h-screen-header md:max-w-screen-sidebar relative w-full px-4 focus:outline-none sm:pl-6 sm:pr-6 md:pl-0"
>
<div
className={cl(
"relative mb-10 min-h-[12.5rem] overflow-hidden rounded-xl pl-6 pr-4 md:pl-10 lg:pr-10",
{
"bg-surface-subtle flex items-center justify-between":
variant === "page",
"bg-deepblue-700 grid py-[4.25rem] pb-6":
variant !== "page",
}
)}
>
<div className="z-[1]">
{variant === "page" && pageProps?.kategori && (
<Detail as="div" className="mb-2">
<NextLink
href={`/${pageType.type.toLowerCase()}`}
passHref
legacyBehavior
>
<Link className="text-text-default">
{capitalize(pageType.type)}
</Link>
</NextLink>{" "}
/ {capitalize(pageProps.kategori)}
</Detail>
<div className="from-deepblue-700 rounded-xl bg-gradient-to-br via-blue-500 to-violet-700">
<div
className={cl(
"relative mb-10 min-h-[12.5rem] overflow-hidden rounded-xl pl-6 pr-4 md:pl-10 lg:pr-10",
{
"bg-surface-subtle flex items-center justify-between":
variant === "page",
"bg-deepblue-700/80 grid py-[4.25rem] pb-6":
variant !== "page",
}
)}
>
<div className="z-[1]">
{variant === "page" && pageProps?.kategori && (
<Detail as="div" className="mb-2">
<NextLink
href={`/${pageType.type.toLowerCase()}`}
passHref
legacyBehavior
>
<Link className="text-text-default">
{capitalize(pageType.type)}
</Link>
</NextLink>{" "}
/ {capitalize(pageProps.kategori)}
</Detail>
)}

<Heading
level="1"
size="xlarge"
className={cl({
"text-deepblue-800": variant === "page",
"text-text-on-action bg-deepblue-700/80 w-fit":
variant !== "page",
})}
>
{pageType.title}
</Heading>
<div
className={cl({
"bg-deepblue-700/80 w-fit": variant !== "page",
})}
>
{intro && intro}
<Heading
level="1"
size="xlarge"
className={cl({
"text-deepblue-800": variant === "page",
"text-text-on-action w-fit": variant !== "page",
})}
>
{pageType.title}
</Heading>
<div
className={cl({
" w-fit": variant !== "page",
})}
>
{intro && intro}
</div>
</div>
{variant === "page" && pageProps.status?.bilde && (
<div
className={cl(
"relative hidden aspect-square h-[12.5rem] lg:block xl:mr-40",
{
"hue-rotate-[65deg]":
pageProps?.status?.tag === "beta",
}
)}
>
<Image
src={urlFor(pageProps.status?.bilde)
.auto("format")
.url()}
decoding="async"
layout="fill"
objectFit="contain"
aria-hidden
priority
/>
</div>
)}
{variant === "landingPage" && (
<div className="pointer-events-none absolute right-0 top-0 hidden sm:block">
<HeaderCube className="text-deepblue-300 z-[-1] max-h-full" />
</div>
)}
</div>
{variant === "page" && pageProps.status?.bilde && (
<div
className={cl(
"relative hidden aspect-square h-[12.5rem] lg:block xl:mr-40",
{
"hue-rotate-[65deg]": pageProps?.status?.tag === "beta",
}
)}
>
<Image
src={urlFor(pageProps.status?.bilde).auto("format").url()}
decoding="async"
layout="fill"
objectFit="contain"
aria-hidden
priority
/>
</div>
)}
{variant === "landingPage" && (
<div className="pointer-events-none absolute top-0 right-0 hidden sm:block">
<HeaderCube className="text-deepblue-300 z-0 max-h-full" />
</div>
)}
</div>
<div className={cl("sm:px-6 md:px-10", { flex: withToc })}>
{withToc && (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { withErrorBoundary } from "@/error-boundary";

import {
AkselGodPraksisDocT,
ResolveContributorsSingleT,
ResolveTemaT,
ResolveSlugT,
AkselGodPraksisDocT,
ResolveTemaT,
} from "@/types";
import { BodyShort, Detail, Heading } from "@navikt/ds-react";
import { useFormatedDate } from "components/website-modules/utils/getDate";
import NextLink from "next/link";
import { abbrName, dateStr, logNav } from "../..";
import { abbrName, logNav } from "../..";

const ArtikkelCard = ({
slug,
Expand All @@ -25,11 +26,9 @@ const ArtikkelCard = ({
}: ResolveContributorsSingleT<
ResolveTemaT<ResolveSlugT<AkselGodPraksisDocT>>
> & { source: string; variant: string; level?: "2" | "3" }) => {
const date = (rest as any)?.updateInfo?.lastVerified
? (rest as any)?.updateInfo?.lastVerified
: publishedAt
? publishedAt
: _updatedAt;
const date = useFormatedDate(
(rest as any)?.updateInfo?.lastVerified ?? publishedAt ?? _updatedAt
);

return (
<div className="hover:shadow-small focus-within:ring-border-focus bg-surface-default ring-border-subtle group relative rounded-lg p-3 pb-16 ring-1 focus-within:ring-[3px] sm:p-5 sm:pb-16">
Expand Down Expand Up @@ -70,7 +69,7 @@ const ArtikkelCard = ({
</Detail>
)}
<Detail as="span" className="text-text-subtle">
{dateStr(date)}
{date}
</Detail>
</span>
) : (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { withErrorBoundary } from "@/error-boundary";
import { AkselBloggDocT, ResolveContributorsT, ResolveSlugT } from "@/types";
import { BodyLong, BodyShort, Heading, Link } from "@navikt/ds-react";
import { getAuthors } from "components/website-modules/LatestBloggs";
import { useFormatedDate } from "components/website-modules/utils/getDate";
import NextLink from "next/link";
import { dateStr, logNav } from "../..";
import { getAuthors, logNav } from "@/utils";

const BloggCard = ({
blog,
}: {
blog: ResolveContributorsT<ResolveSlugT<AkselBloggDocT>>;
}) => {
const date = useFormatedDate(blog?.publishedAt ?? blog._createdAt);
return (
<li
key={blog._id}
Expand Down Expand Up @@ -38,7 +39,7 @@ const BloggCard = ({
className="text-text-subtle mt-auto flex gap-2 justify-self-end"
>
<span className="font-semibold">{getAuthors(blog)[0]}</span>
<span>{dateStr(blog?.publishedAt ?? blog._createdAt)}</span>
<span>{date}</span>
</BodyShort>
)}
</li>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { abbrName, dateStr } from "@/utils";
import { withErrorBoundary } from "@/error-boundary";
import { urlFor } from "@/sanity/interface";
import { abbrName } from "@/utils";
import { BodyShort, Detail, Heading } from "@navikt/ds-react";
import cl from "clsx";
import { logNav } from "components/website-modules/utils/amplitude";
import NextLink from "next/link";
import { getImage } from "components/website-modules/utils/get-image";
import { useFormatedDate } from "components/website-modules/utils/getDate";
import NextImage from "next/legacy/image";
import NextLink from "next/link";
import { Tag } from "./Tag";
import cl from "clsx";
import { getImage } from "components/website-modules/utils/get-image";
import { urlFor } from "@/sanity/interface";
import { withErrorBoundary } from "@/error-boundary";

export type ArticleT = {
_key: string;
Expand Down Expand Up @@ -37,7 +38,7 @@ const Card = ({
visible: boolean;
index: number;
}) => {
const date = article.publishedAt ? article.publishedAt : article._updatedAt;
const date = useFormatedDate(article.publishedAt ?? article._updatedAt);

const showFooter = ["aksel_artikkel", "aksel_blogg"].includes(article._type);
const showImage = [
Expand Down Expand Up @@ -142,7 +143,7 @@ const Card = ({
{abbrName(article?.contributors[0]?.title)}
</Detail>
)}
<Detail as="span">{dateStr(date)}</Detail>
<Detail as="span">{date}</Detail>
</span>
)}
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { urlFor } from "@/sanity/interface";
import { getAuthors, logNav } from "@/utils";
import { BodyLong, BodyShort, Heading, Link } from "@navikt/ds-react";
import cl from "clsx";
import { getImage } from "components/website-modules/utils/get-image";
import { useFormatedDate } from "components/website-modules/utils/getDate";
import Image from "next/legacy/image";
import NextLink from "next/link";
import { ArticleT } from "./Card";
import { Tag } from "./Tag";

export const Highlight = ({
article,
compact,
}: {
article: ArticleT;
compact: boolean;
}) => {
const showFooter = ["aksel_artikkel", "aksel_blogg"].includes(article._type);
const useStatusImage =
["ds_artikkel", "komponent_artikkel"].includes(article._type) &&
article.status?.bilde;

const date = useFormatedDate(article?.publishedAt ?? article._createdAt);

return (
<section
aria-label={`Fremhevet artikkel: ${article?.heading}`}
className={cl({
"grid content-start gap-8 md:grid-cols-2": compact,
"grid content-start gap-6": !compact,
})}
>
<div className="relative block aspect-video">
{useStatusImage ? (
<Image
src={urlFor(article.status.bilde).quality(100).auto("format").url()}
quality={100}
layout="fill"
aria-hidden
priority
className={cl(
"bg-deepblue-200 rounded-lg object-cover sm:object-contain",
{
"hue-rotate-[65deg]": article?.status?.tag === "beta",
}
)}
decoding="sync"
/>
) : article?.seo?.image ? (
<Image
src={urlFor(article.seo.image).quality(100).auto("format").url()}
quality={100}
layout="fill"
objectFit="cover"
aria-hidden
priority
className={cl("rounded-lg", {
"hue-rotate-[65deg]": article?.status?.tag === "beta",
})}
decoding="sync"
/>
) : (
<Image
src={getImage(article?.heading ?? "", "thumbnail")}
layout="fill"
objectFit="contain"
aria-hidden
priority
className="rounded-lg"
decoding="sync"
/>
)}
</div>
<div>
<Tag
type={article._type}
text={article.tema ? article.tema[0] : undefined}
beta={article?.status?.tag === "beta"}
/>
<NextLink href={`/${article.slug.current}`} passHref legacyBehavior>
<Link
onClick={(e) =>
logNav(
"artikkel-kort",
window.location.pathname,
e.currentTarget.getAttribute("href")
)
}
className="text-text-default mb-5 mt-2 no-underline hover:underline"
>
<Heading size="large" level="3">
{article?.heading}
</Heading>
</Link>
</NextLink>
<BodyLong className="mb-4" size="small">
{article?.ingress ?? article.seo?.meta}
</BodyLong>
{showFooter && getAuthors(article as any).length > 0 && (
<BodyShort size="small" className="text-text-subtle flex gap-2">
<span className="font-semibold">
{getAuthors(article as any)[0]}
</span>
<span>{date}</span>
</BodyShort>
)}
</div>
</section>
);
};
Loading

0 comments on commit 9f59e67

Please sign in to comment.