-
Notifications
You must be signed in to change notification settings - Fork 374
/
Copy pathPage.js
322 lines (292 loc) · 9.82 KB
/
Page.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Helmet } from 'react-helmet-async';
import { withRouter } from 'react-router-dom';
import { injectIntl, intlShape } from '../../util/reactIntl';
import classNames from 'classnames';
import routeConfiguration from '../../routeConfiguration';
import config from '../../config';
import { metaTagProps } from '../../util/seo';
import { canonicalRoutePath } from '../../util/routes';
import { CookieConsent } from '../../components';
import facebookImage from '../../assets/yogatimeFacebook-1200x630.jpg';
import twitterImage from '../../assets/yogatimeTwitter-600x314.jpg';
import css from './Page.module.css';
const preventDefault = e => {
e.preventDefault();
};
const twitterPageURL = siteTwitterHandle => {
if (siteTwitterHandle && siteTwitterHandle.charAt(0) === '@') {
return `https://twitter.com/${siteTwitterHandle.substring(1)}`;
} else if (siteTwitterHandle) {
return `https://twitter.com/${siteTwitterHandle}`;
}
return null;
};
class PageComponent extends Component {
constructor(props) {
super(props);
// Keeping scrollPosition out of state reduces rendering cycles (and no bad states rendered)
this.scrollPosition = 0;
this.contentDiv = null;
this.scrollingDisabledChanged = this.scrollingDisabledChanged.bind(this);
}
componentDidMount() {
// By default a dropped file is loaded in the browser window as a
// file URL. We want to prevent this since it might loose a lot of
// data the user has typed but not yet saved. Preventing requires
// handling both dragover and drop events.
document.addEventListener('dragover', preventDefault);
document.addEventListener('drop', preventDefault);
// Remove duplicated server-side rendered page schema.
// It's in <body> to improve initial rendering performance,
// but after web app is initialized, react-helmet-async operates with <head>
const pageSchema = document.getElementById('page-schema');
if (pageSchema) {
pageSchema.remove();
}
}
componentWillUnmount() {
document.removeEventListener('dragover', preventDefault);
document.removeEventListener('drop', preventDefault);
}
scrollingDisabledChanged(currentScrollingDisabled) {
if (currentScrollingDisabled && currentScrollingDisabled !== this.scrollingDisabled) {
// Update current scroll position, if scrolling is disabled (e.g. modal is open)
this.scrollPosition = window.pageYOffset || document.documentElement.scrollTop;
this.scrollingDisabled = currentScrollingDisabled;
} else if (currentScrollingDisabled !== this.scrollingDisabled) {
this.scrollingDisabled = currentScrollingDisabled;
}
}
render() {
const {
className,
rootClassName,
children,
location,
intl,
scrollingDisabled,
referrer,
author,
openGraphType,
description,
facebookImages,
published,
schema,
socialSharing,
tags,
title,
twitterHandle,
twitterImages,
updated,
} = this.props;
const classes = classNames(rootClassName || css.root, className, {
[css.scrollingDisabled]: scrollingDisabled,
});
this.scrollingDisabledChanged(scrollingDisabled);
const canonicalRootURL = config.canonicalRootURL;
const shouldReturnPathOnly = referrer && referrer !== 'unsafe-url';
const canonicalPath = canonicalRoutePath(routeConfiguration(), location, shouldReturnPathOnly);
const canonicalUrl = `${canonicalRootURL}${canonicalPath}`;
const siteTitle = config.siteTitle;
const schemaTitle = intl.formatMessage({ id: 'Page.schemaTitle' }, { siteTitle });
const schemaDescription = intl.formatMessage({ id: 'Page.schemaDescription' });
const pageTitle = title || schemaTitle;
const pageDescription = description || schemaDescription;
const {
title: socialSharingTitle,
description: socialSharingDescription,
images1200: socialSharingImages1200,
// Note: we use image with open graph's aspect ratio (1.91:1) also with Twitter
images600: socialSharingImages600,
} = socialSharing || {};
const openGraphFallbackImages = [
{
name: 'facebook',
url: `${canonicalRootURL}${facebookImage}`,
width: 1200,
height: 630,
},
];
const twitterFallbackImages = [
{
name: 'twitter',
url: `${canonicalRootURL}${twitterImage}`,
width: 600,
height: 314,
},
];
const facebookImgs = socialSharingImages1200 || facebookImages || openGraphFallbackImages;
const twitterImgs = socialSharingImages600 || twitterImages || twitterFallbackImages;
const metaToHead = metaTagProps({
author,
openGraphType,
socialSharingTitle: socialSharingTitle || pageTitle,
socialSharingDescription: socialSharingDescription || pageDescription,
description: pageDescription,
facebookImages: facebookImgs,
twitterImages: twitterImgs,
published,
tags,
twitterHandle,
updated,
url: canonicalUrl,
locale: intl.locale,
});
const facebookPage = config.siteFacebookPage;
const twitterPage = twitterPageURL(config.siteTwitterHandle);
const instagramPage = config.siteInstagramPage;
const sameOrganizationAs = [facebookPage, twitterPage, instagramPage].filter(v => v != null);
// Schema for search engines (helps them to understand what this page is about)
// http://schema.org
// We are using JSON-LD format
// Schema attribute can be either single schema object or an array of objects
// This makes it possible to include several different items from the same page.
// E.g. Product, Place, Video
const hasSchema = schema != null;
const schemaFromProps = hasSchema && Array.isArray(schema) ? schema : hasSchema ? [schema] : [];
const schemaArrayJSONString = JSON.stringify([
...schemaFromProps,
{
'@context': 'http://schema.org',
'@type': 'Organization',
'@id': `${canonicalRootURL}#organization`,
url: canonicalRootURL,
name: siteTitle,
sameAs: sameOrganizationAs,
logo: `${canonicalRootURL}/static/webapp-icon-192x192.png`,
address: config.address,
},
{
'@context': 'http://schema.org',
'@type': 'WebSite',
url: canonicalRootURL,
description: schemaDescription,
name: schemaTitle,
},
]);
const scrollPositionStyles = scrollingDisabled
? { marginTop: `${-1 * this.scrollPosition}px` }
: {};
// If scrolling is not disabled, but content element has still scrollPosition set
// in style attribute, we scrollTo scrollPosition.
const hasMarginTopStyle = this.contentDiv && this.contentDiv.style.marginTop;
if (!scrollingDisabled && hasMarginTopStyle) {
window.requestAnimationFrame(() => {
window.scrollTo(0, this.scrollPosition);
});
}
return (
<div className={classes}>
<Helmet
htmlAttributes={{
lang: intl.locale,
}}
>
<title>{pageTitle}</title>
{referrer ? <meta name="referrer" content={referrer} /> : null}
<link rel="canonical" href={canonicalUrl} />
<meta httpEquiv="Content-Type" content="text/html; charset=UTF-8" />
<meta httpEquiv="Content-Language" content={intl.locale} />
{metaToHead.map((metaProps, i) => (
<meta key={i} {...metaProps} />
))}
<script id="page-schema" type="application/ld+json">
{schemaArrayJSONString.replace(/</g, '\\u003c')}
</script>
</Helmet>
<CookieConsent />
<div
className={css.content}
style={scrollPositionStyles}
ref={c => {
this.contentDiv = c;
}}
>
{children}
</div>
</div>
);
}
}
const { any, array, arrayOf, bool, func, number, object, oneOfType, shape, string } = PropTypes;
PageComponent.defaultProps = {
className: null,
rootClassName: null,
children: null,
author: null,
openGraphType: 'website',
description: null,
facebookImages: null,
twitterImages: null,
published: null,
referrer: null,
schema: null,
socialSharing: null,
tags: null,
twitterHandle: null,
updated: null,
};
PageComponent.propTypes = {
className: string,
rootClassName: string,
children: any,
scrollingDisabled: bool.isRequired,
// Handle referrer policy
referrer: string,
// SEO related props
author: string,
openGraphType: string, // og:type
description: string, // page description
facebookImages: arrayOf(
shape({
width: number.isRequired,
height: number.isRequired,
url: string.isRequired,
})
),
twitterImages: arrayOf(
shape({
width: number.isRequired,
height: number.isRequired,
url: string.isRequired,
})
),
published: string, // article:published_time
schema: oneOfType([object, array]), // http://schema.org
socialSharing: shape({
title: string,
description: string,
images1200: arrayOf(
// Page asset file can define this
shape({
width: number.isRequired,
height: number.isRequired,
url: string.isRequired,
})
),
images600: arrayOf(
// Page asset file can define this
shape({
width: number.isRequired,
height: number.isRequired,
url: string.isRequired,
})
),
}),
tags: string, // article:tag
title: string, // page title
twitterHandle: string, // twitter handle
updated: string, // article:modified_time
// from withRouter
history: shape({
listen: func.isRequired,
}).isRequired,
location: object.isRequired,
// from injectIntl
intl: intlShape.isRequired,
};
const Page = injectIntl(withRouter(PageComponent));
Page.displayName = 'Page';
export default Page;