SEO & <meta>
tags
Search Engine Optimization is a dark art that some folks dedicate their entire lives to. We've add a couple of features to Redwood to make HTML-based SEO fairly simple.
Adding a Title
You certainly want to change the title of your Redwood app from the default of "Redwood App." You can start by adding or modifying title
inside of /redwood.toml
[web]
- title = "Redwood App"
+ title = "My Cool App"
port = 8910
apiUrl = "/.redwood/functions"
This title (the app title) is used by default for all your pages if you don't define another one. It will also be used for the title template.
Title Template
Now that you have the app title set, you probably want some consistence with the page title, that's what the title template is for.
Add titleTemplate
as a prop for RedwoodProvider
to have a title template for every page.
- <RedwoodProvider>
+ <RedwoodProvider titleTemplate="%PageTitle | %AppTitle">
/* ... */
<RedwoodProvider />
You can use whatever formatting you'd like in here. Some examples:
"%PageTitle | %AppTitle" => "Home Page | Redwood App"
"%AppTitle · %PageTitle" => "Redwood App · Home Page"
"%PageTitle : %AppTitle" => "Home Page : Redwood App"
Adding to Page <head>
So you want to change the title of your page, or add elements to the <head>
of the page? We've got you!
Let's say you want to change the title of your About page, Redwood provides a built-in <Head>
component, which you can use like this:
+import { Head } from '@cedarjs/web'
const AboutPage = () => {
return (
<div>
<h2>AboutPage</h2>
+ <Head>
+ <title>About the team</title>
+ </Head>
You can include any valid <head>
tag in here that you like. However, Redwood also provides a utility component <Metadata>.
<MetaTags>
DeprecationPrior to Redwood 6.6.0 this component was called <MetaTags>
and had several special hard-coded props like ogContentUrl
, which didn't properly map to the OpenGraph spec. We'll still render <MetaTags>
for the foreseeable future, but it's deprecated and you should migrate to <Metadata>
if you have an existing app.
What About Nested Tags?
Redwood uses @dr.pogodin/react-helmet underneath, which will use the tags furthest down your component tree.
For example, if you set title in your Layout, and a title in your Page, it'll render the one in Page - this way you can override the tags you wish, while sharing the tags defined in Layout.
<meta>
TagsFor these headers to appear to bots and scrapers e.g. for twitter to show your title, you have to make sure your page is prerendered. If your content is static you can use Redwood's built-in Prerender. For dynamic tags, check the Dynamic head tags
Setting <meta>
Tags and OpenGraph Directives with <Metadata>
Often we want to set more than just the title and description of the page – most commonly OpenGraph headers.
Redwood provides a convenience component <Metadata>
to help you create most of these <meta>
tags for you with a more concise syntax. But, you can also pass children and define any custom content that you want.
Here's an example setting some common meta, including a page title, description, og:image
and an http-equiv
:
import { Metadata } from '@cedarjs/web'
const AboutPage = () => {
return (
<div>
<Metadata
title="About page"
description="About the awesome team"
og={{ image: "https://example.com/images/og.png", url: "https://example.com/start" }}
robots="nofollow"
>
<meta httpEquiv="content-type" content="text/html; charset=UTF-8" />
</Metadata>
<h2>About Page</h2>
<p className="font-light">This is the about page!</p>
</div>
)
}
export default AboutPage
This code would be transformed into this HTML and injected into the <head>
tag:
<title>About page</title>
<meta name="title" content="About page" />
<meta name="description" content="About the awesome team" />
<meta name="robots" content="nofollow" />
<meta property="og:title" content="About page" />
<meta property="og:description" content="About the awesome team" />
<meta property="og:image" content="https://example.com/images/og.png" />
<meta property="og:url" content="https://example.com/start" />
<meta property="og:type" content="website" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
Setting an og:image
is how sites like Facebook and Slack can show a preview of a URL when pasted into a post (also known as "unfurling"):
Sites like GitHub go a step farther than a generic image by actually creating an image for a repo on the fly, including details about the repo itself:
If you want to write your own <meta>
tags, skipping the interpolation that <Metadata>
does for you, you can pass them as children to <Metadata>
or just write them into the <head>
tag as normal.