question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generating sitemap

See original GitHub issue

Feature request

My site contains several dynamic routes and uses the getStaticPaths and getStaticProps hooks from the 9.3 release. I’d like to generate a sitemap for it. It’s quite hard.

Describe the solution you’d like

I’d like some way of accessing the list of paths/pages generated during the build step. The information I need is printed to the CLI during “next build” but there’s no way (I think) for the developer to tap into it:

Screen Shot 2020-07-26 at 8 02 08 PM

Describe alternatives you’ve considered

There are several other issues on here describing incomplete solutions that only work for static pages. My page has several dynamic routes ([blog.tsx]). I’m not aware of any solution that works.

Additional context

Next.js is 🔥, this is the one thing tripping me up 🤙

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:21
  • Comments:17 (14 by maintainers)

github_iconTop GitHub Comments

21reactions
rokinskycommented, Jul 28, 2020

Solution based on information from the build manifest is not ideal, because pages with getServerSideProps, getInitialProps and getStaticProps with fallback option will not be supported. It is much better to create the sitemap on the fly (the approach was described in discussions multiple times) because only you know your web application better.

Here is my boilerplate from pages/sitemap.xml.jsx:

import { renderToStaticMarkup } from "react-dom/server";

const SitemapIndex = () => null;

const Sitemap = ({ pages, origin }) => {
  /*
   * NOTE: <?xml ?> is optional preamble from the spec,
   *  UTF-8 is the default
   *  version 1.0 is the default
   */
  return (
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
      {pages?.map((page, index) => {
        return (
          <url key={index}>
            <loc>{[origin, page?.fullSlug].join("/")}</loc>
            <lastmod>{page?.publishedAt}</lastmod>
          </url>
        );
      })}
    </urlset>
  );
};

export const getServerSideProps = async ({ res }) => {
  const pages = // TODO: fetch your pages
  const origin = // TODO: place your origin

  res.setHeader("Content-Type", "text/xml");
  res.write(renderToStaticMarkup(<Sitemap pages={pages} origin={origin} />));
  res.end();

  return {
    props: {},
  };
};

export default SitemapIndex;
2reactions
lachlanjccommented, Jan 7, 2021

@marcofranssen In your next.config.js:

module.exports = {
  async rewrites() {
    return [
      {
        source: '/sitemap.xml',
        destination: '/api/sitemap',
      },
      // add more here
    ]
  }
}

More on rewrites: https://nextjs.org/docs/api-reference/next.config.js/rewrites

Read more comments on GitHub >

github_iconTop Results From Across the Web

XML Sitemaps Generator: Create your Google Sitemap Online
Free Online Google Sitemap Generator. XML-sitemaps.com provides free online sitemap generator service, creating an XML sitemap that can be submitted to ...
Read more >
Build and Submit a Sitemap | Google Search Central
There are various tools that can generate a sitemap. However, the best way is to have your website software generate it for you....
Read more >
Sitemaps: What They Are, How to Create One & Submit it to ...
How to Create a Sitemap · 1. Decide which pages on your site should be crawled by Google, and determine the canonical version...
Read more >
Free Visual Sitemap Generator & XML Sitemap Creator
Use our FREE Sitemap Generator tool to crawl your website and generate visual site map with meta tags. Also generate sitemap XML for...
Read more >
How to Create an SEO-Boosting XML Sitemap in 20 Minutes ...
Click on the Yoast Plugin ... It has a “Y” icon, and says “SEO.” It should be located in the left sidebar. ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found