Skip to content

Commit 2d6bc78

Browse files
authored
Fix archived news not falling back to existing page #3869 (#3871)
1 parent 3934f6c commit 2d6bc78

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

  • web/app/[locale]/(pages)/news/archive/[...slug]

web/app/[locale]/(pages)/news/archive/[...slug]/page.tsx

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Metadata } from 'next'
2-
import { notFound } from 'next/navigation'
2+
import { notFound, redirect } from 'next/navigation'
33
import { defaultLanguage, languages, metaTitleSuffix } from '@/languageConfig'
44
import archivedNews from '@/lib/archive/archivedNewsPaths.json'
55
import { host } from '@/lib/config'
@@ -13,7 +13,12 @@ import Header from '@/sections/Header/Header'
1313
import ArchivedNews from '@/templates/archivedNews/ArchivedNews'
1414

1515
type Params = Promise<{ locale: string; slug: string[] }>
16-
//TODO types
16+
17+
type ArchivedContentType = {
18+
title: string
19+
description: string
20+
content: string
21+
}
1722
async function getArchivedPageData(params: { locale: string; slug: string[] }) {
1823
const { locale: routeLocale, slug: pagePathArray } = params
1924
const locale = routeLocale === 'en-GB' ? 'en' : 'no'
@@ -24,13 +29,16 @@ async function getArchivedPageData(params: { locale: string; slug: string[] }) {
2429
e => e.slug === `/news/archive/${pagePath}`,
2530
)
2631
if (archivedItems.length === 0) return notFound()
27-
28-
const response = await fetchArchiveData(pagePathArray, pagePath, locale)
29-
30-
if (response.status === 404)
32+
if (archivedItems.length === 1 && archivedItems[0].locale !== locale) {
33+
// fallback to another language if the requested locale does not exist for the archived page
3134
return fallbackToAnotherLanguage(pagePathArray, pagePath, locale)
35+
}
3236

33-
const pageData = await parseResponse(response)
37+
const response = await fetchArchiveData(pagePathArray, pagePath, locale)
38+
if (response.status === 404) {
39+
notFound()
40+
}
41+
const pageData: ArchivedContentType = await parseResponse(response)
3442
return pageData
3543
}
3644

@@ -52,7 +60,7 @@ export async function generateMetadata({
5260
const fullUrl = `${host.url}${slugs.find(it => it.lang === (locale === 'en' ? 'en_GB' : 'nb_NO'))?.slug}`
5361

5462
const pageData = await getArchivedPageData({ locale, slug: pagePathArray })
55-
if (!pageData) {
63+
if ('redirect' in pageData || 'notFound' in pageData) {
5664
return {
5765
title: metaTitleSuffix,
5866
openGraph: {
@@ -233,7 +241,10 @@ export default async function ArchivedNewsPage({ params }: { params: Params }) {
233241

234242
const pageData = await getArchivedPageData(await params)
235243

236-
if (!pageData) notFound()
244+
if (!pageData || 'notFound' in pageData) notFound()
245+
if ('redirect' in pageData) {
246+
redirect(pageData.redirect.destination, 'replace')
247+
}
237248
return (
238249
<>
239250
<Header siteMenuData={siteMenuData} headerData={headerData} />

0 commit comments

Comments
 (0)