Skip to content

fix: prevent double initialization #284

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

Merged
merged 5 commits into from
Jul 28, 2020

Conversation

jlengstorf
Copy link
Contributor

@jlengstorf jlengstorf commented Jul 3, 2020

(NOTE: don’t merge this until #283 goes in)

when setting the URL, the initialize function needs to be called or else the widget gets stuck. however, when no change is required, the init function was being called twice anyways, which caused some undesirable behavior

this adds a check to fix that

@jlengstorf jlengstorf added the type: bug code to address defects in shipped code label Jul 3, 2020
@netlify
Copy link

netlify bot commented Jul 3, 2020

Deploy Success!

Built with commit 280d898

https://deploy-preview-284--identity.netlify.app

@erezrokah erezrokah self-requested a review July 5, 2020 12:57
@@ -141,9 +145,14 @@ observe(store, "siteURL", () => {
if (store.siteURL === null || store.siteURL === undefined) {
localStorage.removeItem("netlifySiteURL");
} else {
const currentURL = localStorage.getItem("netlifySiteURL");
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a bit confused by this.
Looking at the code I initially thought we could use the change argument passed to the handler instead of relying on localStorage, but then realised that it doesn't makes sense for mobx to report identical values.

Did a quick test and:

const store = observable({
    siteURL: null,
});
observe(store, "siteURL", (change) => {
    console.log(JSON.stringify(change));
});

store.siteURL = "hello";
store.siteURL = "hello";

Only prints a log message on the first assignment to siteURL, so I'm not sure how we're getting identical values here. The only thing I can think of is that localStorage and store. siteURL become out of sync for some reason.

Also, this changes the current behaviour of calling store.init when siteURL is set to null or undefined, but I haven't been able to figure if there are any implications to that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@erezrokah as far as I can tell this is only affected by localhost, so I don't think it poses any issues. it's hard to say for sure because I only sort of get how mobx works, so I was just testing workflows

maybe we can publish a beta and put it on a few sites to test before doing a full release?

Copy link
Contributor

@tzmanics tzmanics left a comment

Choose a reason for hiding this comment

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

LGTM

@jlengstorf
Copy link
Contributor Author

I released a beta netlify-identity-widget@1.8.1-beta.0 so we can try this out

to use it, load the JS from https://unpkg.com/netlify-identity-widget@1.8.1-beta.0/build/netlify-identity-widget.js instead of the standard include URL

@erezrokah do you have projects you want to test this against? it works as expected with the repo examples, but those are pretty bare bones

@erezrokah
Copy link
Contributor

Beta is a good idea, but we had the same issue with the CMS beta versions of trying to figure out how to test the beta channel (eventually we stopped using the beta channel).

I finally had some time to look into the original issue with the repo https://github.com/learnwithjason/stripe-jamstack-subscriptions and I think I was able to trace the underlying issue and wrote a test to reproduce it:
https://github.com/netlify/netlify-identity-widget/compare/fix/double-init-local-2?#diff-1dba658c74d9603708d25270ad6e3383R166

The init event is triggered each time the gotrue instance changes:

store.gotrue && trigger("init", store.gotrue.currentUser());

On initial load we attempt to create a new instance here:

store.init(instantiateGotrue(APIUrl));

So far so good.

The thing is that instantiateGotrue sets the siteURL in a specific case (localhost and existing netlifySiteURL in localStorage):

store.setSiteURL(siteURL);

which will end up calling instantiateGotrue again since we are observing changes in siteURL:

observe(store, "siteURL", () => {

In both cases instantiateGotrue will return a new GoTrue instance:

return new GoTrue({ APIUrl: parts.join(""), setCookie: !isLocal });

Which is set to the store here:

store.gotrue = gotrue;

Which triggers the init event.

The solution was to return null on the first instantiateGotrue call and inside the siteURL observer call instantiateGotrue again with the relevant apiUrl.

@jlengstorf
Copy link
Contributor Author

@erezrokah thanks so much for doing this research! I’m not sure where to go from here, though — do you want me to close this PR and you'll open a different one? or should we make the patch here instead?

jlengstorf and others added 5 commits July 8, 2020 11:54
Refactor callback management to use `Map` so that we can unregister
handlers without changing the current behavior.

Co-authored-by: Cassidy Williams <cassidy@netlify.com>

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
GoTrue was instantiated twice when on localhost and netlifySiteURL configured
@erezrokah erezrokah force-pushed the fix/double-init-local branch from 94462e4 to 280d898 Compare July 8, 2020 08:55
@erezrokah
Copy link
Contributor

I’m not sure where to go from here

I should have been more clear about that. I cherry picked the commit from that branch into this PR, did another rebase and pushed the changes.

I'm good with merging if your are.

@tzmanics tzmanics merged commit b445235 into netlify:master Jul 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug code to address defects in shipped code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants