-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #295 from gagahpangeran/generate-static-routes
Generate static page for all blog routes
- Loading branch information
Showing
12 changed files
with
364 additions
and
286 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright (c) Gagah Pangeran Rosfatiputra (GPR) <[email protected]>. | ||
// Licensed under The MIT License. | ||
// Read the LICENSE file in the repository root for full license text. | ||
|
||
import { type Metadata } from "next"; | ||
import { notFound } from "next/navigation"; | ||
import Blog from "@/templates/Blog"; | ||
import Post from "@/templates/Post"; | ||
import { | ||
getOtherMetadata, | ||
getPageMetadata, | ||
notFoundMetadata | ||
} from "@/utils/metadata"; | ||
import { getAllBlogPageRoutes, getBlogPageDataBySlug } from "@/utils/page"; | ||
|
||
interface Props { | ||
params: Promise<{ slug?: string[] }>; | ||
} | ||
|
||
export const dynamicParams = false; | ||
|
||
export async function generateStaticParams() { | ||
const allRoutes = getAllBlogPageRoutes(); | ||
const allSlugs = allRoutes.map(route => { | ||
const [_, ...slug] = route.split("/").filter(Boolean); | ||
return { slug }; | ||
}); | ||
return allSlugs; | ||
} | ||
|
||
export async function generateMetadata({ params }: Props): Promise<Metadata> { | ||
const { slug } = await params; | ||
const data = getBlogPageDataBySlug(slug); | ||
|
||
if (data == null) { | ||
return notFoundMetadata; | ||
} | ||
|
||
if (data.kind === "post") { | ||
const { title, description, image } = data.post; | ||
return { | ||
title, | ||
description, | ||
...getOtherMetadata(title, description, image) | ||
}; | ||
} | ||
|
||
const { type, filterValue } = data; | ||
const metadata = getPageMetadata({ type, filterValue }); | ||
|
||
if (metadata == null) { | ||
return notFoundMetadata; | ||
} | ||
|
||
return metadata; | ||
} | ||
|
||
export default async function BlogPage({ params }: Props) { | ||
const { slug } = await params; | ||
const data = getBlogPageDataBySlug(slug); | ||
|
||
if (data == null) { | ||
notFound(); | ||
} | ||
|
||
if (data.kind === "post") { | ||
return <Post post={data.post} />; | ||
} | ||
|
||
const { type, filterValue } = data; | ||
|
||
const metadata = getPageMetadata({ type, filterValue }); | ||
if (metadata == null) { | ||
notFound(); | ||
} | ||
|
||
return <Blog {...metadata} {...data} />; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.