Skip to content
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

chore(docs): Update tutorial to Head API #36378

Merged
merged 25 commits into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
21fc8a7
use Head API in code snippets for stepwise tutorial
marvinjude Aug 12, 2022
e0fd129
use Head API in SEO tutorial
marvinjude Aug 12, 2022
04802e7
add intro to Head API
marvinjude Aug 15, 2022
9044ea5
correct step
marvinjude Aug 15, 2022
453fb8d
format
marvinjude Aug 15, 2022
3258703
restore image
marvinjude Aug 15, 2022
d06cb6c
Update docs/docs/tutorial/part-2/index.mdx
marvinjude Aug 15, 2022
ac9f618
Update docs/docs/tutorial/part-2/index.mdx
marvinjude Aug 15, 2022
fda31a7
Update docs/docs/tutorial/part-2/index.mdx
marvinjude Aug 15, 2022
06ffdb5
Update docs/docs/tutorial/part-2/index.mdx
marvinjude Aug 15, 2022
a19e582
Update docs/docs/tutorial/part-4/index.mdx
marvinjude Aug 15, 2022
54a35f7
one liner
marvinjude Aug 16, 2022
e9f7402
Update docs/docs/tutorial/part-2/index.mdx
marvinjude Aug 16, 2022
940f74a
resolve conflict
marvinjude Aug 17, 2022
d85f479
Merge branch 'master' into update-tutorial-to-head-api
marvinjude Aug 17, 2022
4eb9999
Update docs/docs/tutorial/part-2/index.mdx
marvinjude Aug 18, 2022
5f5e48e
Update docs/docs/tutorial/part-2/index.mdx
marvinjude Aug 18, 2022
8b8a261
remove duplicate SEO tutorial
marvinjude Aug 18, 2022
ae76510
add key concept
marvinjude Aug 18, 2022
ad073b1
Update docs/docs/tutorial/part-2/index.mdx
marvinjude Aug 18, 2022
bda2418
remove all data prop access
marvinjude Aug 18, 2022
8e2b79d
add note about tutorial vs videos
marvinjude Aug 18, 2022
0b5138c
update note
LekoArts Aug 19, 2022
4d69f0f
add seo component instructions and other stuff
LekoArts Aug 19, 2022
0c0f21e
revert one change
LekoArts Aug 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/docs/glossary/server-side-rendering.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ Instead of purely server-side rendering, Gatsby uses the same APIs to create sta

- [Why server-side render?](/blog/2019-04-02-behind-the-scenes-what-makes-gatsby-great/#why-server-side-render) from _Behind the Scenes: What makes Gatsby Great_

- [Search Engine Optimization (SEO) and Social Sharing Cards with Gatsby](/tutorial/seo-and-social-sharing-cards-tutorial/#reach-skip-nav)
- [Adding an SEO Component](/docs/how-to/adding-common-features/adding-seo-component/)

- [What is a Static Site Generator?](/docs/glossary/static-site-generator/#what-is-a-static-site-generator) from the Gatsby docs
49 changes: 40 additions & 9 deletions docs/docs/tutorial/part-2/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ To build out the basic page structure for your blog site, you'll need to know ab
By the end of this part of the Tutorial, you will be able to:

* Create **page components** to add new pages to your site.
* Add a title to your site using the **Gatsby Head API**
* Import and use a **pre-built component** from another package.
* Create your own **reusable "building block" component**.
* Use component **props** to change the way a component renders.
Expand Down Expand Up @@ -183,13 +184,15 @@ import * as React from 'react'
const IndexPage = () => {
return (
<main>
<title>Home Page</title>
<h1>Welcome to my Gatsby site!</h1>
<p>I'm making this by following the Gatsby Tutorial.</p>
</main>
)
}

// Page title that shows up on the browser tab and in the <head> element
export const Head = () => <title>Home Page</title>

// Step 3: Export your component
export default IndexPage
```
Expand Down Expand Up @@ -222,7 +225,6 @@ import * as React from 'react'
const AboutPage = () => {
return (
<main>
<title>About Me</title>
<h1>About Me</h1>
<p>Hi there! I'm the proud creator of this site, which I built with Gatsby.</p>
</main>
Expand All @@ -233,9 +235,38 @@ const AboutPage = () => {
export default AboutPage
```

2. In a web browser, visit `localhost:8000/about`. When your development server finishes rebuilding your site, the About page should look something like this:
2. Add a page title to your page. Gatsby lets you define a `<title>` and other [document metadata](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head) with the [Gatsby Head API](/docs/reference/built-in-components/gatsby-head/). You have to export a component called `Head` from your page to apply the metadata.
Adding such metadata helps search engines like Google to better understand your site. For this tutorial you'll only be adding a title to this page but you can also later add other metadata.

```js:title=src/pages/about.js
import * as React from 'react'

const AboutPage = () => {
return (
<main>
<h1>About Me</h1>
<p>Hi there! I'm the proud creator of this site, which I built with Gatsby.</p>
</main>
)
}

// Yay! You defined a page title. This shows up on the browser tab
export const Head = () => <title>About Me<title/>

export default AboutPage
```
marvinjude marked this conversation as resolved.
Show resolved Hide resolved
<Announcement style={{marginBottom: "1.5rem"}}>

**Key Gatsby Concept** 💡

![A screenshot of "localhost:8000/about" in a web browser. It has a heading that says, "About Me", and a paragraph that says, "Hi there! I'm the proud creator of this site, which I built with Gatsby."](./about-page.png)
You just added a title to the About page using the [Gastby Head API](https://www.gatsbyjs.com/docs/reference/built-in-components/gatsby-head/). You'll notice that the title is now displayed on the browser tab.
With this API, you can also add other metadata to your site. For example, you can add a description to your site that shows up in search engines like Google.
marvinjude marked this conversation as resolved.
Show resolved Hide resolved

</Announcement>

3. In a web browser, visit `localhost:8000/about`. When your development server finishes rebuilding your site, the About page should look something like this:
LekoArts marked this conversation as resolved.
Show resolved Hide resolved

![A screenshot of "localhost:8000/about" in a web browser. It has a heading and page title that says, "About Me", and a paragraph that says, "Hi there! I'm the proud creator of this site, which I built with Gatsby."](./about-page.png)

<Announcement style={{marginBottom: "1.5rem"}}>

Expand Down Expand Up @@ -343,7 +374,6 @@ import { Link } from 'gatsby'
const IndexPage = () => {
return (
<main>
<title>Home Page</title>
<h1>Welcome to my Gatsby site!</h1>
{/* highlight-next-line */}
<Link to="/about">About</Link>
Expand All @@ -352,6 +382,8 @@ const IndexPage = () => {
)
}

export const Head = () => <title>Home Page</title>

export default IndexPage
```

Expand All @@ -365,7 +397,6 @@ import { Link } from 'gatsby' // highlight-line
const AboutPage = () => {
return (
<main>
<title>About Me</title>
<h1>About Me</h1>
{/* highlight-next-line */}
<Link to="/">Back to Home</Link>
Expand All @@ -374,6 +405,8 @@ const AboutPage = () => {
)
}

export const Head = () => <title>About Me</title>

export default AboutPage
```

Expand Down Expand Up @@ -478,7 +511,6 @@ import { Link } from 'gatsby'
const Layout = ({ pageTitle, children }) => {
return (
<div>
<title>{pageTitle}</title>
<nav>
<ul>
<li><Link to="/">Home</Link></li>
Expand Down Expand Up @@ -643,7 +675,6 @@ import { container } from './layout.module.css' // highlight-line
const Layout = ({ pageTitle, children }) => {
return (
<div className={container}> // highlight-line
<title>{pageTitle}</title>
<nav>
<ul>
<li><Link to="/">Home</Link></li>
Expand Down Expand Up @@ -721,7 +752,6 @@ import {
const Layout = ({ pageTitle, children }) => {
return (
<div className={container}>
<title>{pageTitle}</title>
<nav>
<ul className={navLinks}> // highlight-line
<li className={navLinkItem}> // highlight-line
Expand Down Expand Up @@ -773,6 +803,7 @@ Take a moment to think back on what you've learned so far. Challenge yourself to

* What's the difference between a page component and a building-block component?
* How do you add a new page to your Gatsby site?
* How do you add a title to a page?
* What are the three steps for writing a new React component?
* What are props and when might you use them?
* What is the `children` prop and why is it useful?
Expand Down
11 changes: 8 additions & 3 deletions docs/docs/tutorial/part-4/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,6 @@ const Layout = ({ pageTitle, children }) => {
return (
<div className={container}>
{/* highlight-start */}
<title>{pageTitle} | {data.site.siteMetadata.title}</title>
<header>{data.site.siteMetadata.title}</header>
{/* highlight-end */}
<nav>
Expand Down Expand Up @@ -462,7 +461,6 @@ const Layout = ({ pageTitle, children }) => {

return (
<div className={container}>
<title>{pageTitle} | {data.site.siteMetadata.title}</title>
marvinjude marked this conversation as resolved.
Show resolved Hide resolved
{/* highlight-next-line */}
<header className={siteTitle}>{data.site.siteMetadata.title}</header>
<nav>
Expand Down Expand Up @@ -523,6 +521,10 @@ const BlogPage = () => {
)
}

export const Head = ({ data }) => (
<title>My Blog Posts | {data.site.siteMetadata.title}</title>
)

export default BlogPage
```

Expand All @@ -544,7 +546,6 @@ const Layout = ({ pageTitle, children }) => {

return (
<div className={container}>
<title>{pageTitle} | {data.site.siteMetadata.title}</title>
<header className={siteTitle}>{data.site.siteMetadata.title}</header>
<nav>
<ul className={navLinks}>
Expand Down Expand Up @@ -770,6 +771,10 @@ const BlogPage = () => {
)
}

export const Head = ({ data }) => (
<title> My Blog Posts | {data.site.siteMetadata.title}</title>
)

export default BlogPage
```

Expand Down
2 changes: 2 additions & 0 deletions docs/docs/tutorial/part-5/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,8 @@ const BlogPage = ({ data }) => {
)
}

export const Head = () => <title>My Blog Posts</title>

export const query = graphql`
query {
allMdx(sort: {fields: frontmatter___date, order: DESC}) {
Expand Down
13 changes: 10 additions & 3 deletions docs/docs/tutorial/part-6/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ To create a collection route:
1. Decide what type of node you want to create pages from.
2. Choose which field on that node to use in the route (the URL) for your pages.
3. Create a new page component in your `src/pages` directory using the following naming convention: `{nodeType.field}.js`.
* Don't forget to include the curly braces (`{}`) in your filenames to indicate the dynamic part of the route!
* Don't forget to include the curly braces (`{}`) in your filenames to indicate the dynamic part of the route!

For example, if you wanted to create a separate page for each `Product` node, and you wanted to use the product's `name` field in the URL, you'd create a new file at `src/pages/{Product.name}.js`. Then Gatsby would create those pages at routes like `/water-bottle` or `/sweatshirt` or `/notebook`.

Expand Down Expand Up @@ -155,6 +155,8 @@ When you build your site, Gatsby looks at the page components in your `src/pages
)
}

export const Head = () => <title>Super Cool Blog Posts</title>

export default BlogPost
```

Expand Down Expand Up @@ -330,7 +332,7 @@ Under the hood, Gatsby makes both of these values available to use as query vari
```js
Object {
// ...
pageContext: Object {
pageContext: Object {
id: "11b3a825-30c5-551d-a713-dd748e7d554a"
frontmatter__slug: "my-first-post"
}
Expand Down Expand Up @@ -399,6 +401,8 @@ The JSON object in the Query Variables section should look something like the on
`
// highlight-end

export const Head = () => <title>Super Cool Blog Posts</title>

export default BlogPost
```

Expand Down Expand Up @@ -429,7 +433,8 @@ The JSON object in the Query Variables section should look something like the on
}
}
`

export const Head = ({ data }) => <title>{data.mdx.frontmatter.title}</title>

export default BlogPost
```

Expand Down Expand Up @@ -488,6 +493,8 @@ The last step of Part 6 is to clean up your Blog page. Instead of rendering an e
}
`

export const Head = () => <title>My Blog Posts</title>

export default BlogPage
```

Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.