forked from didierfranc/gatsby-plugin-google-fonts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgatsby-ssr.js
63 lines (54 loc) · 1.39 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"use strict";
var _react = require("react");
var _react2 = _interopRequireDefault(_react);
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : { default: obj };
}
var format = function format(string) {
return string
.split(" ")
.map(function (s) {
return s.replace(/^\w/, function (s) {
return s.toUpperCase();
});
})
.join(" ");
};
var getFonts = function getFonts(options) {
return options.fonts.map(format).join("|").replace(/ /g, "+");
};
function getDisplay(options) {
return options.display ? "&display=" + options.display : "";
}
function getAttributes(options) {
var link =
"https://fonts.googleapis.com/css?family=" +
getFonts(options) +
getDisplay(options);
return [
{
key: "fonts",
href: link,
rel: "stylesheet",
crossOrigin: "anonymous",
},
options.attributes || {},
].reduce(function (r, o) {
Object.keys(o).forEach(function (k) {
r[k] = o[k];
});
return r;
}, {});
}
exports.onRenderBody = function (_ref, options) {
var setHeadComponents = _ref.setHeadComponents;
setHeadComponents([
_react2.default.createElement("link", {
key: "google-fonts-preconnect",
rel: "preconnect",
href: "https://fonts.gstatic.com/",
crossOrigin: "anonymous",
}),
_react2.default.createElement("link", getAttributes(options)),
]);
};