Parçacıklara Dön
Next.jsTypeScript

Next.js Sitemap Oluşturucu

Statik ve dinamik route'lardan otomatik sitemap.xml oluşturma.

nextjssitemapseoxml
import type { MetadataRoute } from class=class="text-emerald-400">"text-emerald-400">'next'

const BASE_URL = class=class="text-emerald-400">"text-emerald-400">'https:class=class="text-emerald-400">"text-gray">//example.com'

export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
  const staticPages = [class=class="text-emerald-400">"text-emerald-400">'', class=class="text-emerald-400">"text-emerald-400">'/about', class=class="text-emerald-400">"text-emerald-400">'/contact', class=class="text-emerald-400">"text-emerald-400">'/blog']

  const staticEntries = staticPages.map(page => ({
    url: class="text-emerald-400">`${BASE_URL}${page}`,
    lastModified: new Date(),
    changeFrequency: class=class="text-emerald-400">"text-emerald-400">'monthly' as const,
    priority: page === class=class="text-emerald-400">"text-emerald-400">'' ? 1.0 : 0.8,
  }))

  class=class="text-emerald-400">"text-gray">// Fetch dynamic routes
  const res = await fetch(class="text-emerald-400">`${BASE_URL}/api/posts`, {
    next: { revalidate: 3600 },
  })
  const posts = res.ok ? await res.json() : []

  const postEntries = posts.map((post: { slug: string; updatedAt: string }) => ({
    url: class="text-emerald-400">`${BASE_URL}/blog/${post.slug}`,
    lastModified: new Date(post.updatedAt),
    changeFrequency: class=class="text-emerald-400">"text-emerald-400">'weekly' as const,
    priority: 0.6,
  }))

  return [...staticEntries, ...postEntries]
}

Nasıl Kullanılır

app/sitemap.ts olarak kaydedin. Next.js bunu otomatik olarak /sitemap.xml adresinde sunar. Tüm statik sayfaları ekleyin ve slug'ları API veya veritabanınızdan dinamik olarak çekin. Google sayfalarınızı keşfedip indeksleyecektir.

İlgili Teknoloji

Next.js

Aklınızda Bir Proje mi Var?

Fikrinizi nasıl hayata geçirebileceğimizi konuşalım. İlk konseptten üretime hazır ürüne kadar — yanınızdayız.

veya ücretsiz görüşme ayarlayın