Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit cab9b44

Browse files
author
george
committed
add prerenderReady call and meta tag optimizations for Page Not Found state
1 parent 099386b commit cab9b44

File tree

10 files changed

+125
-18
lines changed

10 files changed

+125
-18
lines changed

app/components/DocsRoutes/__tests__/DocsRoutes.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import React from "react"
22
import { BrowserRouter as Router } from "react-router-dom"
33
import DocsRoutes from "../DocsRoutes"
44

5+
jest.mock("app/components/PageNotFound", () => global.simpleMock("PageNotFound"))
6+
57
function mountRoutes() {
68
return mount(
79
<Router>

app/components/DocsRoutes/__tests__/__snapshots__/DocsRoutes.spec.js.snap

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,7 @@ exports[`<DocsRoutes /> renders 1`] = `
8282
"url": "/",
8383
}
8484
}
85-
>
86-
<div
87-
className="has-padding-4 has-center-text"
88-
>
89-
<h1>
90-
Sorry, that page doesn't exist. :(
91-
</h1>
92-
<p
93-
className="has-no-margin-bottom"
94-
>
95-
Did you enter the URL correctly?
96-
</p>
97-
</div>
98-
</PageNotFound>
85+
/>
9986
</Route>
10087
</Switch>
10188
</DocsRoutes>

app/components/PageNotFound/PageNotFound.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import React from "react"
22

3+
import SetMeta from "app/components/SetMeta"
4+
35
const PageNotFound = () => {
46
return (
57
<div className="has-padding-4 has-center-text">
8+
<SetMeta pageNotFound={true} />
69
<h1>{"Sorry, that page doesn't exist. :("}</h1>
710
<p className="has-no-margin-bottom">{"Did you enter the URL correctly?"}</p>
811
</div>

app/components/PageNotFound/__tests__/PageNotFound.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import React from "react"
22
import PageNotFound from "../PageNotFound"
33

4+
jest.mock("app/components/SetMeta", () => global.simpleMock("SetMeta"))
5+
46
describe("<PageNotFound />", () => {
57
let wrapper
68
beforeAll(() => {

app/components/PageNotFound/__tests__/__snapshots__/PageNotFound.spec.js.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ exports[`<PageNotFound /> renders 1`] = `
55
<div
66
className="has-padding-4 has-center-text"
77
>
8+
<SetMeta
9+
pageNotFound={true}
10+
/>
811
<h1>
912
Sorry, that page doesn't exist. :(
1013
</h1>

app/components/SetMeta/SetMeta.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@ import { Helmet } from "react-helmet"
44

55
export default class SetMeta extends React.Component {
66
static propTypes = {
7-
title: PropTypes.string.isRequired,
8-
description: PropTypes.string.isRequired,
7+
title: PropTypes.string,
8+
description: PropTypes.string,
9+
pageNotFound: PropTypes.bool,
10+
}
11+
12+
static defaultProps = {
13+
title: "Page Not Found",
14+
description: "",
15+
pageNotFound: false,
916
}
1017

1118
render() {
@@ -14,8 +21,11 @@ export default class SetMeta extends React.Component {
1421
<title itemProp="name" lang="en">
1522
{this.props.title}
1623
</title>
24+
1725
<meta name="description" content={this.props.description} />
1826
<link rel="canonical" href={window.location.href} />
27+
28+
{this.props.pageNotFound && <meta name="prerender-status-code" content="404" />}
1929
</Helmet>
2030
)
2131
}

app/components/SetMeta/__tests__/SetMeta.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,9 @@ describe("<SetMeta />", () => {
66
const wrapper = mount(<SetMeta title="Test title" description="Test description" />)
77
expect(wrapper).toMatchSnapshot()
88
})
9+
10+
it("renders with pageNotFound state", () => {
11+
const wrapper = mount(<SetMeta pageNotFound={true} />)
12+
expect(wrapper).toMatchSnapshot()
13+
})
914
})

app/components/SetMeta/__tests__/__snapshots__/SetMeta.spec.js.snap

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
exports[`<SetMeta /> renders 1`] = `
44
<SetMeta
55
description="Test description"
6+
pageNotFound={false}
67
title="Test title"
78
>
89
<HelmetWrapper
@@ -70,3 +71,83 @@ exports[`<SetMeta /> renders 1`] = `
7071
</HelmetWrapper>
7172
</SetMeta>
7273
`;
74+
75+
exports[`<SetMeta /> renders with pageNotFound state 1`] = `
76+
<SetMeta
77+
description=""
78+
pageNotFound={true}
79+
title="Page Not Found"
80+
>
81+
<HelmetWrapper
82+
defer={true}
83+
encodeSpecialCharacters={true}
84+
titleTemplate="Undernet – %s"
85+
>
86+
<SideEffect(NullComponent)
87+
defer={true}
88+
encodeSpecialCharacters={true}
89+
link={
90+
Array [
91+
Object {
92+
"href": "http://localhost/",
93+
"rel": "canonical",
94+
},
95+
]
96+
}
97+
meta={
98+
Array [
99+
Object {
100+
"content": "",
101+
"name": "description",
102+
},
103+
Object {
104+
"content": "404",
105+
"name": "prerender-status-code",
106+
},
107+
]
108+
}
109+
title="Page Not Found"
110+
titleAttributes={
111+
Object {
112+
"itemprop": "name",
113+
"lang": "en",
114+
}
115+
}
116+
titleTemplate="Undernet – %s"
117+
>
118+
<NullComponent
119+
defer={true}
120+
encodeSpecialCharacters={true}
121+
link={
122+
Array [
123+
Object {
124+
"href": "http://localhost/",
125+
"rel": "canonical",
126+
},
127+
]
128+
}
129+
meta={
130+
Array [
131+
Object {
132+
"content": "",
133+
"name": "description",
134+
},
135+
Object {
136+
"content": "404",
137+
"name": "prerender-status-code",
138+
},
139+
]
140+
}
141+
title="Page Not Found"
142+
titleAttributes={
143+
Object {
144+
"itemprop": "name",
145+
"lang": "en",
146+
}
147+
}
148+
titleTemplate="Undernet – %s"
149+
/>
150+
</SideEffect(NullComponent)>
151+
</HelmetWrapper>
152+
</SetMeta>
153+
`;

app/index.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ if (!modernBrowser) {
88
polyfills.push(import(/* webpackChunkName: "polyfill" */ "core-js/stable"))
99
}
1010

11+
// Tell prerender.io we're ready to prerender
12+
const readyToPrerender = () => {
13+
window.prerenderReady = true
14+
}
15+
16+
const importApp = () => {
17+
return import(/* webpackChunkName: "app" */ "./app")
18+
.then(() => readyToPrerender())
19+
.catch(error => console.error("Polyfills were resolved but the app was not.", error))
20+
}
21+
1122
Promise.all(polyfills)
12-
.then(() => import(/* webpackChunkName: "app" */ "./app"))
13-
.catch(error => console.error("Uh oh! Polyfills couldn't load!", error))
23+
.then(importApp)
24+
.catch(error => console.error("Polyfills could not load.", error))

public/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
gtag("config", "UA-129032354-1")
1313
</script>
1414

15+
<!-- Tell prerender.io not to get a snapshot of the page at first -->
16+
<script>window.prerenderReady = false;</script>
17+
1518
<!-- Title is redundantly set in src/pages/Home/Home.js for accessibility -->
1619
<title lang="en" itemprop="name">
1720
Undernet – A modular, configuration-first front-end framework. No strings.

0 commit comments

Comments
 (0)