๐Ÿ” SEO๋ฅผ ์œ„ํ•œ ๋„๊ตฌ

robots.txt ๋Š” ๊ฒ€์ƒ‰์—”์ง„ ํฌ๋กค๋Ÿฌ๊ฐ€ ์›น ์‚ฌ์ดํŠธ๋ฅผ ํฌ๋กค๋งํ•˜๋ฉด์„œ ์ •๋ณด๋ฅผ ์ˆ˜์งํ• ๋•Œ ์—‘์„ธ์Šคํ•˜๊ฑฐ๋‚˜ ์ •๋ณด ์ˆ˜์ง‘์„ ํ•ด๋„ ๋˜๋Š” ํŽ˜์ด์ง€๊ฐ€ ๋ฌด์—‡์ธ์ง€๋ฅผ ์•Œ๋ ค์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.
์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•œ๋‹ค.

User-agent: *
Allow: /

Sitemap: https://hjk329.github.io/sitemap.xml

User-agent ํ•„๋“œ๋Š” ๊ทœ์น™์ด ์ ์šฉ๋˜๋Š” ํฌ๋กค๋Ÿฌ๋ฅผ ์‹๋ณ„ํ•œ๋‹ค.
Allow ํ•„๋“œ๋Š” ํฌ๋กค๋ง์ด ํ—ˆ์šฉ๋˜๋Š” URL์„ ์˜๋ฏธํ•œ๋‹ค.
Disallow ํ•„๋“œ๋ฅผ ์„ค์ •ํ•ด์„œ ์ ‘๊ทผํ•˜๊ฑฐ๋‚˜ ์ •๋ณด ์ˆ˜์ง‘์ด ํ—ˆ์šฉ๋˜์ง€ ์•Š๋Š” ํŽ˜์ด์ง€๋ฅผ ํ‘œ์‹œํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

Sitemap ํ•„๋“œ๋Š” ๋„๋ฉ”์ธ ๋‚ด์˜ ํŽ˜์ด์ง€ ๋ชฉ๋ก์„ ์˜๋ฏธํ•œ๋‹ค.

Next.js์—์„œ robots.txt์™€ sitemap์„ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•ด์ฃผ๋Š” ํŒจํ‚ค์ง€๊ฐ€ ์žˆ๋‹ค! โœจ

next-sitemap

๊ฐœ๋ฐœ ํ™˜๊ฒฝ๋ณ„๋กœ config๋ฅผ ์„ค์ •ํ•  ์ˆ˜๋„ ์žˆ๊ณ , ๋™์ ์ธ ํŽ˜์ด์ง€์— ๋Œ€ํ•ด์„œ๋„ ์‚ฌ์ดํŠธ๋งต์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค.

๊ณต์‹ ๋ฌธ์„œ๋ฅผ ๋”ฐ๋ผํ•ด๋ณด๋ฉด config๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค์ •ํ•œ๋‹ค.

/** @type {import('next-sitemap').IConfig} */
module.exports = {
  siteUrl: process.env.SITE_URL || 'https://example.com',
  generateRobotsTxt: true, // (optional)
  // ...other options
}

๊ทธ๋ฆฌ๊ณ  package.json์— ์•„๋ž˜์™€ ๊ฐ™์ด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

{
  "build": "next build",
  "postbuild": "next-sitemap"
}

์ดํ›„์— ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋นŒ๋“œํ•˜๋ฉด public ํด๋” ์•„๋ž˜์— robots.txt์™€ sitemap.xml ํŒŒ์ผ์ด ์ƒ์„ฑ๋œ๋‹ค!

๋™์  ํŽ˜์ด์ง€์— ๋Œ€ํ•œ ์‚ฌ์ดํŠธ๋งต์€ ์•„๋ž˜์™€ ๊ฐ™์ด ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

// pages/server-sitemap.xml/index.tsx ํŒŒ์ผ์„ ์ƒ์„ฑํ•œ๋‹ค.

import { getServerSideSitemap } from 'next-sitemap'
import { GetServerSideProps } from 'next'

export const getServerSideProps: GetServerSideProps = async (ctx) => {
  // CMS์˜ URL์„ ๊ฐ€์ ธ์˜จ๋‹ค (articleId ๋ฅผ ๋ฆฌํ„ดํ•ด์ฃผ๋Š” api ์š”์ฒญ์„ ํ•  ์ˆ˜ ์žˆ๊ฒ ๋‹ค)
  // const urls = await fetch('https//example.com/api')

  const fields = [
    {
      loc: 'https://example.com', // Absolute url
      lastmod: new Date().toISOString(),
      // changefreq
      // priority
    },
    {
      loc: 'https://example.com/dynamic-path-2', // Absolute url
      lastmod: new Date().toISOString(),
      // changefreq
      // priority
    },
  ]

  return getServerSideSitemap(ctx, fields)
}

// Default export to prevent next.js errors
export default function Sitemap() {}

๊ทธ๋ฆฌ๊ณ  config ์„ค์ •์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ถ”๊ฐ€ํ•œ๋‹ค.

// next-sitemap.config.js

/** @type {import('next-sitemap').IConfig} */
module.exports = {
  siteUrl: 'https://example.com',
  generateRobotsTxt: true,
  exclude: ['/server-sitemap.xml'], // static ์‚ฌ์ดํŠธ๋งต ๋ฆฌ์ŠคํŠธ์—์„œ๋Š” ์ œ์™ธํ•œ๋‹ค
  robotsTxtOptions: {
    additionalSitemaps: [
      'https://example.com/server-sitemap.xml', // ์ถ”๊ฐ€ ์‚ฌ์ดํŠธ๋งต์„ ์„ค์ •ํ•˜๋Š” ์˜ต์…˜์— ์ถ”๊ฐ€ํ•ด์ค€๋‹ค
    ],
  },
}

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ