The Gatsby Migration, pt.2 - Dumb Pages
01 February 2020
Updated: 03 September 2023
Introduction
In the last post we looked setting up an application with a few basic routes. These routes were all assigned to Components in the src/pages
directory.
This post will be going throught the Gatsby Setup necessary in order to migrate our current site to Gatsby, we will be looking at the second step in the process that was outlined in the last post:
- Creating the initial React App
- Rendering the “Dumb” pages with Gatsby (This post)
- Rendering the “Smart” page with Gatsby
Getting Ready
In order to Gatsbyify
the application, there are three steps that we will need to take before we can start updating our pages to work with the new system
- Update folder structure
- Install Gatsby
- Create the necessary Gatsby Config files
Update Folder Structure
Gatsby Builds are placed into the public
directory. This currently is the output directory of a React build if we are using the standard React configuration
Before we do anything more we should rename our public
directory to static
as this is what Gatsby uses for static files, and then make a git commit
before we add the public
directory to our .gitignore
Now we need to add the following lines to the end of our .gitignore
file so that we do not track the gatsby build files:
.gitignore
Install Gatsby
To add Gataby to our project we need to add the gatsby
package from npm
as a dependency to our project. From the project’s root directory run:
Next we’ll add/update the following commands in our package.json
file so that we can start the Gatsby Dev Server as well as build the application
package.json
Create Config Files
In order to enable gatsby we need to add the gatsby-config.js
file to our root directory, we can use the starter file with the following content, as it currently stands this doesn’t do anything
gatsby-config.js
Next we’ll create an html.js
file in the src
directory with any relevant content from your index.html
file if any of it has been updated. Also be sure to remove the %PUBLIC_URL%
stuff from the file content
The html.js
file needs to be a React Component with the following basic structure for a standard CRA app
src/html.js
You can add or remove any elements in that file as per your specific requirements but most of the time the above should be fine
Now that we have updated the index.js
file delete the static/index.html
file you can run the Gatsby Dev Server
Your application should now be running on http://localhost:8000/
, if started correctly you should see the Gatsby Development 404 Page:
Development 404 Page
Gatsby.js development 404 page
There’s not a page yet at /
Create a React.js component in your site directory at src/pages/index.js and this page will automatically refresh to show the new page component you created.
If you were trying to reach another page, perhaps you can find it below.
Pages (4)
Search:
/Blog/
/Home/
/NotFound/
/Post/
Page Setup
From the Development 404 Page we can see that Gatsby has found our previously created pages - this is because Gatsby looks for pages in the pages
directory, however when we click on a the links we will notice the following:
Home
and404
render correctlyBlog
results in an error messagePost
results in an error message (we will address this page in the next post)- Routes aren’t aligned with our initial setup
- Components no longer have the layout we setup in the
App.js
file
These are, for the most part, easy problems to solve
Fix the Blog Error Message
If we look at the Blog
page we will see that there is an issue with the Page render, this is because we need to change the Link
components to be imported from gatsby
instead of react-router-dom
because with Gatsby we are no longer using the React Router
Blog.js
Fix the Routes
We can also see that our routes are capitalized which is not what we want. Gatsby uses the file organization to do routing, so in order to correct our routes to align what we defined previously in our App.js
we will first need to rename our files:
Component | Route | Old Name | New Name |
---|---|---|---|
Home | / | Home.js | index.js |
Blog | /blog | Blog.js | blog.js |
NotFound | /* | NotFound.js | 404.js |
We’re not going to handle the
Post
component for now because this uses a dynamic route which is something I’ll cover in the next part of this series
You should stop and restart the Gatsby Dev server with:
When the page loads up we should now see our Home
content rendered on the /
route
Fix the Layout
When we were using the standard React Routing we made use of the App
component to wrap our page routes as well as our navigation. We’ll convert our App.js
file into a component that we can use in our other pages
- Remove the
Router
andSwitch
- Update the
Link
s to usegatsby
instead ofreact-router-dom
- Add the
children
input parameter - Render the
children
in themain
section - Remove unused imports
App.js
Use the Layout
Now that we’ve essentially created a Layout
component in the form of the App
component we can use it to wrap the pages that we’ve exported. We can do this for the Home
page like so:
index.js
The same applies for the blog
and 404
pages
Blog
blog.js
404
404.js
Fix the CSS
Before we can use the App
component we need to add the gatsby-plugin-postcss
and postcss-preset-env
plugins so that Gatsby knows how to interpret the default create-react-app
method of importing our CSS into a component
Stop the Dev Server and install the required packages:
We then need to update the gatsby-config.js
file to use the plugin we just added:
gatsby-config.js
If you start the application now you’ll notice that the font is not correct, this is because we specify the font styles in the index.css
file. In order to fix this we need to import the index.css
file into our application, in the default React application this is done via the src/index.js
file, but since we don’t use that to load up the application anymore we need to create another file to do that with Gatsby
In the root directory create a file called gatsby-browser.js
, in this we just need to import the index.css
file:
gatsby-browser.js
You can now run the following commands and start up the application:
Assuming everything went as planned the application should start up correctly and the styling from our index.css
file will be correctly applied
Summary
By now we have completed the first part of the migration process - converting our static pages to use Gatsby - by taking the following steps:
- Updating our file structure to better align with Gatsby
- Create the relevant Gatsby config files
- Update our
Link
usage - Update our Routing
- Use a shared layout component
- Fixing the CSS to work with Gatsby
In the next part we’ll look at how we can go about providing the data needed for our Post
component using GraphQL and show our posts. We’ll also look at how we can pre-process the data that we provide to our component so that we can enhance our content while improving our content creation process
Nabeel Valley