Most examples in the Gatsby docs and on the web at large focus on leveraging source plugins to manage your data in Gatsby sites. However, source plugins (or even Gatsby nodes) aren’t strictly necessary to pull data into a Gatsby site! It’s also possible to use an “unstructured data” approach in Gatsby sites, no GraphQL required.
The approach: fetch data and use Gatsby’sNote: For our purposes here, “unstructured data” means data “handled outside of Gatsby’s data layer” (we’re using the data directly, and not transforming the data into Gatsby nodes).
createPages
API
Note: This example is drawn from an example repo built specifically to model how to use this “unstructured data” approach. View the full repo on GitHub.
In your Gatsby project’s gatsby-node.js
file, fetch the needed data, and supply it to the createPage
action within the createPages
API:
createPages
is a Gatsby Node API. It hooks into a certain point in Gatsby’s bootstrap sequence.createPage
action is what actually creates the page.On the highlighted lines, the data is being supplied to the page template, where it can be accessed as props:
When might using “unstructured data” make sense?You may find this approach useful when using Gatsby’s data layer feels a bit too heavy-handed for your project scope.
The pros of using unstructured dataUsing Gatsby’s data layer provides the following benefits:
gatsby-node.js
to pass all Pokémon to the page, and restart the dev server. In contrast, when using queries, you can add a query and it will hot reload.Learn more about GraphQL in Gatsby.
Working outside of the data layer also means foregoing the optimizations provided by transformer plugins, like:
gatsby-plugin-image
(speedy optimized images),gatsby-transformer-sharp
(provides queryable fields for processing your images in a variety of ways including resizing, cropping, and creating responsive images),Another difficulty added when working with unstructured data is that your data fetching code becomes increasingly hairy when you source directly from multiple locations.
The Gatsby recommendationIf you’re building a small site, one efficient way to build it is to pull in unstructured data as outlined in this guide, using createPages
API, and then if the site becomes more complex later on, you move on to building more complex sites, or you’d like to transform your data, follow these steps:
Start building today on
Netlify!
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4