Skip to content

Commit

Permalink
feat(sitemap): add muitilingual sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
RobsonOlv committed Jan 18, 2024
1 parent 1686d61 commit 3c51b6f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
27 changes: 26 additions & 1 deletion next-sitemap.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,35 @@ const siteUrl = process.env.NEXT_PUBLIC_DOMAIN_URL

module.exports = {
siteUrl,
transform: async (config, path) => {
return {
loc: path, // => this will be exported as http(s)://<config.siteUrl>/<path>
changefreq: config.changefreq,
priority: config.priority,
lastmod: config.autoLastmod ? new Date().toISOString() : undefined,
alternateRefs: [
{
href: `${siteUrl}/pt/${path}`,
hreflang: 'pt',
},
{
href: `${siteUrl}/es/${path}`,
hreflang: 'es',
},
],
}
},
generateIndexSitemap: false,
exclude: ['server-sitemap.xml'],
exclude: ['/404', '/*/404', '/500', '/*/500', 'server-sitemap.xml'],
generateRobotsTxt: true,
robotsTxtOptions: {
policies: [
{
userAgent: '*',
disallow: ['/404'],
},
{ userAgent: '*', allow: '/' },
],
additionalSitemaps: [`${siteUrl}/server-sitemap.xml`],
},
}
20 changes: 16 additions & 4 deletions src/pages/server-sitemap.xml/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { GetServerSidePropsContext } from 'next'
import { getServerSideSitemapLegacy } from 'next-sitemap'
import { ISitemapField, getServerSideSitemapLegacy } from 'next-sitemap'
import getNavigation from 'utils/getNavigation'

const DOMAIN_URL = 'https://leafy-mooncake-7c2e5e.netlify.app'

function getEndpoint(element: any, slugPrefix: string) {
let urls: any = []
let urls: ISitemapField[] = []
if (element.children) {
const children = element.children.flatMap((e: any) => {
return getEndpoint(e, slugPrefix)
Expand All @@ -15,16 +15,28 @@ function getEndpoint(element: any, slugPrefix: string) {
}

if (element.type === 'markdown') {
const url: any = {}
const url: ISitemapField = {
loc: '',
}
url.loc = `${DOMAIN_URL}/${slugPrefix}/${element.slug}`
url.lastmod = new Date().toISOString()
url.alternateRefs = [
{
href: `${DOMAIN_URL}/pt/${slugPrefix}/${element.slug}`,
hreflang: 'pt',
},
{
href: `${DOMAIN_URL}/es/${slugPrefix}/${element.slug}`,
hreflang: 'es',
},
]
urls.push(url)
}
return urls
}

export async function getServerSideProps(ctx: GetServerSidePropsContext) {
let urls: any[] = []
let urls: ISitemapField[] = []
const documentations = await getNavigation()

for (let i = 0; i < documentations.length; i++) {
Expand Down

0 comments on commit 3c51b6f

Please sign in to comment.