A static website contains Web pages with fixed content. Technically, it is a simple list of HTML files, which displays the same information to every visitor. Unlike dynamic websites, they do not require any back-end programming or database. Publishing a static website is easy: the files are uploaded on a simple Web server or storage provider. The two main advantages of static websites are security and speed: there is no database so it can not be hacked and there is no need to render a page for each request, which makes Web browsing faster. To make their creation easier, numerous open-source static websites generators are available: , , , etc. Most of the time, the content is managed through static (ideally Markdown) files or a Content API. Then, the generator requests the content, injects it in templates defined by the developer and generates a bunch of HTML files. Jekyll Hugo Hexo Progressive Web Apps (PWA) are web applications, highly based on JavaScript, and are . Since they make web browsing much faster and offer a better user experience, PWA have become the default way to build Web interfaces. Thus, many amazing front-end frameworks appeared over the last couple years: Angular, Vue and more recently, React. reliable, fast and engaging Gatsby: when static websites meet Progressive Web Apps Static websites and PWA both have strong advantages which make us crave for a way to use them both in the same project! Luckily, we have tools that bridge the gap between them and the one we recently heard of is definitely Gatsby. So, we decided to give you a complete example of how to get started with Gatsby. A static website needs a source of content: in this example we will deliver it using an API built with Strapi. What is Gatsby? is a . It allows developers to build React based websites within minutes. Whether you want to develop a blog or a corporate website, Gatsby will fill your needs. Gatsby blazing-fast website framework for React Because it is based on React, the website pages are never reloaded which makes the generated website super fast. A large set of plugins is available to let developers save time and get data from any source (Markdown files, CMS, etc.). Gatsby is strongly based on the , which is the center of Gatsby’s data system. “node” interface Created by , the project has been officially and is already . Kyle Mathews released in July 2017 used by tens of companies What is Strapi? is the Strapi most advanced Node.js Headless Content Management System . Thanks to its extensible plugin system, it provides a large set of built-in features: Admin Panel, Authentication & Permissions management, Content Management, API Generator, etc. Unlike others CMSs, , which means: Strapi is 100% open-source . Strapi is completely free You can , so you own the data. host it on your own servers It is entirely , thanks to the plugin system. customisable and extensible API Setup To make the magic happen, let’s create a Strapi API and add some content. Create a Strapi project Install Strapi Requirements: please make sure Node 8 (or higher) and MongoDB are installed and running on your machine. Install Strapi using npm: $ npm i strapi@alpha -g Note: Strapi v3 is still an alpha version, but it will be fine for this tutorial. Generate a Strapi project Create a directory named : gatsby-strapi-tutorial $ mkdir gatsby-strapi-tutorial Scaffold your API inside it through a single command line: $ cd gatsby-strapi-tutorial$ strapi new api Start the server Enter inside your project’s folder: $ cd api Launch the Node.js server: $ strapi start Starting now, you should be able to visit the admin panel of your project: . http://localhost:1337/admin Create your first User Add your first user from the . registration page Create a Content Type Strapi APIs are based on a data structure called Content Types (equivalent of models in frameworks and Content Types in Wordpress). named with four fields: Create a Content Type article (type ) title string (type ) content text (type ) image media (type , many article to one user) author relation Insert some entries Add some articles in the database. To do so, follow these instructions: Visit the . articles list page Click on . Add New Article Insert values, link to an author and submit the form. Create two other articles. Allow access For security reasons, is, by default, restricted. To allow access, visit the , select the action and save. At this point, you should be able to . API access Auth and Permissions section for Guest role Article - find request the list of articles The is also restricted. Authorize anonymous access by selecting the (in "Users & Permissions" section) action and saving the form. author API access find Static website development Great job, our API is ready! We can start developing the static website. Install Gatsby First, install Gatsby CLI: $ npm install --global gatsby-cli Generate a Gatsby project In the folder that you previously created, generate your brand new blog: gatsby-strapi-tutorial $ gatsby new blog Start in development mode Enter in your project’s folder: $ cd blog Start the server: $ gatsby develop At this point, you should already be able to get access to your Gatsby website at this address: . http://localhost:8000 Install the Strapi source plugin When you manage a static website, your data can come from different sources: Markdown files, CSV files, a WordPress website (using the JSON REST API plugin), etc. Gatsby understands this pretty well. So its creators decided to build a specific and independent layer: the data layer. This entire system is strongly powered by . GraphQL To connect Gatsby to a new source of data, you have to . Fortunately, , so on of them should fill your needs. develop a new source plugin several source plugins already exist In this example, we are using Strapi. Obviously, we are going to need a source plugin for Strapi APIs. Good news: ! we built it for you Let’s install it: $ npm install --save gatsby-source-strapi This plugin need some configurations. Replace the content of with: gatsby-config.js Path: _gatsby-config.js_ Then, restart the server to let Gatsby consider these updates. Articles list First, we want to display the list of articles. To do so, add the following content in the existing home page file: Path: _src/pages/index.js_ What are we doing here? At the end of the file, we export , a GraphQL query which requests the entire list of articles. As you can see, we require only the , and fields, thanks to the precise GraphQL query language. pageQuery id title content Then, we pass the destructured object as parameter of and loop on its object to display the data. { data } IndexPage allStrapiArticles Tip: generate your GraphQL query in seconds! Gatsby includes a useful GraphiQL interface. It makes GraphQL queries development way easier and intuitive. and try to create some queries. Take look at it Adding images To add images, we will need to import from package installed by default. Replace the content of with the following : Img gatsby-image src/pages/index.js Path: src/pages/index.js Article view Our website now starts looking like a blog which is a good thing. However, an important part is still missing: the article’s details page. Let’s create the template, containing a specific GraphQL request and defining the content displayed: Path: _src/templates/article.js_ That looks fine, but at this point, Gatsby does not know when this template should be displayed. Each article needs a specific URL. So, we are going to inform Gatsby about the new URLs we need thanks to the . [createPage](https://www.gatsbyjs.org/docs/creating-and-modifying-pages) function First, we are going to code a new function called to execute the GraphQL request. Then, we export a function named in which we get the list of articles and create a page for each of them. Here is the result: makeRequest createPages Path: _gatsby-node.js_ Restart the Gatsby server. From now on, you should be able to visit the detail page by clicking on URLs displayed on the homepage. Author view Articles are written by authors. They deserve a dedicated page. The processes for creating author views and article pages are very similar. First, we create the template: Path: _src/templates/user.js_ Second, we update the file to create the URLs: gatsby-node.js Path: _gatsby-node.js_ Finally, restart the server and visit the author page from the article view’s links. Conclusion Congrats! You’ve successfully built a super fast and easy-to-maintain blog! Since the content is managed by Strapi, the authors can write article through a nice UI and developers only have to rebuilt the Gatsby blog in order to update the content. Where to go next? Feel free to continue this project to discover both Gatsby and Strapi advantages. Here are some features you can add: list of authors, article’s categories, and comment system with the Strapi API or Disqus. You can also create other kind of websites (e-commerce shop, corporate website, etc.). When your project is achieved, you will probably want to deploy it. The static website generated by Gatsby can : Netlify, S3/Cloudfront, GitHub pages, GitLab pages, Heroku, etc. The Strapi API is nothing else than a simple Node.js API, so it can be hosted on Heroku or any Linux instance that has Node.js installed. easily be published on storage providers The . To see it live, clone the repository, run , start the Strapi server ( ) and the Gatsby server ( ). code source of this tutorial is available on GitHub npm run setup cd api && strapi start cd blog && npm run develop We hope you enjoyed this tutorial. Feel free to comment on it, share it, and let us know how you create sites built with React and manage their content.