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: avoid throwing errors if container is no longer in page #424

Merged
merged 8 commits into from
Apr 24, 2023

Conversation

oscarleonnogales
Copy link
Contributor

@oscarleonnogales oscarleonnogales commented Apr 7, 2023

Description

We have identified a few errors that zoid throws that cannot be caught by the implementation code (render.catch() and close.catch()) during asynchronous dismounts. Although this can occur in every webpage, it's primarily an issue when using React as seen here: Issue #334

With this change, we first check if the container element has been removed from the DOM or the user has navigated away from the page mid-render. In these scenarios we should avoid throwing errors.

Here are 3 examples:
https://github.com/krakenjs/zoid/blob/main/src/parent/parent.js#L857
https://github.com/krakenjs/zoid/blob/main/src/parent/parent.js#L826
https://github.com/krakenjs/zoid/blob/main/src/parent/parent.js#L1172

These 3 error flows eventually cause the promise to be rejected since the render was never completed, and an error to be displayed on the console: https://github.com/krakenjs/zoid/blob/main/src/parent/parent.js#L732

Reproduction Steps (if applicable)

To reproduce the Window navigated away error, open Chrome developer tools and persist logs. Call paypalButtons.render('container-selector') and then refresh the window or navigate to a different URL before second render is finished.

To reproduce Component closed or Detected container element removed from DOM, Call paypalButtons.render('container-selector') and then immediately remove the container selector before second render is finished.

Why are we making these changes? Include references to any related Jira tasks or GitHub Issues

https://paypal.atlassian.net/browse/LI-7432

Copy link
Member

@wsbrunson wsbrunson left a comment

Choose a reason for hiding this comment

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

I haven't looked into testing in Zoid so I don't know how reasonable this ask is: can we write a test for this?

src/parent/parent.js Outdated Show resolved Hide resolved
src/parent/parent.js Outdated Show resolved Hide resolved
@gregjopa
Copy link
Contributor

I haven't looked into testing in Zoid so I don't know how reasonable this ask is: can we write a test for this?

I agree that it would be awesome to add a couple tests to cover this new logic. I'm really excited about this improvement. Great work so far @oscarleonnogales!

@oscarleonnogales oscarleonnogales marked this pull request as ready for review April 10, 2023 22:31
@oscarleonnogales oscarleonnogales requested a review from a team as a code owner April 10, 2023 22:31
src/parent/parent.js Outdated Show resolved Hide resolved
src/constants.js Outdated Show resolved Hide resolved
src/constants.js Outdated Show resolved Hide resolved
@mdentremont
Copy link

This is great, thanks everyone!

@codecov-commenter
Copy link

codecov-commenter commented Apr 11, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.02 🎉

Comparison is base (31d59fb) 95.63% compared to head (d8f9806) 95.65%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #424      +/-   ##
==========================================
+ Coverage   95.63%   95.65%   +0.02%     
==========================================
  Files          18       18              
  Lines        1213     1221       +8     
==========================================
+ Hits         1160     1168       +8     
  Misses         53       53              
Impacted Files Coverage Δ
src/constants.js 100.00% <100.00%> (ø)
src/parent/parent.js 95.66% <100.00%> (+0.05%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Contributor

@gregjopa gregjopa left a comment

Choose a reason for hiding this comment

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

This looks great to me! Nice work @oscarleonnogales 💯

Comment on lines -593 to -611
it("should error out where the domain is an invalid regex", () => {
return wrapPromise(({ expect }) => {
window.__component__ = () => {
return zoid.create({
tag: "test-render-domain-invalid-regex",
url: "mock://www.child.com/base/test/windows/child/index.htm",
domain: /^mock:\/\/www\.meep\.com$/,
});
};

const component = window.__component__();
return component({
onDestroy: expect("onDestroy"),
})
.render(getBody())
.catch(expect("catch"));
});
});

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removing this test because we aren't explicitly checking for valid regex inside of zoid.

The getDomain function already has proper error handling and checking in the cross-domain-utils repository.

@spencersablan
Copy link

Looks great man 🔥 Love that you moved the error messages into a const ⚡️

context === CONTEXT.IFRAME &&
isClosed &&
((currentContainer && isElementClosed(currentContainer)) ||
isSecondRenderFinished)
Copy link
Member

Choose a reason for hiding this comment

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

where did we land on checking for second render? I thought we said this might cause weird issues but I don't remember if we changed our minds later

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We did decide to not use the condition of waiting until second render was finished by itself, as that could cause issues when the container is removed for a legitimate reason before it's finished.

Essentially I combined options 1 and 2 and it seemed to work well. If the container is removed, OR second render is finished then we should call close here.

Copy link
Member

Choose a reason for hiding this comment

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

okay sounds good, thank you!

Copy link
Contributor

Choose a reason for hiding this comment

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

I am a little worried about adding the new isSecondRenderFinished logic here. The prerender phase still renders inside an <iframe> with no src tag. If that gets destroyed, we would still want this error to be thrown.

src/parent/parent.js Outdated Show resolved Hide resolved
src/parent/parent.js Outdated Show resolved Hide resolved
src/parent/parent.js Outdated Show resolved Hide resolved
@wsbrunson wsbrunson merged commit e2825bb into krakenjs:main Apr 24, 2023
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants