SEO and head tags
We're getting pretty close to having a finished site now, one that is fully-featured and looks good. But how is anyone going to find it?
One thing we've yet to take into account is SEO. To boost the chances of our site appearing in the search engine listings we'll add some tags to the <head> element of our document. Tags such as title and description can be important for SEO so it's good practice if our theme includes them.
Frontity provides a <Head> component that can be used to add tags to the HTML <head> element.
Let's import the <Head> component into our root component file. Then anything that we include within <Head>...</Head> tags in our component will be included in the <head> section of the HTML document sent to the browser.
1
// File: /packages/my-first-theme/src/components/index.js
2
3
import { ..., Head } from "frontity";
4
5
// ...
6
7
const Root = ({ state, actions }) => {
8
const data = state.source.get(state.router.link);
9
10
return (
11
<>
12
<Head>
13
<title>My First Frontity Theme</title>
14
<meta
15
name="description"
16
content="Based on the Frontity step by step tutorial"
17
/>
18
</Head>
19
...
20
</>
21
);
22
};
Copied!
With this code added you should already see the title appearing in the tab of you browser, and if you check your document with the browser devtools you should see the title and the description showing up.
Frontity in the console
You can include the <Head> component wherever you like, there’s no need for it to be in the <Root> component of the theme. Additionally you can use variables so that the tags change dynamically according to the content.
Let's put the post title in the <title> element and the post excerpt in the meta description tag on post pages. As before, we'll import the <Head> component into post.js and use it within the returned element. But instead of the strings that we used before we'll populate the content of the elements with data from the post.
1
// File: /packages/my-first-theme/src/components/post.js
2
3
import { connect, styled, Head } from "frontity"
4
// ...
5
6
const Post = ({ state, libraries }) => {
7
// ...
8
return (
9
<div>
10
<Head>
11
<title>{post.title.rendered}</title>
12
<meta name="description" content={post.excerpt.rendered} />
13
</Head>
14
<h2>...</h2>
15
// ...
16
</div>
17
)
18
}
Copied!
Now if you check in your browser devtools you'll see the title and meta description for each post corresponds to the title and excerpt that is held in the state for that post. Also the browser tab helpfully shows the title of the post rather than the previous generic title.
Frontity in the console
Frontity in the browser
Why not try the same exercise for page.js and then test the results using the About Us page.
Check you're on the right track by comparing your changes with the ones in this commit.
Last modified 5mo ago
Copy link