Skip to content

Commit

Permalink
chore(starters): Migrate starters to Head API (#36234)
Browse files Browse the repository at this point in the history
Co-authored-by: Lennart <lekoarts@gmail.com>
  • Loading branch information
marvinjude and LekoArts authored Jul 27, 2022
1 parent 5443479 commit cc548c0
Show file tree
Hide file tree
Showing 29 changed files with 110 additions and 276 deletions.
1 change: 0 additions & 1 deletion starters/blog/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ module.exports = {
icon: `src/images/gatsby-icon.png`, // This path is relative to the root of the site.
},
},
`gatsby-plugin-react-helmet`,
// this (optional) plugin enables Progressive Web App + Offline functionality
// To learn more, visit: https://gatsby.dev/offline
// `gatsby-plugin-offline`,
Expand Down
9 changes: 9 additions & 0 deletions starters/blog/gatsby-ssr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Implement Gatsby's SSR (Server Side Rendering) APIs in this file.
*
* See: https://www.gatsbyjs.com/docs/ssr-apis/
*/

exports.onRenderBody = ({ setHtmlAttributes }) => {
setHtmlAttributes({ lang: `en` })
}
29 changes: 0 additions & 29 deletions starters/blog/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions starters/blog/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"gatsby-plugin-image": "^2.19.0",
"gatsby-plugin-manifest": "^4.19.0",
"gatsby-plugin-offline": "^5.19.0",
"gatsby-plugin-react-helmet": "^5.19.0",
"gatsby-plugin-sharp": "^4.19.0",
"gatsby-remark-copy-linked-files": "^5.19.0",
"gatsby-remark-images": "^6.19.0",
Expand All @@ -28,7 +27,6 @@
"prismjs": "^1.28.0",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-helmet": "^6.1.0",
"typeface-merriweather": "0.0.72",
"typeface-montserrat": "0.0.75"
},
Expand Down
63 changes: 16 additions & 47 deletions starters/blog/src/components/seo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

import * as React from "react"
import PropTypes from "prop-types"
import { Helmet } from "react-helmet"
import { useStaticQuery, graphql } from "gatsby"

const Seo = ({ description, lang, meta, title }) => {
const Seo = ({ description, lang, title, children }) => {
const { site } = useStaticQuery(
graphql`
query {
Expand All @@ -31,60 +30,30 @@ const Seo = ({ description, lang, meta, title }) => {
const defaultTitle = site.siteMetadata?.title

return (
<Helmet
htmlAttributes={{
lang,
}}
title={title}
titleTemplate={defaultTitle ? `%s | ${defaultTitle}` : null}
meta={[
{
name: `description`,
content: metaDescription,
},
{
property: `og:title`,
content: title,
},
{
property: `og:description`,
content: metaDescription,
},
{
property: `og:type`,
content: `website`,
},
{
name: `twitter:card`,
content: `summary`,
},
{
name: `twitter:creator`,
content: site.siteMetadata?.social?.twitter || ``,
},
{
name: `twitter:title`,
content: title,
},
{
name: `twitter:description`,
content: metaDescription,
},
].concat(meta)}
/>
<>
<title>{defaultTitle ? `${title} | ${defaultTitle}` : title}</title>
<meta name="description" content={metaDescription} />
<meta property="og:title" content={title} />
<meta property="og:description" content={metaDescription} />
<meta property="og:type" content="website" />
<meta name="twitter:card" content="summary" />
<meta
name="twitter:creator"
content={site.siteMetadata?.social?.twitter || ``}
/>
<meta name="twitter:title" content={title} />
<meta name="twitter:description" content={metaDescription} />
{children}
</>
)
}

Seo.defaultProps = {
lang: `en`,
meta: [],
description: ``,
}

Seo.propTypes = {
description: PropTypes.string,
lang: PropTypes.string,
meta: PropTypes.arrayOf(PropTypes.object),
title: PropTypes.string.isRequired,
}

Expand Down
3 changes: 2 additions & 1 deletion starters/blog/src/pages/404.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ const NotFoundPage = ({ data, location }) => {

return (
<Layout location={location} title={siteTitle}>
<Seo title="404: Not Found" />
<h1>404: Not Found</h1>
<p>You just hit a route that doesn&#39;t exist... the sadness.</p>
</Layout>
)
}

export const Head = () => <Seo title="404: Not Found" />

export default NotFoundPage

export const pageQuery = graphql`
Expand Down
4 changes: 2 additions & 2 deletions starters/blog/src/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const BlogIndex = ({ data, location }) => {
if (posts.length === 0) {
return (
<Layout location={location} title={siteTitle}>
<Seo title="All posts" />
<Bio />
<p>
No blog posts found. Add markdown posts to "content/blog" (or the
Expand All @@ -25,7 +24,6 @@ const BlogIndex = ({ data, location }) => {

return (
<Layout location={location} title={siteTitle}>
<Seo title="All posts" />
<Bio />
<ol style={{ listStyle: `none` }}>
{posts.map(post => {
Expand Down Expand Up @@ -65,6 +63,8 @@ const BlogIndex = ({ data, location }) => {

export default BlogIndex

export const Head = () => <Seo title="All posts" />

export const pageQuery = graphql`
query {
site {
Expand Down
5 changes: 3 additions & 2 deletions starters/blog/src/pages/using-typescript.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// If you don't want to use TypeScript you can delete this file!
import * as React from "react"
import { PageProps, Link, graphql } from "gatsby"
import { PageProps, Link, graphql, HeadFC } from "gatsby"

import Layout from "../components/layout"
import Seo from "../components/seo"
Expand All @@ -17,7 +17,6 @@ const UsingTypescript: React.FC<PageProps<DataProps>> = ({
location,
}) => (
<Layout title="Using TypeScript" location={location}>
<Seo title="Using TypeScript" />
<h1>Gatsby supports TypeScript by default!</h1>
<p>
This means that you can create and write <em>.ts/.tsx</em> files for your
Expand All @@ -43,6 +42,8 @@ const UsingTypescript: React.FC<PageProps<DataProps>> = ({
</Layout>
)

export const Head: HeadFC<DataProps> = () => <Seo title="Using TypeScript" />

export default UsingTypescript

export const query = graphql`
Expand Down
22 changes: 14 additions & 8 deletions starters/blog/src/templates/blog-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@ import Bio from "../components/bio"
import Layout from "../components/layout"
import Seo from "../components/seo"

const BlogPostTemplate = ({ data, location }) => {
const post = data.markdownRemark
const siteTitle = data.site.siteMetadata?.title || `Title`
const { previous, next } = data
const BlogPostTemplate = ({
data: { previous, next, site, markdownRemark: post },
location,
}) => {
const siteTitle = site.siteMetadata?.title || `Title`

return (
<Layout location={location} title={siteTitle}>
<Seo
title={post.frontmatter.title}
description={post.frontmatter.description || post.excerpt}
/>
<article
className="blog-post"
itemScope
Expand Down Expand Up @@ -64,6 +61,15 @@ const BlogPostTemplate = ({ data, location }) => {
)
}

export const Head = ({ data: { markdownRemark: post } }) => {
return (
<Seo
title={post.frontmatter.title}
description={post.frontmatter.description || post.excerpt}
/>
)
}

export default BlogPostTemplate

export const pageQuery = graphql`
Expand Down
1 change: 0 additions & 1 deletion starters/default/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module.exports = {
siteUrl: `https://gatsbystarterdefaultsource.gatsbyjs.io/`,
},
plugins: [
`gatsby-plugin-react-helmet`,
`gatsby-plugin-image`,
{
resolve: `gatsby-source-filesystem`,
Expand Down
4 changes: 3 additions & 1 deletion starters/default/gatsby-ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
* See: https://www.gatsbyjs.com/docs/ssr-apis/
*/

// You can delete this file if you're not using it
exports.onRenderBody = ({ setHtmlAttributes }) => {
setHtmlAttributes({ lang: `en` })
}
29 changes: 0 additions & 29 deletions starters/default/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions starters/default/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
"gatsby-plugin-image": "^2.19.0",
"gatsby-plugin-manifest": "^4.19.0",
"gatsby-plugin-offline": "^5.19.0",
"gatsby-plugin-react-helmet": "^5.19.0",
"gatsby-plugin-sharp": "^4.19.0",
"gatsby-source-filesystem": "^4.19.0",
"gatsby-transformer-sharp": "^4.19.0",
"prop-types": "^15.8.1",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-helmet": "^6.1.0"
"react-dom": "^18.1.0"
},
"devDependencies": {
"prettier": "^2.7.1"
Expand Down
Loading

0 comments on commit cc548c0

Please sign in to comment.