jolbol1 / nextjs-velite-blog-template

A static blog template built using NextJS 14, Velite, Tailwind, Shadcn/UI and MDX. Follow along on YouTube!
https://my-first-blog-dusky.vercel.app
120 stars 40 forks source link

[Feature request] generate Sitemaps #3

Open hunght opened 4 months ago

hunght commented 4 months ago

Thank you for making this, could you add generateSitemaps feature in the future? To support generate blog page automatically. Sorry I don't find where to push this

randysmachado commented 3 months ago

Hi @hunght.

I created my script based from documentation to NextJS, something like this:

// app/sitemap.ts

import { MetadataRoute } from 'next'

import { posts } from '#site/content'
import { siteConfig } from '@/config/site'
import { getAllTags, sortTagsByCount } from '@/lib/utils'

export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
  const sitemapPost: MetadataRoute.Sitemap = posts.map((post) => {
    return {
      url: `${siteConfig.url}/blog/${post.slugAsParams}`,
      priority: 1.0,
      changeFrequency: 'daily',
      lastModified: post.date
    }
  });
  const tags = getAllTags(posts)
  const sortedTags = sortTagsByCount(tags)

  const sitemapPostTags: MetadataRoute.Sitemap = sortedTags.map((tag) => {
    return {
      url: `${siteConfig.url}/tags/${tag}`,
      priority: 1.0,
      changeFrequency: 'daily'
    }
  })

  return [
    {
      url: `${siteConfig.url}`,
      priority: 1.0,
      changeFrequency: 'daily',
      lastModified: new Date()
    },
    {
      url: `${siteConfig.url}/sobre`,
      priority: 1.0,
      changeFrequency: 'daily',
      lastModified: new Date()
    },
    {
      url: `${siteConfig.url}/tags`,
      priority: 1.0,
      changeFrequency: 'daily',
      lastModified: new Date()
    },
    ...sitemapPost,
    ...sitemapPostTags
  ]
}

I hope to helped you.

hunght commented 3 months ago

Thank you. That is very helpful

On Fri, Apr 26, 2024, 6:16 PM Randys Machado @.***> wrote:

Hi @hunght https://github.com/hunght.

I created my script based from documentation to NextJS, something like this:

import { MetadataRoute } from 'next' import { posts } from '#site/content'import { siteConfig } from '@/config/site'import { getAllTags, sortTagsByCount } from '@/lib/utils' export default async function sitemap(): Promise { const sitemapPost: MetadataRoute.Sitemap = posts.map((post) => { return { url: ${siteConfig.url}/blog/${post.slugAsParams}, priority: 1.0, changeFrequency: 'daily', lastModified: post.date } }); const tags = getAllTags(posts) const sortedTags = sortTagsByCount(tags)

const sitemapPostTags: MetadataRoute.Sitemap = sortedTags.map((tag) => { return { url: ${siteConfig.url}/tags/${tag}, priority: 1.0, changeFrequency: 'daily' } })

return [ { url: ${siteConfig.url}, priority: 1.0, changeFrequency: 'daily', lastModified: new Date() }, { url: ${siteConfig.url}/sobre, priority: 1.0, changeFrequency: 'daily', lastModified: new Date() }, { url: ${siteConfig.url}/tags, priority: 1.0, changeFrequency: 'daily', lastModified: new Date() }, ...sitemapPost, ...sitemapPostTags ]}

I hope to helped you.

— Reply to this email directly, view it on GitHub https://github.com/jolbol1/nextjs-velite-blog-template/issues/3#issuecomment-2079186859, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAM6PXBOGCFNOAVSTQUGRGLY7IZP7AVCNFSM6AAAAABFYXSOBKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANZZGE4DMOBVHE . You are receiving this because you were mentioned.Message ID: @.***>

jolbol1 commented 3 months ago

Hi @hunght, sorry for not getting round to replying earlier.

Thanks @randysmachado for this code snippet, exactly the way I would have done it!

Going to leave this open for now as I am considering a part 3 to my tutorial to cover of some other requests