New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Docs/yaml json source client import #12634
Changes from 11 commits
1b3d918
3cedcd7
ee48fb4
8dfad67
c5286a3
2708473
27fba61
70ce3fb
63a05d3
ba72546
e6a3ee0
6450b00
cb3c42d
d454a39
d5d5ccf
0ce61e6
1d78ff1
f3419da
7021d8d
d3771d8
f604035
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,177 @@ | ||||||
--- | ||||||
title: Sourcing Content from JSON or YAML | ||||||
--- | ||||||
|
||||||
## Table of Contents | ||||||
|
||||||
- [Introduction](#Introduction) | ||||||
- [Setup](#Setup) | ||||||
- [YAML example](#YAML-example) | ||||||
- [JSON example](#JSON-example) | ||||||
- [Joining the pieces](#Joining-the-pieces) | ||||||
|
||||||
## Introduction | ||||||
|
||||||
As you come across Gatsby and start discovering the extent of its possibilities, sometimes you might wonder about basic things like importing a JSON file or a YAML file directly into a page. And that's what you'll build while following this small tutorial! | ||||||
|
||||||
## Prerequisites | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I don't think this section is needed–the docs reference guides should present information in a flat way, without the narrative of a step-by-step tutorial. ES6 syntax is also used throughout the docs, so it wouldn't be unique to this one. |
||||||
|
||||||
Before we go through the details and code, you should be familiar with the basics of Gatsby. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
Check out the [tutorial](/tutorial/) and brush up on the [documentation](/docs/). | ||||||
In addition to this, some knowledge of [ES6 syntax](https://medium.freecodecamp.org/write-less-do-more-with-javascript-es6-5fd4a8e50ee2) will be useful. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
Otherwise just skip this part and move onto the next part. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
## Setup | ||||||
|
||||||
You'll start by creating a new Gatsby website based on the official _hello world starter_. | ||||||
|
||||||
Open up a terminal and run the following command. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
```bash | ||||||
gatsby new gatsby-YAML-JSON-at-buildtime https://github.com/gatsbyjs/gatsby-starter-hello-world | ||||||
``` | ||||||
|
||||||
After the process is complete, some additional packages are needed. | ||||||
|
||||||
Change directories to the newly created Gatsby website and issue the following command: | ||||||
|
||||||
```bash | ||||||
npm install --save uuid | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We can simplify this section for the sake of learning and not require uuid to be installed. See below. |
||||||
``` | ||||||
|
||||||
This package is used to ensure uniqueness with React prop keys. | ||||||
|
||||||
## YAML example | ||||||
|
||||||
This tutorial starts with sourcing content from a YAML file. If you want to see how to do it using JSON instead, jump to the [next section](#JSON-example). | ||||||
|
||||||
### Adding the YAML content | ||||||
|
||||||
Create a folder called `content` and inside, add a file called `My-YAML-Content.yaml` with the following content inside: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
```yml | ||||||
title: YAML content used at build time with Gatsby | ||||||
content: | ||||||
- item: | ||||||
Cupcake ipsum dolor. Sit amet marshmallow topping cheesecake muffin. Halvah | ||||||
croissant candy canes bonbon candy. Apple pie jelly beans topping carrot cake | ||||||
danish tart cake cheesecake. Muffin danish chocolate soufflé pastry icing bonbon | ||||||
oat cake. Powder cake jujubes oat cake. Lemon drops tootsie roll marshmallow halvah | ||||||
carrot cake. | ||||||
- item: | ||||||
Spicy jalapeno bacon ipsum dolor amet t-bone burgdoggen short loin kevin flank. | ||||||
Filet mignon frankfurter spare ribs, sausage corned beef picanha beef ribs pork belly kevin cupim porchetta alcatra hamburger. | ||||||
Picanha brisket shankle buffalo tri-tip. Doner prosciutto meatloaf ribeye kevin kielbasa jowl beef ribs flank burgdoggen venison. | ||||||
- item: 192.33 | ||||||
- item: 111111 | ||||||
``` | ||||||
|
||||||
### Importing YAML into the page component | ||||||
|
||||||
Now that you have something you want to show, the only thing missing is to create a page that will consume the data. | ||||||
|
||||||
Add a new file called `yml-at-buildtime.js` inside the `pages` folder, with the following code: | ||||||
|
||||||
```javascript | ||||||
import React from "react" | ||||||
import uuid from "uuid" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We should skip any dependencies that aren't strictly needed, so that examples are focused. |
||||||
import YAMLData from "../../content/My-YAML-Content.yaml" | ||||||
|
||||||
const YAMLbuildtime = () => ( | ||||||
<div style={{ maxWidth: `960px`, margin: `1.45rem` }}> | ||||||
<h1>{YAMLData.title}</h1> | ||||||
<ul> | ||||||
{YAMLData.content.map(data => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We can leverage the second argument of |
||||||
return <li key={`content_item_${uuid.v4()}`}>{data.item}</li> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
})} | ||||||
</ul> | ||||||
</div> | ||||||
) | ||||||
export default YAMLbuildtime | ||||||
``` | ||||||
|
||||||
The above code imports YAML source data and renders it in a functional stateless React component with Gatsby. Without any extra configuration, it will display a page sourced from a YAML file. | ||||||
|
||||||
## JSON example | ||||||
|
||||||
In this part you'll use JSON as a datasource. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Since this is a docs reference guide, making it more general than step-by-step will be consistent with the expected format and tone of voice. |
||||||
|
||||||
### Adding the JSON content | ||||||
|
||||||
Create a folder named `content` if it doesn't exist, and then add a new file inside called `My-JSON-Content.json` with the following content inside: | ||||||
|
||||||
```json | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ | ||||||
"title": "JSON content used at build time with Gatsby", | ||||||
"content": [ | ||||||
{ | ||||||
"item": "Cupcake ipsum dolor. Sit amet marshmallow topping cheesecake muffin. Halvah croissant candy canes bonbon candy. Apple pie jelly beans topping carrot cake danish tart cake cheesecake. Muffin danish chocolate soufflé pastry icing bonbon oat cake. Powder cake jujubes oat cake. Lemon drops tootsie roll marshmallow halvah carrot cake." | ||||||
}, | ||||||
{ | ||||||
"item": "Spicy jalapeno bacon ipsum dolor amet t-bone burgdoggen short loin kevin flank. | ||||||
Filet mignon frankfurter spare ribs, sausage corned beef picanha beef ribs pork belly kevin cupim porchetta alcatra hamburger. | ||||||
Picanha brisket shankle buffalo tri-tip. Doner prosciutto meatloaf ribeye kevin kielbasa jowl beef ribs flank burgdoggen venison. | ||||||
Does your lorem ipsum text long for something a little meatier? Give our generator a try… it’s tasty!" | ||||||
}, | ||||||
{ | ||||||
"item": 192.33 | ||||||
}, | ||||||
{ | ||||||
"item": 111111 | ||||||
} | ||||||
] | ||||||
} | ||||||
``` | ||||||
|
||||||
### Importing JSON into the page component | ||||||
|
||||||
Now that you have something that needs to be shown, all that's missing is a page to show it. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
Add a new file called `json-at-buildtime.js` inside the `pages` folder with the following code: | ||||||
|
||||||
```javascript | ||||||
import React from "react" | ||||||
import uuid from "uuid" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Same as above–let's eliminate any extra variables / dependencies to keep learning focused. |
||||||
import JSONData from "../../content/My-JSON-Content.json" | ||||||
|
||||||
const JSONbuildtime = () => ( | ||||||
<div style={{ maxWidth: `960px`, margin: `1.45rem` }}> | ||||||
<h1>{JSONData.title}</h1> | ||||||
<ul> | ||||||
{JSONData.content.map(data => { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
return <li key={`content_item_${uuid.v4()}`}>{data.item}</li> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
})} | ||||||
</ul> | ||||||
</div> | ||||||
) | ||||||
export default JSONbuildtime | ||||||
``` | ||||||
|
||||||
The only thing different in this case, is the file import. Instead of the YAML file, this time you're importing directly a JSON file into the page component. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
Once again: out of the box and without any extra configuration, the page will show the content. | ||||||
|
||||||
## Joining the pieces | ||||||
|
||||||
After all these steps are complete, you should have the following file and folder structure: | ||||||
|
||||||
``` | ||||||
|gatsby-YAML-JSON-at-buildtime | ||||||
|content | ||||||
- My-YAML-Content.yaml or - My-JSON-Content.json | ||||||
|src | ||||||
|pages | ||||||
- index.js | ||||||
- json-at-buildtime.js or - yml-at-buildtime.js | ||||||
- 404.js | ||||||
``` | ||||||
|
||||||
Running `gatsby develop` in the terminal and opening a browser window to `http://localhost:8000/json-at-buildtime/` or `http://localhost:8000/yml-at-buildtime`, you'll see the results of this small tutorial. | ||||||
|
||||||
To make this work on your existing Gatsby site: | ||||||
|
||||||
- For JSON, copy over the file contents of `json-at-buildtime.js` : https://github.com/gatsbyjs/gatsby/examples/using-gatsby-with-json-yaml/src/pages/json-at-buildtime.js and corresponding JSON file https://github.com/gatsbyjs/gatsby/examples/using-gatsby-with-json-yaml/content/My-JSON-Content.json | ||||||
|
||||||
- For YAML, copy over the file contents of `yml-at-buildtime.js`: https://github.com/gatsbyjs/gatsby/examples/using-gatsby-with-json-yaml/src/pages/yml-at-buildtime.js and corresponding YAML file https://github.com/gatsbyjs/gatsby/examples/using-gatsby-with-json-yaml/content/My-YAML-Content.yaml |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove the manual Table of Contents now, as we've got a TOC component! Woot!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there some announcement or documentation about it?
PS: found only is PR #15251