Skip to content

Commit

Permalink
🎨 Improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
BorghildSelle committed Feb 7, 2024
1 parent 423b34e commit b6f1ba6
Show file tree
Hide file tree
Showing 16 changed files with 102 additions and 16 deletions.
7 changes: 7 additions & 0 deletions sanityv3/schemas/documents/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ export default (isoCode: string, title: string) => {
type: 'excludeFromSearch',
name: 'excludeFromSearch',
},
{
type: 'boolean',
name: 'includeInBuild',
title: 'Include in static build',
description: 'Enable this if this route should be generated at build time',
initialValue: false,
},
],
preview: {
select: {
Expand Down
6 changes: 6 additions & 0 deletions sanityv3/schemas/documents/routeHomepage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ export default (isoCode: string, title: string) => ({
readOnly: true,
initialValue: { current: '/', _type: 'slug' },
},
{
name: 'includeInBuild',
type: 'boolean',
initialValue: true,
},
],

preview: {
select: {
title: 'content.title',
Expand Down
30 changes: 30 additions & 0 deletions web/common/helpers/getPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ const getTopicRoutesForLocale = async (locale: string) => {
)
return data
}
const getTopicRoutesForLocaleToStaticallyBuild = async (locale: string) => {
const lang = getNameFromLocale(locale)
// Empty array as fallback for satelittes
const blacklist = topicSlugBlackList[lang as keyof typeof topicSlugBlackList] || []
const data: { slug: string; _updatedAt: string }[] = await sanityClient.fetch(
groq`*[_type match "route_" + $lang + "*" && (!(slug.current in $blacklist)) && includeInBuild && defined(slug.current) && !(_id in path("drafts.**"))][] {
_updatedAt,
"slug": slug.current,
}`,
{
lang,
blacklist,
},
)
console.log('data', data)
return data
}

const getDocumentsForLocale = async (type: 'news' | 'localNews' | 'magazine', locale: string) => {
const lang = getNameFromLocale(locale)
Expand Down Expand Up @@ -64,6 +81,19 @@ export type PathType = {
locale: string
}

export const getStaticBuildRoutePaths = async (locales: string[]): Promise<PathType[]> => {
const fetchPaths = locales.map(async (locale) => {
const pages = await getTopicRoutesForLocaleToStaticallyBuild(locale)
return pages.map((page) => ({
slug: page.slug.split('/').filter((p) => p),
updatedAt: page._updatedAt,
locale,
}))
})

return (await Promise.all(fetchPaths)).flat()
}

export const getRoutePaths = async (locales: string[]): Promise<PathType[]> => {
const fetchPaths = locales.map(async (locale) => {
const pages = await getTopicRoutesForLocale(locale)
Expand Down
2 changes: 1 addition & 1 deletion web/components/src/Link/ButtonLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const ButtonLink = forwardRef<HTMLAnchorElement, ButtonLinkProps>(functio
ref,
) {
return (
<NextLink href={href} locale={locale}>
<NextLink href={href} locale={locale} prefetch={false}>
<StyledButtonLink color="secondary" variant="outlined" ref={ref} {...rest}>
{children}
</StyledButtonLink>
Expand Down
1 change: 1 addition & 0 deletions web/components/src/Link/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export const Link = forwardRef<HTMLAnchorElement, LinkProps>(function Link(
} as CSSProperties
}
$textDecoration={underline ? 'underline' : 'none'}
prefetch={false}
{...rest}
>
{children} {type === 'externalUrl' ? <Icon data={external_link} size={16} /> : null}
Expand Down
10 changes: 10 additions & 0 deletions web/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ export default withBundle(
experimental: {
largePageDataBytes: 300 * 1000,
scrollRestoration: true,
optimizePackageImports: [
'@components',
'@chakra-ui',
'@chakra-ui/skip-nav',
'@emotion/react',
'@emotion/styled',
'@equinor/eds-core-react',
'@equinor/eds-icons',
'hls.js',
],
},
eslint: {
// Warning: Dangerously allow production builds to successfully complete even if
Expand Down
2 changes: 1 addition & 1 deletion web/pageComponents/pageTemplates/newsroom/Hit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const Hit = ({ hit }: { hit: any }) => {
const pageTitle = Array.isArray(hit.pageTitle) ? toPlainText(hit.pageTitle as PortableTextBlock[]) : hit.pageTitle

return (
<StyledHitLink href={hit.slug}>
<StyledHitLink href={hit.slug} prefetch={false}>
<article>
<Date>{hit.publishDateTime && <FormattedDate datetime={hit.publishDateTime} uppercase />}</Date>
<StyledHeading level="h3" size="md">
Expand Down
2 changes: 1 addition & 1 deletion web/pageComponents/shared/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ const Footer = forwardRef<HTMLDivElement, FooterProps>(function Footer({ footerD
const icon = type === 'someLink' && someType ? getSomeSvg(someType) : null

return (
<FooterLink key={id} href={url || getLink(link)}>
<FooterLink key={id} href={url || getLink(link)} prefetch={false}>
{icon && <SomeIcon aria-hidden={true}>{icon}</SomeIcon>}
{label}
</FooterLink>
Expand Down
4 changes: 2 additions & 2 deletions web/pageComponents/shared/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ const HeadTags = ({ slugs }: { slugs: AllSlugsType }) => {
const AllSites = () => {
const allSitesURL = getAllSitesLink('external')
return (
<StyledAllSites href={allSitesURL}>
<StyledAllSites href={allSitesURL} prefetch={false}>
<FormattedMessage id="all_sites" defaultMessage="All Sites" />
</StyledAllSites>
)
Expand Down Expand Up @@ -172,7 +172,7 @@ const Header = ({ slugs, menuData }: HeaderProps) => {
>
{hasSearch && (
<ControlChild>
<NextLink href="/search">
<NextLink href="/search" prefetch={false}>
<StyledSearchButton variant="ghost_icon" aria-expanded="true" aria-label="Search">
<Icon size={24} data={search} />
</StyledSearchButton>
Expand Down
13 changes: 11 additions & 2 deletions web/pageComponents/shared/Hero/LoopingVideo.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { HLSPlayer } from '@components'
import { HLSPlayer } from '../../../components/src/HLSPlayer'
import { useSanityLoader } from '../../../lib/hooks/useSanityLoader'
import styled from 'styled-components'
import { LoopingVideoData, LoopingVideoRatio } from '../../../types'
import dynamic from 'next/dynamic'

const DEFAULT_MAX_WIDTH = 1920

const DynamicHLSVideoComponent = dynamic<React.ComponentProps<typeof HLSPlayer>>(
() => import('../../../components/src/HLSPlayer').then((mod) => mod.HLSPlayer),
{
ssr: false,
loading: () => <p>Loading...</p>,
},
)

const Container = styled.div<{ $aspectRatio: LoopingVideoRatio }>`
position: relative;
${({ $aspectRatio }) =>
Expand All @@ -27,7 +36,7 @@ const StyledFigure = styled.figure`
position: absolute;
`

const StyledHLSPlayer = styled(HLSPlayer)`
const StyledHLSPlayer = styled(DynamicHLSVideoComponent)`
position: absolute;
width: 100%;
height: 100%;
Expand Down
2 changes: 1 addition & 1 deletion web/pageComponents/shared/LocalizationSwitch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ type LocaleLinkProps = {
const LocaleLink: React.FC<React.PropsWithChildren<LocaleLinkProps>> = ({ href, title, locale, active, children }) => {
if (!active) {
return (
<StyledLink href={href} locale={locale} aria-label={title}>
<StyledLink href={href} locale={locale} aria-label={title} prefetch={false}>
{children}
</StyledLink>
)
Expand Down
2 changes: 1 addition & 1 deletion web/pageComponents/shared/LogoLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type LogoLinkProps = AnchorHTMLAttributes<HTMLAnchorElement>

export const LogoLink = ({ ...rest }: LogoLinkProps) => {
return (
<StyledLogoLink href="/" aria-label="Equinor home page" {...rest} className="logo">
<StyledLogoLink href="/" aria-label="Equinor home page" {...rest} className="logo" prefetch={false}>
<AlignedLogoSecondary />
</StyledLogoLink>
)
Expand Down
14 changes: 12 additions & 2 deletions web/pageComponents/shared/VideoPlayer.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
/* eslint-disable jsx-a11y/media-has-caption */
import styled from 'styled-components'
import dynamic from 'next/dynamic'
import {
VideoControlsType,
VideoPlayerData,
VideoPlayerRatios,
VideoType,
VideoDesignOptionsType,
} from '../../types/types'
import { BackgroundContainer, HLSPlayer } from '@components'
import { BackgroundContainer } from '@components'
import TitleText from '../shared/portableText/TitleText'
import { urlFor } from '../../common/helpers'
import IngressText from './portableText/IngressText'
import { ButtonLink } from './ButtonLink'
import { HLSPlayer } from '../../components/src/HLSPlayer'

const DynamicHLSVideoComponent = dynamic<React.ComponentProps<typeof HLSPlayer>>(
() => import('../../components/src/HLSPlayer').then((mod) => mod.HLSPlayer),
{
ssr: false,
loading: () => <p>Loading...</p>,
},
)

const StyledHeading = styled(TitleText)`
padding: 0 0 var(--space-large) 0;
Expand Down Expand Up @@ -84,7 +94,7 @@ const ButtonWrapper = styled.div`
margin-bottom: var(--space-xLarge);
`

const StyledHLSPlayer = styled(HLSPlayer)`
const StyledHLSPlayer = styled(DynamicHLSVideoComponent)`
object-fit: cover;
width: inherit;
Expand Down
4 changes: 3 additions & 1 deletion web/pageComponents/topicPages/Breadcrumbs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ export const Breadcrumbs = ({
}
return (
<BreadcrumbsListItem key={item.slug}>
<StyledNextLink href={item.slug}>{item.label}</StyledNextLink>
<StyledNextLink href={item.slug} prefetch={false}>
{item.label}
</StyledNextLink>
</BreadcrumbsListItem>
)
})}
Expand Down
14 changes: 12 additions & 2 deletions web/pageComponents/topicPages/FullWidthVideo.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { FullWidthVideoData, FullWidthVideoRatio } from '../../types/types'
import styled from 'styled-components'
import { BackgroundContainer, HLSPlayer } from '@components'
import { BackgroundContainer } from '@components'
import { HLSPlayer } from '../../components/src/HLSPlayer'
import dynamic from 'next/dynamic'

const DynamicHLSVideoComponent = dynamic<React.ComponentProps<typeof HLSPlayer>>(
() => import('../../components/src/HLSPlayer').then((mod) => mod.HLSPlayer),
{
ssr: false,
loading: () => <p>Loading...</p>,
},
)

const Container = styled.div<{ $aspectRatio: FullWidthVideoRatio }>`
position: relative;
Expand Down Expand Up @@ -35,7 +45,7 @@ const StyledFigure = styled.figure`
position: absolute;
`

const StyledHLSPlayer = styled(HLSPlayer)`
const StyledHLSPlayer = styled(DynamicHLSVideoComponent)`
position: absolute;
top: 0;
left: 0;
Expand Down
5 changes: 3 additions & 2 deletions web/pages/[[...slug]].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { defaultLanguage } from '../languages'
import Header from '../pageComponents/shared/Header'
import { FormattedMessage } from 'react-intl'
import getIntl from '../common/helpers/getIntl'
import { getRoutePaths } from '../common/helpers/getPaths'
import { getStaticBuildRoutePaths } from '../common/helpers/getPaths'
import getPageSlugs from '../common/helpers/getPageSlugs'
import { getComponentsData } from '../lib/fetchData'
import { useContext, useEffect } from 'react'
Expand Down Expand Up @@ -123,7 +123,8 @@ export const getStaticProps: GetStaticProps = async ({ params, preview = false,
}

export const getStaticPaths: GetStaticPaths = async ({ locales = [] }) => {
const routePaths = await getRoutePaths(locales)
// Not changing getRoutePaths(locales) because its used in sitemap.xml
const routePaths = await getStaticBuildRoutePaths(locales)

const paths = routePaths.map((path) => ({
params: { slug: path.slug },
Expand Down

0 comments on commit b6f1ba6

Please sign in to comment.