sergeymyssak / nextjs-sitemap Goto Github PK
View Code? Open in Web Editor NEWGenerate dynamic sitemap.xml for Next.js projects following the example of Google!
Generate dynamic sitemap.xml for Next.js projects following the example of Google!
When generating sitemap on a default nextjs setup, module.scss files will be included in the sitemap.xml as follow:
<?xml version="1.0" encoding="UTF-8" ?>
<urlset xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url>
<loc>https://example.com/404/</loc>
<lastmod>2021-01-16</lastmod>
</url>
<url>
<loc>https://example.com/404.module/</loc>
<lastmod>2021-01-16</lastmod>
</url>
<url>
<loc>https://example.com/</loc>
<lastmod>2021-01-16</lastmod>
</url>
<url>
<loc>https://example.com/index.module/</loc>
<lastmod>2021-01-16</lastmod>
</url>
</urlset>
By default, scss modules should be excluded.
A possible workaround would be to add them with a wildcard in exclude prop, but the following doesn't work:
const Sitemap = configureSitemap({
baseUrl: 'https://example.com',
exclude: [
'/*.module',
'/**/*.module'
],
excludeIndex: true,
isTrailingSlashRequired: true,
targetDirectory: __dirname + '/public',
pagesDirectory: __dirname + '/src/pages'
})
Sitemap.generateSitemap()
Hello!
I see you mentioned you're looking for maintainers since you no longer have time to work on this. I totally understand ๐
Would you consider deprecating/archiving this package in favor of https://github.com/iamvishnusankar/next-sitemap? I believe this would help the community consolidate efforts on a single package for sitemaps ๐ If you don't want to, totally understand.
Thank you so much!
It's better to print the % of progress for some large sites (few millions pages)
Hey there,
I am currently running the script prebuild on vercel.
What is the best practice to have the sitemap update for dynamic routes which are generated via getStaticPaths and fallback: 'blocking'? Is there a way to have the script run periodically?
Cheers!
I have module scss files with the same folder per page
Eventhough I had added excludedExtensions excludeExtensions: ['.module', '.scss', '.module.scss'] it didn't work. There are still some pages into sitemap like https://example.com/blalblablabla.module .
Since the targeted hreflang might be different from the nextjs locale, I suggest changing the IDomain type to
export interface IDomain {
domain: string;
defaultLocale?: string;
locales?: { nextLocale: string; hreflang: string }[];
http?: boolean;
}
Nextjs supports default locale natively.
Using langs sitemap option with a nexjs defaultLocale will generate wrong path for the default locale, as it should not include locale information in the generated url.
Hello ,
Your documentation say we can implement dynamic paths in our sitemap https://github.com/SergeyMyssak/nextjs-sitemap#with-dynamic-routes
but i can't manage to do so ,
I need to fetch all my url blog from sanity.io
See your example
const { configureSitemap } = require('@sergeymyssak/nextjs-sitemap');
async function fetchDynamicPaths() {
return ['house', 'flower', 'table'];
}
async function getDynamicPaths() {
const paths = await fetchDynamicPaths();
return paths.map((item) => `/project/${item}`);
}
getDynamicPaths().then((paths) => {
const Sitemap = configureSitemap({
domains: [{ domain: 'example.com', defaultLocale: 'en' }],
include: paths,
exclude: ['/project/*'],
excludeIndex: true,
pagesConfig: {
'/project/*': {
priority: '0.5',
changefreq: 'daily',
},
},
trailingSlash: true,
targetDirectory: __dirname + '/public',
pagesDirectory: __dirname + '/src/pages',
});
Sitemap.generateSitemap();
});
But if i want to import fetch module i get an error
const fetch = require("node-fetch")
const { configureSitemap } = require("@sergeymyssak/nextjs-sitemap")
const fs = require("fs")
async function fetchDynamicPaths() {
const query = encodeURIComponent(`{
"posts": *[_type == "post"] {
...,
categories[]->
}}
`)
const url = `https://eb29jfco.api.sanity.io/v1/data/query/production?query=${query}`
const result = await fetch(url).then((res) => res.json())
const paths = []
result.result.posts.map((element) => {
return paths.push({
slug: `${element.slug.current}`,
})
})
return {
paths,
}
}
async function getDynamicPaths() {
const paths = await fetchDynamicPaths()
return paths.paths.map((item) => `/blog/${item.slug}`)
}
const paths = getDynamicPaths()
console.log(paths)
async function writeSitemap() {
// const dynamicPaths = uniquePaths
const publicDirectory = `${__dirname}/public`
const domain = "www.myblog.app"
const Sitemap = configureSitemap({
domains: [
{
domain,
locales: ["fr"],
defaultLocale: "en",
},
],
// include: dynamicPaths,
exclude: ["/advisor", "/404", "/api/*", "blog/*"],
targetDirectory: publicDirectory,
pagesDirectory: `${__dirname}/pages`,
})
const result = await Sitemap.generateSitemap()
let sitemaps = ""
result.forEach((r) => {
sitemaps += `Sitemap: https://${domain}/${r.name}\n`
})
const robotTxt = `# *
User-agent: *
Allow: /
# Host
Host: https://${domain}
# Sitemaps
${sitemaps}`
fs.writeFileSync(`${publicDirectory}/robots.txt`, robotTxt)
}
writeSitemap()
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.