Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby-plugin-netlify-cms): dynamically import netlify-identity-widget #9565

Merged
merged 3 commits into from Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/babel-preset-gatsby-package/__tests__/index.js
Expand Up @@ -32,6 +32,9 @@ it(`Specifies proper presets and plugins in Node mode`, () => {
path.join(`@babel`, `plugin-proposal-optional-chaining`)
),
expect.stringContaining(path.join(`@babel`, `plugin-transform-runtime`)),
expect.stringContaining(
path.join(`@babel`, `plugin-syntax-dynamic-import`)
),
])
})

Expand Down Expand Up @@ -66,6 +69,9 @@ it(`Specifies proper presets and plugins in debug Node mode`, () => {
path.join(`@babel`, `plugin-proposal-optional-chaining`)
),
expect.stringContaining(path.join(`@babel`, `plugin-transform-runtime`)),
expect.stringContaining(
path.join(`@babel`, `plugin-syntax-dynamic-import`)
),
])
})

Expand Down Expand Up @@ -100,6 +106,9 @@ it(`Specifies proper presets and plugins in browser mode`, () => {
path.join(`@babel`, `plugin-proposal-optional-chaining`)
),
expect.stringContaining(path.join(`@babel`, `plugin-transform-runtime`)),
expect.stringContaining(
path.join(`@babel`, `plugin-syntax-dynamic-import`)
),
])
})

Expand Down Expand Up @@ -134,5 +143,8 @@ it(`Specifies proper presets and plugins in debug browser mode`, () => {
path.join(`@babel`, `plugin-proposal-optional-chaining`)
),
expect.stringContaining(path.join(`@babel`, `plugin-transform-runtime`)),
expect.stringContaining(
path.join(`@babel`, `plugin-syntax-dynamic-import`)
),
])
})
1 change: 1 addition & 0 deletions packages/babel-preset-gatsby-package/index.js
Expand Up @@ -43,6 +43,7 @@ function preset(context, options = {}) {
r(`@babel/plugin-proposal-class-properties`),
r(`@babel/plugin-proposal-optional-chaining`),
r(`@babel/plugin-transform-runtime`),
r(`@babel/plugin-syntax-dynamic-import`),
],
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/babel-preset-gatsby-package/package.json
Expand Up @@ -5,6 +5,7 @@
"dependencies": {
"@babel/plugin-proposal-class-properties": "^7.0.0",
"@babel/plugin-proposal-optional-chaining": "^7.0.0",
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
"@babel/plugin-transform-runtime": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/preset-flow": "^7.0.0",
Expand Down
31 changes: 23 additions & 8 deletions packages/gatsby-plugin-netlify-cms/src/gatsby-browser.js
@@ -1,18 +1,33 @@
/* global __PATH_PREFIX__ */
import netlifyIdentityWidget from "netlify-identity-widget"

// Taken from https://github.com/netlify/netlify-identity-widget
const routes = /(confirmation|invite|recovery|email_change)_token=([^&]+)/
const errorRoute = /error=access_denied&error_description=403/
const accessTokenRoute = /access_token=/
erquhart marked this conversation as resolved.
Show resolved Hide resolved

exports.onInitialClientRender = (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this whole block anything we want to test for? I'd think adding some tests would be really valuable here, even if "just" unit tests!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that would be nice. Not sure how tho, since it's a dynamic import things become a bit more tricky. Are there any similar tests a can look at to get going?

_,
{ enableIdentityWidget = true, publicPath = `admin` }
) => {
if (enableIdentityWidget) {
netlifyIdentityWidget.on(`init`, user => {
if (!user) {
netlifyIdentityWidget.on(`login`, () => {
document.location.href = `${__PATH_PREFIX__}/${publicPath}/`
const hash = (document.location.hash || ``).replace(/^#\/?/, ``)

if (
enableIdentityWidget &&
(hash.match(routes) ||
hash.match(errorRoute) ||
hash.match(accessTokenRoute))
) {
import(`netlify-identity-widget`).then(
({ default: netlifyIdentityWidget }) => {
netlifyIdentityWidget.on(`init`, user => {
if (!user) {
netlifyIdentityWidget.on(`login`, () => {
document.location.href = `${__PATH_PREFIX__}/${publicPath}/`
})
}
})
netlifyIdentityWidget.init()
}
})
netlifyIdentityWidget.init()
)
}
}