sitemap.xml

sitemap.(xml|js|ts) arama motoru tarayıcılarının sitenizi daha verimli bir şekilde indekslemesine yardımcı olmak için Site Haritaları XML formatıyla eşleşen özel bir dosyadır.

Sitemap files (.xml)

Daha küçük uygulamalar için bir sitemap.xml dosyası oluşturabilir ve bunu app dizininizin kök dizinine yerleştirebilirsiniz.

app/sitemap.xml
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>https://acme.com</loc>
    <lastmod>2023-04-06T15:02:24.021Z</lastmod>
    <changefreq>yearly</changefreq>
    <priority>1</priority>
  </url>
  <url>
    <loc>https://acme.com/about</loc>
    <lastmod>2023-04-06T15:02:24.021Z</lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.8</priority>
  </url>
  <url>
    <loc>https://acme.com/blog</loc>
    <lastmod>2023-04-06T15:02:24.021Z</lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.5</priority>
  </url>
</urlset>

Generating a sitemap using code (.js, .ts)

URL dizisi döndüren varsayılan bir işlevi dışa aktararak programlı olarak bir site haritası oluşturmak için sitemap.(js|ts) dosya kuralını kullanabilirsiniz. TypeScript kullanıyorsanız, bir Sitemap tipi mevcuttur.

app/sitemap.ts
import { MetadataRoute } from 'next'
 
export default function sitemap(): MetadataRoute.Sitemap {
  return [
    {
      url: 'https://acme.com',
      lastModified: new Date(),
      changeFrequency: 'yearly',
      priority: 1,
    },
    {
      url: 'https://acme.com/about',
      lastModified: new Date(),
      changeFrequency: 'monthly',
      priority: 0.8,
    },
    {
      url: 'https://acme.com/blog',
      lastModified: new Date(),
      changeFrequency: 'weekly',
      priority: 0.5,
    },
  ]
}

Çıktı:

acme.com/sitemap.xml
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>https://acme.com</loc>
    <lastmod>2023-04-06T15:02:24.021Z</lastmod>
    <changefreq>yearly</changefreq>
    <priority>1</priority>
  </url>
  <url>
    <loc>https://acme.com/about</loc>
    <lastmod>2023-04-06T15:02:24.021Z</lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.8</priority>
  </url>
  <url>
    <loc>https://acme.com/blog</loc>
    <lastmod>2023-04-06T15:02:24.021Z</lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.5</priority>
  </url>
</urlset>

Generating multiple sitemaps

Tek bir site haritası çoğu uygulama için işe yarayacaktır. Büyük web uygulamaları için bir site haritasını birden fazla dosyaya bölmeniz gerekebilir.

Birden fazla site haritası oluşturmanın iki yolu vardır:

Örneğin, generateSitemaps kullanarak bir site haritasını bölmek için, id site haritası ile bir dizi nesne döndürün. Ardından, benzersiz site haritalarını oluşturmak için id adresini kullanın.

app/product/sitemap.ts
import { BASE_URL } from '@/app/lib/constants'
 
export async function generateSitemaps() {
  // Fetch the total number of products and calculate the number of sitemaps needed
  return [{ id: 0 }, { id: 1 }, { id: 2 }, { id: 3 }]
}
 
export default async function sitemap({
  id,
}: {
  id: number
}): Promise<MetadataRoute.Sitemap> {
  // Google's limit is 50,000 URLs per sitemap
  const start = id * 50000
  const end = start + 50000
  const products = await getProducts(
    `SELECT id, date FROM products WHERE id BETWEEN ${start} AND ${end}`
  )
  return products.map((product) => ({
    url: `${BASE_URL}/product/${id}`,
    lastModified: product.date,
  }))
}

Üretimde, oluşturulan site haritalarınız /.../sitemap/[id].xml adresinde mevcut olacaktır. Örneğin, /product/sitemap/1.xml.

Geliştirme aşamasında, oluşturulan site haritasını /.../sitemap.xml/[id] adresinde görüntüleyebilirsiniz. Örneğin, /product/sitemap.xml/1. Bu farklılık geçicidir ve üretim formatını takip edecektir.

Daha fazla bilgi için generateSitemaps API referansına bakın.

Returns

sitemap.(xml|ts|js) adresinden dışa aktarılan varsayılan işlev, aşağıdaki özelliklere sahip bir nesne dizisi döndürmelidir:

type Sitemap = Array<{
  url: string
  lastModified?: string | Date
  changeFrequency?:
    | 'always'
    | 'hourly'
    | 'daily'
    | 'weekly'
    | 'monthly'
    | 'yearly'
    | 'never'
  priority?: number
}>

Version History

VersionChanges
v13.4.5Add changeFrequency and priority attributes to sitemaps.
v13.3.0sitemap introduced.