Skip to content

Commit e55a856

Browse files
committed
Migrate code to use a global variable instead of linking your gatsby-config in your code/bundle
1 parent 4121e27 commit e55a856

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

src/gatsby-ssr.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from "react"
2+
3+
exports.onRenderBody = ({ setPostBodyComponents }, pluginOptions) => {
4+
setPostBodyComponents([
5+
React.createElement(
6+
'script',
7+
{
8+
key: "gatsby-plugin-mailchimp",
9+
dangerouslySetInnerHTML: { __html: `window.__GATSBY_PLUGIN_MAILCHIMP__ = ${JSON.stringify(pluginOptions)};` }
10+
}
11+
)
12+
])
13+
}

src/index.js

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import gatsbyConfig from '../../gatsby-config'
21
import jsonp from 'jsonp'
32
import { validate } from 'email-validator'
43

@@ -18,23 +17,6 @@ const subscribeEmailToMailchimp = url => (
1817
})
1918
)
2019

21-
/*
22-
* parse the plugin options to use in our jsonp request
23-
*/
24-
25-
const getPluginOptions = () => {
26-
// find gatsby-mailchimp plugin options (MC list settings)
27-
const options = gatsbyConfig.plugins.find(
28-
plugin => plugin.resolve === 'gatsby-plugin-mailchimp'
29-
).options
30-
31-
const isString = typeof options.endpoint === 'string'
32-
if (!isString) {
33-
throw `Mailchimp endpoint required and must be of type string. See repo README for more info.`
34-
}
35-
return options
36-
};
37-
3820
/*
3921
* build a query string of MC list fields
4022
* ex: '&KEY1=value1&KEY2=value2'
@@ -65,7 +47,10 @@ const addToMailchimp = (email, fields) => {
6547
// generate Mailchimp endpoint for jsonp request
6648
// note, we change `/post` to `/post-json`
6749
// otherwise, Mailchomp returns an error
68-
let {endpoint} = getPluginOptions()
50+
let {endpoint} = window.__GATSBY_PLUGIN_MAILCHIMP__ || {};
51+
if (!typeof endpoint === 'string') {
52+
return Promise.reject(`Mailchimp endpoint required and must be of type string. See repo README for more info.`);
53+
}
6954
endpoint = endpoint.replace(/\/post/g, '/post-json')
7055

7156
const queryParams = `&EMAIL=${emailEncoded}${convertListFields(fields)}`

0 commit comments

Comments
 (0)