1555297401
Building static sites with React.js using Gatsby.js seem to be more and more a topic of debate when you require an easy to deploy setup, blazing fast speed and smooth developer experience. Gatsby has been consistently growing, and I am super glad to see this shifting trend as we see a large number of blogs, marketing, and e-commerce sites built with it.
Every time, you build a site you can help search engine's crawlers to improve organic search rankings. You have to ensure that search engines like Google can understand your site's architecture and index it intelligently. You can do all that by including a sitemap.xml
file at the root of your site.
Considering the growing popularity of Gatsby.*js *#JAMstack sites, I've decided to write about how you can automatically generate a custom XML sitemap file in your Gatsby powered websites.
So, let’s start with the basic stuff before we get more technical.
A website is composed of several web pages like About, Contact, Blog, Subscribe, etc. A sitemap file maintains a list of all these pages to tell search engines like Google about the organization of your site content. Search engine web crawlers like Googlebot read through this file and crawl your site intelligently.
Back in the early days of the web, HTML sitemap which was a manually generated bullet-list were in trend. But, owing to their popularity and importance, sitemaps are published in XML format instead of HTML since their target audience is search engines and not people. I hope we can move to JSON format someday.
So, an XML sitemap file communicates with the search engines about all the pages which exist on your website.
Considering Search Engine Optimization (SEO), sitemaps are very important. However, they do not affect your search rankings. Instead, if there's a web page which is not indexed, then sitemap tells the search engines about that page to get it appropriately indexed.
Sitemaps are equally important for both new, and old sites. Especially if your site is relatively new then I'd definitely recommend adding one since it is difficult for search engines to find posts and pages of a new site. You want to make the search engine's job as easy as possible to get the most out of it.
You will find sitemap.xml
files on literally every popular website. This helps the search engine bots to keep a tab on various updates and basically everything that goes about on a site that should be indexed.
One key highlight of Gatsby is its growing collection of plugins which implement Gatsby API through simple NPM packages. With time, good folks behind Gatsby have built an extensive ecosystem for plugins which helps you to extend and modify web functionalities.
Now, to create a sitemap you don't have to bother writing several lines of code. There is a Gatsby plugin to generate the sitemap file called the gatsby-plugin-sitemap
.
Before I dive into its installation make sure you have a Gatsby site up and running.
To install the gatsby-plugin-sitemap
package, run the following command in the root folder:
npm install --save gatsby-plugin-sitemap
Recently, I purchased my Maedah.dev
domain where I'll be creating a sitemap file since I am setting it up a new blog with Gatsby. It'll take just a few seconds to install.
Let's install that:
After the plugin is successfully installed, now it's time to add this plugin to the gatsby-config.js
file. A quick reminder to those who are new to Gatsby; that inside gatsby-config.js
file you'll find all the site configuration options which are placed in the root folder.
One of these configuration options is for plugins. Here you find an array of plugins which implement Gatsby APIs. Some plugins are listed by name, while others may take options as well — and gatsby-plugin-sitemap carries options as well (more on this later).
So, add the following lines of code in your gatsby-config.js
file:
module.exports = { siteMetadata: { title: 'Your Site Title', siteUrl: 'https://yoursite.com', }, plugins: ['gatsby-plugin-sitemap'], }
Make sure that the inside siteMetadata
you change the title
and siteUrl
according to your project details.
To create a sitemap you need to generate a production build and start the server. In your terminal type the following command and hit Enter.
npm run build
Wait for a few seconds and you get a working sitemap with Gatsby. Here's a GIF:
By default, the sitemap is generated in the root of your website which is a folder called
public
. When you deploy your site, you can access it through /sitemap.xml
and it will display all of your site’s pages which are currently accessible to users.
You can access the sitemap of your site with the following URL:
https://yoursite.com/sitemap.xml
But as I have mentioned earlier that the gatsby-plugin-sitemap
plugin supports advanced custom options so this default functionality can be changed accordingly. Let's dig a little deep with these options.
The gatsby-plugin-sitemap
support different advanced options which you can customize to gain more control over your sitemap.xml
files. Let's take a look at some of these:
output
: The default file name is sitemap.xml
you can use the output
option to change the name of output file. E.g. with output: '/some-other-sitemap.xml',
the URL now becomes https://yoursite.com/some-other-sitemap.xml
.exclude
: This option can help you exclude any links from the sitemap for whatever reasons.query
: Custom GraphQL query to fetch info like siteMetadata
, siteURL
, allSitePage
, etc.There are a couple of other handy options as well for e.g., for sitemapSize
and sitemap index
. You can visit the official plugin repo for more info here.
For the sake of sharing an example, I tried customizing the plugin's options to generate data of my choice. Here, I have customized the GraphQL query that I will explain below.
{ resolve: `gatsby-plugin-sitemap`, options: { query: `{ site { siteMetadata { siteUrlNoSlash } } allSitePage { edges { node { path } } } allMarkdownRemark { edges { node { fields { slug } } } } }`, serialize: ({ site, allSitePage, allMarkdownRemark }) => { let pages = [] allSitePage.edges.map(edge => { pages.push({ url: site.siteMetadata.siteUrlNoSlash + edge.node.path, changefreq: `daily`, priority: 0.7, }) }) allMarkdownRemark.edges.map(edge => { pages.push({ url: `${site.siteMetadata.siteUrlNoSlash}/${ edge.node.fields.slug }`, changefreq: `daily`, priority: 0.7, }) })return pages }, }, },
Here, I am using the query
option to fetch data for my site
which includes info about siteMetadata
and siteUrlNoSlash
. Further, I am quering the allSitePage
to get all site pages URL paths i.e. to retreiving path
property for each graph node
through all edges
. And finally, I used the allMarkdownRemark
which reads files written in markdown and then converts them into HTML pages. Here we are getting the slug
info for each markdown post from inside the field
property.
Towards the end, I’ve called the serialize
function to map data which is fetched for allSitePage
and allMarkdownRemark
. Each returns a page url with changefreq: ‘daily’
and a priority: 0.7
.
This was one demonstration of playing around with custom options for the gatsby-plugin-sitemap
, you can do it according to the requirement of your project.
Moreover, for a live demo of sitemap.xml
file, I created it in the VSCode.pro course site that my better-half Ahmad Awais had built. You can access this live sitemap demo for Gatsby.js here.
Gif or didn’t happen?!
That’s about it! Generating sitemaps should now be very easy with Gatsby.js. With time, Gatsby has stepped up its game in performance and a lot of effort is being put in improving the DX (Developer Experience). The result of creating a sitemap.xml file with gatsby-plugin-sitemap
is a clear indication for that.
Thanks for reading ❤
If you liked this post, share it with all of your programming buddies!
Follow me on Facebook | Twitter
☞ Angular 7 (formerly Angular 2) - The Complete Guide
☞ The Complete JavaScript Course 2019: Build Real Projects!
☞ Modern React with Redux [2019 Update]
☞ Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
☞ Build Responsive Real World Websites with HTML5 and CSS3
☞ The Complete Web Developer Course 2.0
☞ Top 10 Podcasts for Web Developers
☞ Build Your First PWA with Angular
☞ Building a Desktop App with Vue: NW.js
☞ How to Perform Web-Scraping using Node.js
☞ Build Progressive Web Apps with React
☞ Build a web scraper with Node
#web-development #reactjs
1626645300
Setup Gatsby filesystem to make sure Gatsby is aware of application local files so you can access them via GraphQL.
This is a single video in a series of 26 lessons. Watch the entire course at this playlist here: https://www.youtube.com/playlist?list=PLW0RabRDhwwzVNhlOgQQgw6HJzXdM1MnT
Want to support the channel? We make our courses free to watch so anyone can access our content and level up their skills. For our larger courses, like this one, we sell the final code and design files for a small price to help support the creation of free educational content like this.
You can purchase the course files below–any support is greatly appreciated!
#WebDev #WebDesign #WebDevelopment
#gatsby js #gatsby #js #webdev
1626597840
Clone a Gatsby starter from Github to get the basic boilerplate for our Gatsby React application.
This is a single video in a series of 26 lessons. Watch the entire course at this playlist here: https://www.youtube.com/playlist?list=PLW0RabRDhwwzVNhlOgQQgw6HJzXdM1MnT
Want to support the channel? We make our courses free to watch so anyone can access our content and level up their skills. For our larger courses, like this one, we sell the final code and design files for a small price to help support the creation of free educational content like this.
You can purchase the course files below–any support is greatly appreciated!
#WebDev #WebDesign #WebDevelopment
#gatsby js #webdev #js #gatsby
1626649080
Create a responsive image React component using Gatsby image and Styled Components.
This is a single video in a series of 26 lessons. Watch the entire course at this playlist here: https://www.youtube.com/playlist?list=PLW0RabRDhwwzVNhlOgQQgw6HJzXdM1MnT
Want to support the channel? We make our courses free to watch so anyone can access our content and level up their skills. For our larger courses, like this one, we sell the final code and design files for a small price to help support the creation of free educational content like this.
You can purchase the course files below–any support is greatly appreciated!
#WebDev #WebDesign #WebDevelopment
#webdev #gatsby #js #gatsby js
1622111265
Want to build a custom mobile app for your business or startup? We at AppClues Infotech, provide the best custom mobile app development services in USA. We have highly skilled & creative team of custom mobile app designers and developers that will help to make a perfect mobile app with the latest features & functionalities.
However big or small your app development needs, we’ll build you a finest & effective mobile app that’s tailored specifically to your business needs.
Our Custom Mobile App Development Services:
• Android & iOS App Development
• Cross-Platform & Hybrid App Development
• Enterprise Mobility Solutions
• Mobile Commerce App Development
• Mobile Wallet App Development
• Wearable App Development
• UI/UX Design
• Mobile App Consulting
For more info:
Website: https://www.appcluesinfotech.com/
Email: info@appcluesinfotech.com
Call: +1-978-309-9910
#custom mobile app development company in usa #hire custom mobile app developers #top custom app development company in usa #how to build custom mobile app #custom mobile app development #custom mobile app development services in usa
1626579480
Get your development ready for building sites with Gatsby by installing Homebrew, Node, NPM, Gatsby CLI, and Git.
This is a single video in a series of 26 lessons. Watch the entire course at this playlist here: https://www.youtube.com/playlist?list=PLW0RabRDhwwzVNhlOgQQgw6HJzXdM1MnT
Want to support the channel? We make our courses free to watch so anyone can access our content and level up their skills. For our larger courses, like this one, we sell the final code and design files for a small price to help support the creation of free educational content like this.
You can purchase the course files below–any support is greatly appreciated!
#WebDev #WebDesign #WebDevelopment
#gatsby js #webdev #js #gatsby