forked from 3nvi/hellenic-space-agency
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-ssr.js
47 lines (40 loc) · 1.5 KB
/
gatsby-ssr.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
import React from 'react';
import PageContext from './src/context/PageContext';
import config from './gatsby-config';
export const onPreRenderHTML = ({ getHeadComponents, replaceHeadComponents }) => {
if (process.env.NODE_ENV !== 'production') {
return;
}
const headComponents = getHeadComponents();
// workaround to revert forcefully injected inline styles
// described in https://github.com/gatsbyjs/gatsby/issues/1526
// takes data-href from <style> tag with inline styles which contains URL to global css file
// and transforms it into stylesheet link component
const transformedHeadComponents = headComponents.map(node => {
if (node.type === 'style') {
const globalStyleHref = node.props['data-href'];
if (globalStyleHref) {
return <link href={globalStyleHref} rel="stylesheet" type="text/css" />;
} else {
return node;
}
} else {
return node;
}
});
replaceHeadComponents(transformedHeadComponents);
};
export const wrapPageElement = ({ element, props }) => {
const { excludedPages, supportedLanguages, defaultLanguage } = config.plugins.find(
plugin => plugin.resolve === '@3nvi/gatsby-plugin-intl'
).options;
// if page should be excluded do nothing
if (excludedPages.includes(props.location.pathname)) {
return element;
}
const contextValue = Object.assign({}, props.pageContext, {
supportedLanguages,
defaultLanguage,
});
return <PageContext.Provider value={contextValue}>{element}</PageContext.Provider>;
};