feat: dynamic sitemap and robots.txt at app root

This commit is contained in:
Jeffrey 2026-02-18 12:27:55 +01:00
parent c3b3868288
commit f5857f1298
4 changed files with 92 additions and 16 deletions

View File

@ -1,11 +0,0 @@
# *
User-agent: *
Disallow: /admin/*
# Host
Host: http://localhost:3000
# Sitemaps
Sitemap: http://localhost:3000/sitemap.xml
Sitemap: http://localhost:3000/pages-sitemap.xml
Sitemap: http://localhost:3000/posts-sitemap.xml

View File

@ -1,5 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap><loc>http://localhost:3000/pages-sitemap.xml</loc></sitemap>
<sitemap><loc>http://localhost:3000/posts-sitemap.xml</loc></sitemap>
</sitemapindex>

22
src/app/robots.ts Normal file
View File

@ -0,0 +1,22 @@
import type { MetadataRoute } from 'next'
export default function robots(): MetadataRoute.Robots {
const siteUrl = process.env.NEXT_PUBLIC_SERVER_URL || 'https://fiberdirekt.se'
return {
rules: [
{
userAgent: '*',
allow: '/',
disallow: [
'/admin',
'/admin/',
'/api/',
'/_next/',
],
},
],
sitemap: `${siteUrl}/sitemap.xml`,
host: siteUrl,
}
}

70
src/app/sitemap.ts Normal file
View File

@ -0,0 +1,70 @@
import type { MetadataRoute } from 'next'
import { getPayload } from 'payload'
import config from '@payload-config'
export const dynamic = 'force-dynamic'
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
const payload = await getPayload({ config })
const siteUrl = process.env.NEXT_PUBLIC_SERVER_URL || 'https://fiberdirekt.se'
// ── Fetch all published pages ──────────────────────────────────────────
const pages = await payload.find({
collection: 'pages',
draft: false,
limit: 1000,
overrideAccess: false,
select: {
slug: true,
updatedAt: true,
},
})
// ── Fetch all published posts ──────────────────────────────────────────
const posts = await payload.find({
collection: 'posts',
draft: false,
limit: 1000,
overrideAccess: false,
select: {
slug: true,
updatedAt: true,
},
})
// ── Static routes ──────────────────────────────────────────────────────
const staticRoutes: MetadataRoute.Sitemap = [
{
url: siteUrl,
lastModified: new Date(),
changeFrequency: 'weekly',
priority: 1.0,
},
{
url: `${siteUrl}/posts`,
lastModified: new Date(),
changeFrequency: 'weekly',
priority: 0.8,
},
]
// ── Dynamic page routes ────────────────────────────────────────────────
const pageRoutes: MetadataRoute.Sitemap = pages.docs
.filter((page) => page.slug !== 'home') // home is already the root URL
.map((page) => ({
url: `${siteUrl}/${page.slug}`,
lastModified: new Date(page.updatedAt),
changeFrequency: 'monthly' as const,
priority: 0.8,
}))
// ── Dynamic post routes ────────────────────────────────────────────────
const postRoutes: MetadataRoute.Sitemap = posts.docs.map((post) => ({
url: `${siteUrl}/posts/${post.slug}`,
lastModified: new Date(post.updatedAt),
changeFrequency: 'monthly' as const,
priority: 0.6,
}))
return [...staticRoutes, ...pageRoutes, ...postRoutes]
}