GithubHelp home page GithubHelp logo

sergeymyssak / nextjs-sitemap Goto Github PK

View Code? Open in Web Editor NEW
121.0 121.0 5.0 318 KB

Generate dynamic sitemap.xml for Next.js projects following the example of Google!

JavaScript 3.24% TypeScript 96.76%
nextjs sitemap sitemap-builder sitemap-generation sitemap-generator sitemap-xml

nextjs-sitemap's People

Contributors

andreymyssak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

nextjs-sitemap's Issues

Sitemap doesn't exclude module.scss

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()

Sitemap Generation with Dynamic Routes

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!

Add custom hreflang to IDomain locales

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;
}

Allow es modules to be imported in sitemap-generator to use fetch or axios inside sitemap-generator.js ?

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()

error
image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.