mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2026-10-16 13:35:45 -04:00
fix: exclude route groups from sitemap
This commit is contained in:
1 file changed
+12
-18
+12
-18
@@ -5,22 +5,12 @@ import type { MetadataRoute } from "next";
|
|||||||
|
|
||||||
import { getFrontMatter } from "@/lib/posts";
|
import { getFrontMatter } from "@/lib/posts";
|
||||||
|
|
||||||
// routes in /app (in other words, directories containing a page.tsx/mdx file) are automatically included; add a route
|
|
||||||
// here to exclude it.
|
|
||||||
const excludedRoutes = [
|
|
||||||
// homepage is already included manually
|
|
||||||
"./(home)",
|
|
||||||
// other excluded pages
|
|
||||||
// "./license",
|
|
||||||
// "./privacy",
|
|
||||||
];
|
|
||||||
|
|
||||||
const sitemap = async (): Promise<MetadataRoute.Sitemap> => {
|
const sitemap = async (): Promise<MetadataRoute.Sitemap> => {
|
||||||
// start with manual routes
|
// start with manual routes
|
||||||
const routes: MetadataRoute.Sitemap = [
|
const routes: MetadataRoute.Sitemap = [
|
||||||
{
|
{
|
||||||
// homepage
|
// homepage
|
||||||
url: process.env.NEXT_PUBLIC_BASE_URL!,
|
url: process.env.NEXT_PUBLIC_BASE_URL ?? "/",
|
||||||
priority: 1.0,
|
priority: 1.0,
|
||||||
lastModified: new Date(),
|
lastModified: new Date(),
|
||||||
},
|
},
|
||||||
@@ -33,9 +23,8 @@ const sitemap = async (): Promise<MetadataRoute.Sitemap> => {
|
|||||||
glob("**/page.{tsx,mdx}", {
|
glob("**/page.{tsx,mdx}", {
|
||||||
cwd: path.join(process.cwd(), "app"),
|
cwd: path.join(process.cwd(), "app"),
|
||||||
ignore: [
|
ignore: [
|
||||||
...excludedRoutes.map((route) => `${route}/page.{tsx,mdx}`),
|
// don't include dynamic routes or route groups
|
||||||
// don't include dynamic routes
|
"**/{\\[*\\],\\(*\\)}/page.{tsx,mdx}",
|
||||||
"**/\\[*\\]/page.{tsx,mdx}",
|
|
||||||
],
|
],
|
||||||
}),
|
}),
|
||||||
|
|
||||||
@@ -43,18 +32,23 @@ const sitemap = async (): Promise<MetadataRoute.Sitemap> => {
|
|||||||
getFrontMatter(),
|
getFrontMatter(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// add each directory in the app folder as a route (excluding special routes)
|
// normalize static routes and blog slugs to be absolute URLs
|
||||||
staticRoutes.forEach((route) => {
|
staticRoutes.forEach((route) => {
|
||||||
|
const routePath = route
|
||||||
|
.replace(/(^|\/)page\.(tsx|mdx)$/, "")
|
||||||
|
.split("/")
|
||||||
|
.filter((segment) => !/^\(.+\)$/.test(segment))
|
||||||
|
.join("/");
|
||||||
|
|
||||||
routes.push({
|
routes.push({
|
||||||
// remove matching page.(tsx|mdx) file and make all URLs absolute
|
url: `${process.env.NEXT_PUBLIC_BASE_URL}/${routePath}`,
|
||||||
url: `${process.env.NEXT_PUBLIC_BASE_URL}/${route.replace(/\/page\.(tsx|mdx)$/, "")}`,
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// enrich blog entries with frontmatter dates
|
||||||
frontmatter.forEach((post) => {
|
frontmatter.forEach((post) => {
|
||||||
routes.push({
|
routes.push({
|
||||||
url: post.permalink,
|
url: post.permalink,
|
||||||
// pull lastModified from front matter date
|
|
||||||
lastModified: new Date(post.date),
|
lastModified: new Date(post.date),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in new issue
Block a user