Skip to content

Commit f9262dc

Browse files
committed
feat: try/catch metadata generation
1 parent 6f1f644 commit f9262dc

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

app/[locale]/[...slug]/page.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,15 @@ export async function generateMetadata({
124124
params: Promise<SlugPageParams>
125125
}) {
126126
const { locale, slug } = await params
127-
const t = await getTranslations({ locale, namespace: "common" })
128127

129128
try {
130129
return await getMdMetadata({
131130
locale,
132131
slug,
133132
})
134133
} catch (error) {
134+
const t = await getTranslations({ locale, namespace: "common" })
135+
135136
// Return basic metadata for invalid paths
136137
return {
137138
title: t("page-not-found"),

app/[locale]/page.tsx

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -147,23 +147,26 @@ export async function generateMetadata({
147147
}) {
148148
const { locale } = await params
149149

150-
// If invalid locale, return not-found metadata title
151-
if (!LOCALES_CODES.includes(locale)) {
150+
try {
151+
const t = await getTranslations({ locale, namespace: "page-index" })
152+
return await getMetadata({
153+
locale,
154+
slug: [""],
155+
title: t("page-index-meta-title"),
156+
description: t("page-index-meta-description"),
157+
})
158+
} catch (error) {
152159
const t = await getTranslations({
153160
locale: DEFAULT_LOCALE,
154161
namespace: "common",
155162
})
156-
return { title: t("we-couldnt-find-that-page") }
157-
}
158-
159-
const t = await getTranslations({ locale, namespace: "page-index" })
160163

161-
return await getMetadata({
162-
locale,
163-
slug: [""],
164-
title: t("page-index-meta-title"),
165-
description: t("page-index-meta-description"),
166-
})
164+
// Return basic metadata for invalid paths
165+
return {
166+
title: t("page-not-found"),
167+
description: t("page-not-found-description"),
168+
}
169+
}
167170
}
168171

169172
export default Page

0 commit comments

Comments
 (0)