Is netlifyIdentity.init() required after script load?
In order to get netlify identity to initialize on a Gatsby site I need to call netlifyIdentity.init() after loading in netlify-identity-widget.js.
I was able to figure this out by finding this example. https://github.com/AustinGreen/gatsby-starter-netlify-cms/blob/master/src/pages/index.js#L17
However, I don't see this in the documentation anywhere.
Changing basic example documentation doesn't appear to be correct in all cases. https://github.com/netlify/netlify-cms/pull/1030
Should calling init() be required at all in Gatsby?
I believe if you use React Helmet to load the script, it will load in the head and initialization will happen automatically (and on time), no further code required.
This still needs to be tested in the template, but I believe it would work.
I tried the following code based on an issue in react-helmet (https://github.com/nfl/react-helmet/issues/146).
handleNetlifyIdentityLoad() {
console.log('onload');
if (window.netlifyIdentity) {
console.log('netlifyIdentity');
window.netlifyIdentity.on('init', user => {
console.log('init');
if (!user) {
window.netlifyIdentity.on('login', () => {
document.location.href = '/admin/';
});
}
});
//window.netlifyIdentity.init();
}
}
<Helmet
script={[{src: 'https://identity.netlify.com/v1/netlify-identity-widget.js'}]}
onChangeClientState={(newState, addedTags) => {
if (addedTags.scriptTags) {
addedTags.scriptTags[0].onload = this.handleNetlifyIdentityLoad;
}
}}
/>
In my testing it gets to the console.log for 'netlifyIdentity' but not the 'init' line unless I uncomment the init function call.
You can just load the script, nothing else required - I'm going to open a PR to remove the script you referenced in the Gatsby starter as well, since it's no longer necessary. You should be able to just do:
<Helmet script={[{src: 'https://identity.netlify.com/v1/netlify-identity-widget.js'}]}/>
Spoke too soon, redirect still isn't happening. Researching a bit.
Looks like the code you mentioned is still required for redirecting after creating or updating your password on the site's home page. I'm going to update gatsby-plugin-netlify-cms to inject the script so it just works.
@acarl once https://github.com/gatsbyjs/gatsby/pull/4215 is merged, Netlify Identity will "just work" for Gatsby sites using the Netlify CMS plugin. I'll close this once the update is released, as well as https://github.com/AustinGreen/gatsby-starter-netlify-cms/pull/40 being merged in the starter.
I just deployed it with 1.0.9 and it works great! I'll go ahead and close this out. Thanks!