Skip to content

Commit

Permalink
fix: handle non existing page in pages collection
Browse files Browse the repository at this point in the history
  • Loading branch information
babslgam committed Dec 20, 2024
1 parent e8a3f56 commit 296a197
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions app/(app)/[locale]/(index)/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Metadata, ResolvingMetadata } from "next";
import { setRequestLocale } from "next-intl/server";
import { notFound } from "next/navigation";
import { getTranslations, setRequestLocale } from "next-intl/server";
import type { ReactNode } from "react";

import { MainContent } from "@/components/main-content";
Expand Down Expand Up @@ -40,11 +41,15 @@ export async function generateMetadata(
const { locale } = params;
const id = decodeURIComponent(params.id);

const page = await createCollectionResource("pages", locale).read(id);
const metadata: Metadata = {};

const metadata: Metadata = {
title: page.data.title,
};
try {
const page = await createCollectionResource("pages", locale).read(id);
metadata.title = page.data.title;
} catch {
const t = await getTranslations({ locale, namespace: "NotFoundPage" });
metadata.title = t("meta.title");
}

return metadata;
}
Expand All @@ -57,17 +62,22 @@ export default async function ContentPage(props: Readonly<ContentPageProps>): Pr

setRequestLocale(locale);

const page = await createCollectionResource("pages", locale).read(id);
const { default: Content } = await page.compile(page.data.content);

return (
<MainContent className="layout-grid content-start">
<section className="layout-subgrid relative py-16 xs:py-24">
<h1 className="text-balance font-heading text-heading-1 font-strong text-text-strong">
{page.data.title}
</h1>
<Content />
</section>
</MainContent>
);
try {
const page = await createCollectionResource("pages", locale).read(id);

const { default: Content } = await page.compile(page.data.content);

return (
<MainContent className="layout-grid content-start">
<section className="layout-subgrid relative py-16 xs:py-24">
<h1 className="text-balance font-heading text-heading-1 font-strong text-text-strong">
{page.data.title}
</h1>
<Content />
</section>
</MainContent>
);
} catch {
notFound();
}
}

0 comments on commit 296a197

Please sign in to comment.