Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to add sitemap in ReactJS

Getting Started

Before diving into how to add a sitemap in ReactJS, let's briefly understand what a sitemap is. In simple terms, a sitemap is like a roadmap of your website that guides search engines to all your important pages. It's essential for SEO (Search Engine Optimization) because it makes it easier for Google to crawl (find and index) all of your website's pages.

Now, let's get to the fun part - adding a sitemap in ReactJS. ReactJS, being a JavaScript library for building user interfaces, doesn't include a built-in way to create a sitemap. But don't worry, that's what we're here for!

Step 1: Install Required Packages

First off, we're going to need a few packages to generate our sitemap. Let's install them using npm, which is a package manager for the JavaScript programming language.

npm install --save @babel/preset-env
npm install --save express
npm install --save react-router
npm install --save react-router-config
npm install --save react-router-dom
npm install --save sitemap

Step 2: Create Server.js File

Next, we need to create a server.js file. This file will contain the code for our server, which will handle requests and responses.

In your project root directory, create a new file named server.js and add the following code:

const express = require('express');
const { renderToNodeStream } = require('react-dom/server');
const { StaticRouter } = require('react-router-dom');
const { matchRoutes } = require('react-router-config');
const { SitemapStream, streamToPromise } = require('sitemap');
const routes = require('./src/routes');

const app = express();

app.get('/sitemap.xml', (req, res) => {

});

app.listen(3000, () => console.log('Server started on port 3000'));

Step 3: Route Configuration

In this step, we'll configure our routes. Routes are like the paths to different pages on our website. We'll use the react-router-config package for this.

In your src directory, create a new file named routes.js and add the following code:

const routes = [
  { path: '/', exact: true, component: 'Home' },
  { path: '/about', component: 'About' },
  { path: '/contact', component: 'Contact' },
];

module.exports = routes;

Step 4: Sitemap Generation

Now, let's write the code to generate our sitemap. We'll use the sitemap package for this.

In your server.js file, find the app.get('/sitemap.xml', ...) line and replace it with the following code:

app.get('/sitemap.xml', (req, res) => {
  const sitemap = new SitemapStream({ hostname: 'https://www.yourwebsite.com' });

  matchRoutes(routes, req.path).map(({ route }) => {
    sitemap.write({ url: route.path, changefreq: 'monthly', priority: 0.7 });
  });

  sitemap.end();
  streamToPromise(sitemap).then((sm) => {
    res.header('Content-Type', 'application/xml');
    res.send(sm);
  });
});

Step 5: Running the Server

Finally, let's run our server and see if our sitemap is generated correctly. In your terminal, run the following command:

node server.js

Now, if you navigate to http://localhost:3000/sitemap.xml in your web browser, you should see your shiny new sitemap!

Wrapping Up

Building a sitemap in ReactJS might seem like a daunting task at first, especially for those who are new to programming. However, once you break it down into smaller steps and take the time to understand what each part of the code does, it becomes much more manageable.

Remember, every expert was once a beginner. It's okay to struggle and fail sometimes. That's how we learn and grow. So keep trying, keep learning, and keep building amazing things with ReactJS!

And who knows? Someday, you might be the one writing guides like this to help other beginners on their programming journey. Happy coding!