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 5 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
42 changes: 33 additions & 9 deletions docs/docs/tutorial/part-2/index.mdx
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 title to your site using the **Gatsby Head API**
marvinjude marked this conversation as resolved.
Show resolved Hide resolved
* 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
marvinjude marked this conversation as resolved.
Show resolved Hide resolved
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,31 @@ 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: Gatsby lets you define a title and other [document head](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head) related tags with the [Gatsby Head API](/docs/reference/built-in-components/gatsby-head/).
When you export a `Head` component from your page file, It automatically picks all the head tags in your JSX and adds them to the [document head](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head).
This is important because it helps search engines like Google to better understand your site. For this tutorial we'll only be adding a title to this page but you can also add `<meta>`, `<links>` and other head related tags
marvinjude marked this conversation as resolved.
Show resolved Hide resolved

```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! We defined a page title. This shows up on the browser tab
marvinjude marked this conversation as resolved.
Show resolved Hide resolved
export const Head = () => <title>About Me<title/>

export default AboutPage
```
marvinjude marked this conversation as resolved.
Show resolved Hide resolved

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 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)
![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 +367,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 +375,8 @@ const IndexPage = () => {
)
}

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

export default IndexPage
```

Expand All @@ -365,7 +390,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 +398,8 @@ const AboutPage = () => {
)
}

export const Head = () => <title>About Page</title>
marvinjude marked this conversation as resolved.
Show resolved Hide resolved

export default AboutPage
```

Expand Down Expand Up @@ -478,7 +504,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 +668,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 +745,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 +796,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 title to page?
marvinjude marked this conversation as resolved.
Show resolved Hide resolved
* 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
13 changes: 8 additions & 5 deletions docs/docs/tutorial/part-4/index.mdx
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 @@ -420,8 +419,6 @@ const Layout = ({ pageTitle, children }) => {
export default Layout
```

![A screenshot of the home page in a web browser. The title of the page in the browser tab now says, "Home Page | My First Gatsby Site", and the top of the page has an unstyled paragraph that says, "My First Gatsby Site".](./index-page-with-site-title.png)
LekoArts marked this conversation as resolved.
Show resolved Hide resolved

4. Now that the site title is showing up on the page, it's time to add some style! Define some styles for the site title below the existing styles in your `layout.module.css` file.

```css:title=src/components/layout.module.css
Expand Down Expand Up @@ -462,7 +459,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 +519,10 @@ const BlogPage = () => {
)
}

export const Head = ({ data }) => (
<title> My Blog Posts | {data.site.siteMetadata.title}</title>
marvinjude marked this conversation as resolved.
Show resolved Hide resolved
)

export default BlogPage
```

Expand All @@ -544,7 +544,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 +769,10 @@ const BlogPage = () => {
)
}

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

export default BlogPage
```

Expand Down
4 changes: 4 additions & 0 deletions docs/docs/tutorial/part-5/index.mdx
Expand Up @@ -557,6 +557,10 @@ const BlogPage = ({ data }) => {
)
}

const Head = () => (
LekoArts marked this conversation as resolved.
Show resolved Hide resolved
<title>My Blog Posts</title>
)

export const query = graphql`
query {
allMdx(sort: {fields: frontmatter___date, order: DESC}) {
Expand Down
23 changes: 23 additions & 0 deletions docs/docs/tutorial/part-6/index.mdx
Expand Up @@ -146,6 +146,10 @@ When you build your site, Gatsby looks at the page components in your `src/pages
)
}

export const Head = () => (
LekoArts marked this conversation as resolved.
Show resolved Hide resolved
<title>Super Cool Blog Posts</title>
)

export default BlogPost
```

Expand Down Expand Up @@ -393,6 +397,10 @@ The JSON object in the Query Variables section should look something like the on
`
// highlight-end

export const Head = () => (
LekoArts marked this conversation as resolved.
Show resolved Hide resolved
<title> Super Cool Blog Posts </title>
)

export default BlogPost
```

Expand Down Expand Up @@ -428,6 +436,12 @@ The JSON object in the Query Variables section should look something like the on
}
`

export const Head = ({ data }) => {
LekoArts marked this conversation as resolved.
Show resolved Hide resolved
return(
<title>{data.mdx.frontmatter.title}</title>
)
}

export default BlogPost
```

Expand Down Expand Up @@ -477,6 +491,11 @@ The last step of Part 6 is to clean up your Blog page. Instead of rendering the
}
`

export const Head = () => (
LekoArts marked this conversation as resolved.
Show resolved Hide resolved
<title> My Blog Posts </title>
)


export default BlogPage
```

Expand Down Expand Up @@ -524,6 +543,10 @@ The last step of Part 6 is to clean up your Blog page. Instead of rendering the
}
`

export const Head = () => (
LekoArts marked this conversation as resolved.
Show resolved Hide resolved
<title> My Blog Posts </title>
)

export default BlogPage
```

Expand Down