Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reapply "✨ Take back algolia #2657" #2718

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions web/pages/api/revalidate.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ export default async function handler(req, res) {
})
}

const revalidateNewsroomPages = async () => {
console.log(new Date(), 'Revalidating: /news')
res.revalidate(`/news`)
console.log(new Date(), 'Revalidating: /no/nyheter')
res.revalidate('/no/nyheter')
}

if (['page', 'landingPage', 'event'].includes(data._type)) {
const routes = await sanityClient.fetch(
groq`*[_type match "route_*" && content._ref == $id]{"slug": slug.current}`,
Expand All @@ -53,6 +60,8 @@ export default async function handler(req, res) {
console.log(new Date(), 'Revalidating: ', data?.slug)
if (data.slug) await res.revalidate(data.slug)
await revalidateHomePages()
// revalidate newsroom pages
if (data._type === 'news') await revalidateNewsroomPages()
return res.json({ revalidated: true, slug: data.slug })
}
} catch (err) {
Expand Down
48 changes: 21 additions & 27 deletions web/pages/news/index.global.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
import { GetServerSideProps } from 'next'
import { GetStaticProps } 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 { allNewsDocuments, newsroomQuery } from '../../lib/queries/newsroom'
import { renderToString } from 'react-dom/server'
import { 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, getData } from '../../lib/fetchData'
import NewsRoomTemplateSanity from '@templates/newsroom/sanity/NewsroomSanity'
import { getComponentsData } from '../../lib/fetchData'
import NewsRoomTemplate from '@templates/newsroom/Newsroom'
import { getServerState, InstantSearchSSRProvider } from 'react-instantsearch'

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

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

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

export const getServerSideProps: GetServerSideProps = async ({ req, preview = false, locale = 'en' }) => {
export const getStaticProps: GetStaticProps = async ({ 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 @@ -75,8 +79,6 @@ export const getServerSideProps: GetServerSideProps = async ({ req, preview = fa
lang,
}

const slug = req.url

const { menuData, pageData, footerData } = await getComponentsData(
{
query: newsroomQuery,
Expand All @@ -85,26 +87,18 @@ export const getServerSideProps: GetServerSideProps = async ({ req, preview = fa
preview,
)

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,
const serverState = await getServerState(<NewsRoom data={{ menuData, pageData, footerData, intl }} />, {
renderToString,
})

return {
props: {
url,
data: {
menuData,
footerData,
intl,
pageData: {
...pageData,
newsArticles: data,
},
slug,
pageData,
},
serverState,
},
}
}
55 changes: 26 additions & 29 deletions web/pages/nyheter/index.global.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
import { GetServerSideProps } from 'next'
import { GetStaticProps } 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 { allNewsDocuments, newsroomQuery } from '../../lib/queries/newsroom'
import { renderToString } from 'react-dom/server'
import { 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, getData } from '../../lib/fetchData'
import NewsRoomTemplateSanity from '@templates/newsroom/sanity/NewsroomSanity'
import { getComponentsData } from '../../lib/fetchData'
import NewsRoomTemplate from '@templates/newsroom/Newsroom'
import { getServerState, InstantSearchSSRProvider } from 'react-instantsearch'

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

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

Expand All @@ -32,6 +36,8 @@ 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 @@ -54,10 +60,10 @@ NorwegianNewsRoom.getLayout = (page: AppProps) => {
)
}

export const getServerSideProps: GetServerSideProps = async ({ req, preview = false, locale = 'no' }) => {
export const getStaticProps: GetStaticProps = async ({ 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 Norwegian.
// This is a hack, and we should improve this at some point
// We will also return 404 if the locale is not English.
// This is a hack and and we should improve this at some point
// See https://github.com/vercel/next.js/discussions/18485

if (locale !== 'no') {
Expand All @@ -69,39 +75,30 @@ export const getServerSideProps: GetServerSideProps = async ({ req, preview = fa
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 { data } = await getData({
query: allNewsDocuments,
queryParams,
})

const serverState = await getServerState(<NorwegianNewsRoom data={{ menuData, footerData, pageData, intl }} />, {
renderToString,
})
return {
props: {
locale,
url,
data: {
menuData,
footerData,
intl,
pageData: {
...pageData,
newsArticles: data,
},
slug,
pageData,
},
serverState,
},
}
}
28 changes: 10 additions & 18 deletions web/templates/newsroom/Newsroom.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { forwardRef, useMemo, useRef } from 'react'
import { forwardRef, useRef } from 'react'
import singletonRouter from 'next/router'
import Blocks from '../../pageComponents/shared/portableText/Blocks'
import type { NewsRoomPageType } from '../../types'
Expand All @@ -20,15 +20,13 @@ 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(
{ isServerRendered, locale, pageData, slug, url },
{ locale, pageData, slug },
ref,
) {
const { ingress, title, seoAndSome, subscriptionLink, subscriptionLinkTitle, localNewsPages, fallbackImages } =
Expand Down Expand Up @@ -89,7 +87,7 @@ const NewsRoomTemplate = forwardRef<HTMLElement, NewsRoomTemplateProps>(function
const routing = {
router: createInstantSearchRouterNext({
singletonRouter,
serverUrl: url,
serverUrl: `https://www.equinor.com${isoCode === 'nb-NO' ? '/no/nyheter' : '/news'}`, // temporary fix for url to be available during build time
routerOptions: {
createURL: createURL,
parseURL: parseURL,
Expand Down Expand Up @@ -130,23 +128,17 @@ const NewsRoomTemplate = forwardRef<HTMLElement, NewsRoomTemplateProps>(function
},
}

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

const searchClient = client()
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: 0 additions & 80 deletions web/templates/newsroom/sanity/NewsHeadlinerSanity.tsx

This file was deleted.

Loading
Loading