Skip to content

Commit

Permalink
Revert "✨ Take back algolia #2657"
Browse files Browse the repository at this point in the history
This reverts commit 8aa4b7a.
  • Loading branch information
padms committed Dec 9, 2024
1 parent 8aa4b7a commit 79e16c1
Show file tree
Hide file tree
Showing 8 changed files with 486 additions and 57 deletions.
48 changes: 27 additions & 21 deletions web/pages/news/index.global.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
import { GetStaticProps } from 'next'
import { GetServerSideProps } from 'next'
import type { AppProps } from 'next/app'
import { IntlProvider } from 'react-intl'
import Footer from '../../pageComponents/shared/Footer'
import Header from '../../pageComponents/shared/Header'
import { renderToString } from 'react-dom/server'
import { newsroomQuery } from '../../lib/queries/newsroom'
import { allNewsDocuments, newsroomQuery } from '../../lib/queries/newsroom'
import getIntl from '../../common/helpers/getIntl'
import { getNameFromLocale, getIsoFromLocale } from '../../lib/localization'
import { defaultLanguage } from '../../languages'
import { AlgoliaIndexPageType, NewsRoomPageType } from '../../types'
import { getComponentsData } from '../../lib/fetchData'
import NewsRoomTemplate from '@templates/newsroom/Newsroom'
import { getServerState, InstantSearchSSRProvider } from 'react-instantsearch'
import { getComponentsData, getData } from '../../lib/fetchData'
import NewsRoomTemplateSanity from '@templates/newsroom/sanity/NewsroomSanity'

export default function NewsRoom({ data, serverState }: AlgoliaIndexPageType) {
export default function NewsRoom({ data }: AlgoliaIndexPageType) {
const defaultLocale = defaultLanguage.locale
const { pageData, slug, intl } = data
const locale = data?.intl?.locale || defaultLocale

return (
<InstantSearchSSRProvider {...serverState}>
<IntlProvider
locale={getIsoFromLocale(locale)}
defaultLocale={getIsoFromLocale(defaultLocale)}
messages={intl?.messages}
>
<NewsRoomTemplate locale={locale} pageData={pageData as NewsRoomPageType} slug={slug} />
</IntlProvider>
</InstantSearchSSRProvider>
<IntlProvider
locale={getIsoFromLocale(locale)}
defaultLocale={getIsoFromLocale(defaultLocale)}
messages={intl?.messages}
>
<NewsRoomTemplateSanity locale={locale} pageData={pageData as NewsRoomPageType} slug={slug} />
</IntlProvider>
)
}

Expand Down Expand Up @@ -60,7 +56,7 @@ NewsRoom.getLayout = (page: AppProps) => {
)
}

export const getStaticProps: GetStaticProps = async ({ preview = false, locale = 'en' }) => {
export const getServerSideProps: GetServerSideProps = async ({ req, preview = false, locale = 'en' }) => {
// For the time being, let's just give 404 for satellites
// We will also return 404 if the locale is not English.
// This is a hack and and we should improve this at some point
Expand All @@ -79,6 +75,8 @@ export const getStaticProps: GetStaticProps = async ({ preview = false, locale =
lang,
}

const slug = req.url

const { menuData, pageData, footerData } = await getComponentsData(
{
query: newsroomQuery,
Expand All @@ -87,18 +85,26 @@ export const getStaticProps: GetStaticProps = async ({ preview = false, locale =
preview,
)

const serverState = await getServerState(<NewsRoom data={{ menuData, pageData, footerData, intl }} />, {
renderToString,
console.log(JSON.stringify(req.headers))
const url = new URL(req.headers.referer || `https://${req.headers.host}${req.url}`).toString()
const { data } = await getData({
query: allNewsDocuments,
queryParams,
})

return {
props: {
url,
data: {
menuData,
footerData,
intl,
pageData,
pageData: {
...pageData,
newsArticles: data,
},
slug,
},
serverState,
},
}
}
55 changes: 29 additions & 26 deletions web/pages/nyheter/index.global.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
import { GetStaticProps } from 'next'
import { GetServerSideProps } from 'next'
import type { AppProps } from 'next/app'
import { IntlProvider } from 'react-intl'
import Footer from '../../pageComponents/shared/Footer'
import Header from '../../pageComponents/shared/Header'
import { renderToString } from 'react-dom/server'
import { newsroomQuery } from '../../lib/queries/newsroom'
import { allNewsDocuments, newsroomQuery } from '../../lib/queries/newsroom'
import getIntl from '../../common/helpers/getIntl'
import { getNameFromLocale, getIsoFromLocale } from '../../lib/localization'
import { defaultLanguage } from '../../languages'
import { AlgoliaIndexPageType, NewsRoomPageType } from '../../types'
import { getComponentsData } from '../../lib/fetchData'
import NewsRoomTemplate from '@templates/newsroom/Newsroom'
import { getServerState, InstantSearchSSRProvider } from 'react-instantsearch'
import { getComponentsData, getData } from '../../lib/fetchData'
import NewsRoomTemplateSanity from '@templates/newsroom/sanity/NewsroomSanity'

export default function NorwegianNewsRoom({ data, serverState }: AlgoliaIndexPageType) {
export default function NorwegianNewsRoom({ data, url }: AlgoliaIndexPageType) {
const defaultLocale = defaultLanguage.locale
const { pageData, slug, intl } = data
const locale = intl?.locale || defaultLocale

return (
<InstantSearchSSRProvider {...serverState}>
<IntlProvider
locale={getIsoFromLocale(locale)}
defaultLocale={getIsoFromLocale(defaultLocale)}
messages={intl?.messages}
>
<NewsRoomTemplate locale={locale} pageData={pageData as NewsRoomPageType} slug={slug} />
</IntlProvider>
</InstantSearchSSRProvider>
<IntlProvider
locale={getIsoFromLocale(locale)}
defaultLocale={getIsoFromLocale(defaultLocale)}
messages={intl?.messages}
>
<NewsRoomTemplateSanity locale={locale} pageData={pageData as NewsRoomPageType} slug={slug} url={url} />
</IntlProvider>
)
}

Expand All @@ -36,8 +32,6 @@ NorwegianNewsRoom.getLayout = (page: AppProps) => {
// @ts-ignore
const { props } = page
const { data } = props

// Too hardcoded?
const slugs = [
{ slug: '/news', lang: 'en_GB' },
{ slug: '/nyheter', lang: 'nb_NO' },
Expand All @@ -60,10 +54,10 @@ NorwegianNewsRoom.getLayout = (page: AppProps) => {
)
}

export const getStaticProps: GetStaticProps = async ({ preview = false, locale = 'en' }) => {
export const getServerSideProps: GetServerSideProps = async ({ req, preview = false, locale = 'no' }) => {
// For the time being, let's just give 404 for satellites
// We will also return 404 if the locale is not English.
// This is a hack and and we should improve this at some point
// We will also return 404 if the locale is not Norwegian.
// This is a hack, and we should improve this at some point
// See https://github.com/vercel/next.js/discussions/18485

if (locale !== 'no') {
Expand All @@ -75,30 +69,39 @@ export const getStaticProps: GetStaticProps = async ({ preview = false, locale =
const lang = getNameFromLocale(locale)
const intl = await getIntl(locale, false)

const url = new URL(req.headers.referer || `https://${req.headers.host}${req.url}`).toString()
const queryParams = {
lang,
}

const slug = req.url

const { menuData, pageData, footerData } = await getComponentsData(
{
query: newsroomQuery,
queryParams,
},
preview,
)

const serverState = await getServerState(<NorwegianNewsRoom data={{ menuData, footerData, pageData, intl }} />, {
renderToString,
const { data } = await getData({
query: allNewsDocuments,
queryParams,
})

return {
props: {
locale,
url,
data: {
menuData,
footerData,
intl,
pageData,
pageData: {
...pageData,
newsArticles: data,
},
slug,
},
serverState,
},
}
}
28 changes: 18 additions & 10 deletions web/templates/newsroom/Newsroom.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { forwardRef, useRef } from 'react'
import { forwardRef, useMemo, useRef } from 'react'
import singletonRouter from 'next/router'
import Blocks from '../../pageComponents/shared/portableText/Blocks'
import type { NewsRoomPageType } from '../../types'
Expand All @@ -20,13 +20,15 @@ import { List } from '@core/List'
import { PaginationContextProvider } from '../../common/contexts/PaginationContext'

type NewsRoomTemplateProps = {
isServerRendered?: boolean
locale?: string
pageData?: NewsRoomPageType | undefined
slug?: string
url?: string
}

const NewsRoomTemplate = forwardRef<HTMLElement, NewsRoomTemplateProps>(function NewsRoomTemplate(
{ locale, pageData, slug },
{ isServerRendered, locale, pageData, slug, url },
ref,
) {
const { ingress, title, seoAndSome, subscriptionLink, subscriptionLinkTitle, localNewsPages, fallbackImages } =
Expand Down Expand Up @@ -87,7 +89,7 @@ const NewsRoomTemplate = forwardRef<HTMLElement, NewsRoomTemplateProps>(function
const routing = {
router: createInstantSearchRouterNext({
singletonRouter,
// serverUrl: `http://localhost:3000/${isoCode === 'nb-NO' ? 'no/nyheter' : 'news'}`, // uncomment this line for local development
serverUrl: url,
routerOptions: {
createURL: createURL,
parseURL: parseURL,
Expand Down Expand Up @@ -128,17 +130,23 @@ const NewsRoomTemplate = forwardRef<HTMLElement, NewsRoomTemplateProps>(function
},
}

const searchClient = client()
const searchClient = useMemo(() => {
return isServerRendered
? client({ headers: {
//@ts-ignore: TODO
Referer: url } })
: client(undefined);
}, [isServerRendered, url]);

return (
<PaginationContextProvider defaultRef={resultsRef}>
<Seo seoAndSome={seoAndSome} slug={slug} pageTitle={title} />
<main ref={ref} className="">
<InstantSearch
searchClient={searchClient}
future={{ preserveSharedStateOnUnmount: false }}
indexName={indexName}
routing={routing}
>
<InstantSearch
searchClient={searchClient}
future={{ preserveSharedStateOnUnmount: false }}
indexName={indexName}
routing={routing}>
<Configure
facetingAfterDistinct
maxFacetHits={50}
Expand Down
80 changes: 80 additions & 0 deletions web/templates/newsroom/sanity/NewsHeadlinerSanity.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { FormattedDate } from '@components/FormattedDateTime'
import { forwardRef, HTMLAttributes } from 'react'
import { BaseLink } from '@core/Link'
import { Typography } from '@core/Typography'
import Image, { Ratios } from '../../../pageComponents/shared/SanityImage'
import envisTwMerge from '../../../twMerge'
import { NewsRoomNewsItem } from '../../../types/algoliaIndexPage'
import { SanityImageObject } from '@sanity/image-url/lib/types/types'
import Blocks from '../../../pageComponents/shared/portableText/Blocks'

export type NewsHeadlinerProps = {
data: NewsRoomNewsItem
fallbackImage?: SanityImageObject
} & HTMLAttributes<HTMLLIElement>

const NewsHeadlinerSanity = forwardRef<HTMLLIElement, NewsHeadlinerProps>(function NewsHeadlinerSanity(
{ data, fallbackImage, className = '', ...rest },
ref,
) {
const { slug, title, ingress, publishDateTime, heroImage, tags, countryTags } = data

return (
<section ref={ref} {...rest} className={envisTwMerge('', className)}>
<BaseLink href={slug} className="group flex flex-col gap-2 pb-6">
{(heroImage?.image?.asset || fallbackImage) && (
<div className="max-h-[212px] aspect-video md:aspect-video relative md:max-h-[324px] mb-2">
<Image
//@ts-ignore: TODO Fix SanityImage to take SanityImageObject
image={heroImage?.image?.asset ? heroImage?.image : fallbackImage}
fill
priority
aspectRatio={Ratios.NINE_TO_SIXTEEN}
sizes="(max-width: 800px) 100vw, 1440px"
className="rounded-xs"
aria-hidden
/>
</div>
)}
{publishDateTime && (
<FormattedDate datetime={publishDateTime} uppercase className="pt-4 text-2xs font-normal leading-normal" />
)}
{title && (
<Typography as="h2" variant="lg" className="pb-6 group-hover:underline">
{title}
</Typography>
)}
<div className="pb-2 flex flex-wrap gap-y-4 text-xs divide-x-2 divide-energy-red-100">
{tags?.map((tag: any, i: number) => {
return (
<span
key={tag.label}
className=" text-xs inline-block text-grey-70 pl-3 pr-3 first:pl-0 whitespace-nowrap"
>
{tag.label}
{i < tags.length - 1 && <span className="sr-only">,</span>}
</span>
)
})}
{countryTags?.length > 0 && <span className="sr-only">,</span>}
{countryTags?.map((country: any, i: number) => {
return (
<span key={country.label} className=" inline-block text-grey-70 pl-3 pr-3 first:pl-0 whitespace-nowrap">
{country.label}
{i < countryTags.length - 1 && <span className="sr-only">,</span>}
</span>
)
})}
</div>
{Array.isArray(ingress) ? (
<Blocks value={ingress} className="text-sm max-w-prose" />
) : (
<Typography variant="body" className="text-sm max-w-prose`">
{ingress}
</Typography>
)}
</BaseLink>
</section>
)
})
export default NewsHeadlinerSanity
Loading

0 comments on commit 79e16c1

Please sign in to comment.