This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
gatsby-ssr.js
93 lines (73 loc) · 2.75 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
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
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var React = require("react");
var SnipcartStyles = require("./components/SnipcartStyles");
var Snipcart = require("./components/Snipcart");
var SnipcartProvider = require("./components/SnipcartProvider").default;
var GATSBY_SNIPCART_API_KEY = process.env.GATSBY_SNIPCART_API_KEY;
/**
* insert script, style and tag in body on ssr render
* @param options : {currency, version}
*/
exports.onRenderBody = function (_ref, pluginOptions) {
var setPostBodyComponents = _ref.setPostBodyComponents;
if (pluginOptions === void 0) {
pluginOptions = {};
}
var _options = (0, _extends2.default)({}, {
version: "3.0.29",
innerHTML: "",
openCartOnAdd: true,
useSideCart: false,
templatesUrl: null
}, pluginOptions); // find public api key in options plugin or environment variable
var publicApiKey = GATSBY_SNIPCART_API_KEY || _options.publicApiKey;
if (!publicApiKey) {
throw new Error("Snipcart public API Key is not defined. Insert in plugin options the \"publicApiKey\" parameter or use GATSBY_SNIPCART_API_KEY in environment variable");
return null;
} // Use a default currency value by default. True if plugin option is undefined
// or defined as true. False only if plugin option is defined as false.
var provideDefaultCurrency = _options.provideDefaultCurrency !== false ? true : false;
var components = [/*#__PURE__*/React.createElement(Snipcart, {
key: "snipcart",
publicApiKey: publicApiKey,
innerHTML: _options.innerHTML // Only pass currency value if using default currency
,
currency: provideDefaultCurrency ? _options.currency : null,
openCartOnAdd: _options.openCartOnAdd,
useSideCart: _options.useSideCart,
templatesUrl: _options.templatesUrl
}),
/*#__PURE__*/
// insert style
React.createElement(SnipcartStyles, {
key: "snipcart-style",
version: _options.version
}),
/*#__PURE__*/
// insert script
React.createElement("script", {
key: "snipcart-script",
defer: true,
rel: "preload",
as: "script",
src: "https://cdn.snipcart.com/themes/v" + _options.version + "/default/snipcart.js"
})];
return setPostBodyComponents(components);
};
/**
* wrapp app with provider for dispatch cart and customer infos
*/
exports.wrapRootElement = function (_ref2, pluginOptions) {
var element = _ref2.element;
if (pluginOptions === void 0) {
pluginOptions = {};
}
var _options = (0, _extends2.default)({}, {
version: "3.0.29",
locales: {},
defaultLang: "en"
}, pluginOptions);
return /*#__PURE__*/React.createElement(SnipcartProvider, _options, element);
};