Skip to content

Commit

Permalink
feat: sitemap from app directory
Browse files Browse the repository at this point in the history
  • Loading branch information
mbifulco committed Nov 23, 2024
1 parent 692e67f commit a268036
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 49 deletions.
7 changes: 0 additions & 7 deletions next-sitemap.config.js

This file was deleted.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"packageManager": "[email protected]",
"scripts": {
"analyze": "ANALYZE=true next build",
"build": "next build && next-sitemap",
"postbuild": "next-sitemap",
"build": "next build",
"dev": "NODE_OPTIONS='--inspect' next dev --turbo",
"email": "email dev --dir ./src/utils/email/templates",
"format": "prettier --write \"**/*.{js,jsx,ts,tsx,json,md}\"",
Expand Down Expand Up @@ -99,7 +98,6 @@
"eslint-plugin-react-hooks": "^5.0.0",
"github-slugger": "^2.0.0",
"jiti": "^2.4.0",
"next-sitemap": "^4.2.3",
"prettier": "^3.3.3",
"prettier-plugin-tailwindcss": "^0.6.9",
"schema-dts": "^1.1.2",
Expand Down
48 changes: 10 additions & 38 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions src/app/robots.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { MetadataRoute } from 'next';

import { BASE_SITE_URL } from '@/config';

const noIndexPaths = [
'/ingest', // posthog's reverse proxy
'/ingest/*', // posthog's reverse proxy
];

export default function robots(): MetadataRoute.Robots {
return {
rules: [
{
userAgent: '*',
disallow: '/api/',
},
{
userAgent: '*',
disallow: '/_next/',
},
{
userAgent: '*',
disallow: '/public/',
},
...noIndexPaths.map((path) => ({
userAgent: '*',
disallow: path,
})),
],
sitemap: `${BASE_SITE_URL}/sitemap.xml`,
};
}
63 changes: 63 additions & 0 deletions src/app/sitemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import type { MetadataRoute } from 'next';
import { getAllPosts } from '@lib/blog';
import { getAllNewsletters } from '@lib/newsletters';
import { getAllTags } from '@lib/tags';

import { BASE_SITE_URL } from '@/config';

export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
// Define your base URL
const baseUrl = BASE_SITE_URL;

const newsletters = await getAllNewsletters();
const newslettersSitemap = newsletters.map((newsletter) => ({
url: `${baseUrl}/newsletter/${newsletter.slug}`,
lastModified: new Date(newsletter.frontmatter.date),
changeFrequency: 'weekly' as const,
priority: 0.8,
}));

const posts = await getAllPosts();
const postsSitemap = posts.map((post) => ({
url: `${baseUrl}/blog/${post.slug}`,
lastModified: new Date(post.frontmatter.date),
changeFrequency: 'weekly' as const,
priority: 0.8,
}));

const { allTags } = await getAllTags();
const tagsSitemap = allTags.map((tag) => ({
url: `${baseUrl}/tags/${tag}`,
lastModified: new Date(),
changeFrequency: 'weekly' as const,
priority: 0.5,
}));

// Define your static routes
const routes: string[] = [
'', // home page
'/about',
'/integrity',
'/newsletter',
'/podcast',
'/posts',
'/tags',
'/work',
'/shop',
];

// Create sitemap entries for static routes
const staticRoutesSitemap = routes.map((route) => ({
url: `${baseUrl}${route}`,
lastModified: new Date(),
changeFrequency: 'weekly' as const,
priority: route === '' ? 1 : 0.8,
}));

return [
...staticRoutesSitemap,
...newslettersSitemap,
...postsSitemap,
...tagsSitemap,
];
}
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const BASE_SITE_URL = 'https://mikebifulco.com';
export const BASE_SITE_URL = 'https://mikebifulco.com';

const config = {
employer: {
Expand Down

0 comments on commit a268036

Please sign in to comment.