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 iframe.js breaking links with hash URLs #1115

Merged
merged 7 commits into from
Dec 12, 2022

Conversation

oshi97
Copy link
Contributor

@oshi97 oshi97 commented Dec 9, 2022

Previously, iframe.js would not work with links contains hash URLs, i.e. links like

<a id="skip-to-content-link" href="#main-content">Skip to content</a>

notice the URL starts with a hashtag. These links are different than normal HTML links,
in that instead of redirecting to a new page, they will scroll the browser down to the element
with the id matching the link. For the example above, it would scroll down to an element like

<div id="main-content">main content!</div>

However, iframe.js has an issue where, when one of these links are clicked, the iframe.js will reload
and the parent frame's URL will be changed.
This issue was caused by the following process:

  1. iframe.js's has a window.popstate listener, which reloads the iframe
    whenever a popstate event was triggered (for instance by clicking on one of these links),
  2. clicking a hash link will trigger this listener, reloading the iframe
  3. reloading the iframe causes the inner iframe to send messages to the outer frame
  4. these messages are received by iframe.js's onMessage listener, which then performs a history.replaceState (for certain types of messages)

To avoid this situation, this PR updates the popstate listener to be a no-op if the new URL of the page is the same as the old one, ignoring any hash URLs

J=TECHOPS-7401
TEST=manual

ran the theme's test site, created a basic iframe implementation
before this change, hash urls would have the broken behavior
after the change, they would scroll down the page without reloading the iframe or changing the parent's URL

@coveralls
Copy link

coveralls commented Dec 9, 2022

Coverage Status

Coverage decreased (-0.007%) to 8.743% when pulling 1b1ef6a on dev/fix-iframe-hash-url into 223c682 on hotfix/v1.29.5.

@oshi97 oshi97 requested a review from a team December 9, 2022 22:23
@@ -13,6 +13,8 @@ webpack-config.js
!config/global_config.json
!config/locale_config.json
!config/index.json
!config/index.ar.json
Copy link
Contributor Author

@oshi97 oshi97 Dec 9, 2022

Choose a reason for hiding this comment

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

I deleted these pages to speed up the testing loop. When I added them back I noticed they were missing from the gitignore. They were probably being tracked with git add --force or something similar.


/**
* Reloads the iframe with a recalculated src URL.
* Does not reload the iframe if the URL has only changed its hash param.
Copy link
Contributor

Choose a reason for hiding this comment

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

so we'd still want regular query params to trigger a reload?

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 don't want to touch the preexisting behavior because we're not sure how it's being used or for what purpose it's there. This code was copy and pasted all at once from the AEB (answers experience builder created by Mcgin) and it doesn't seem possible to track that info down.

static/js/iframe-common.js Outdated Show resolved Hide resolved
@oshi97 oshi97 requested a review from nmanu1 December 12, 2022 15:27
@oshi97 oshi97 merged commit 68dc465 into hotfix/v1.29.5 Dec 12, 2022
@oshi97 oshi97 deleted the dev/fix-iframe-hash-url branch December 12, 2022 17:15
oshi97 added a commit that referenced this pull request Dec 12, 2022
* fix iframe.js not working with hash URL links (#1115)
@oshi97 oshi97 mentioned this pull request Dec 12, 2022
oshi97 added a commit that referenced this pull request Dec 12, 2022
Previously, iframe.js would not work with links contains hash URLs, i.e. links like

```html
<a id="skip-to-content-link" href="#main-content">Skip to content</a>
```

notice the URL starts with a hashtag. These links are different than normal HTML links,
in that instead of redirecting to a new page, they will scroll the browser down to the element
with the id matching the link. For the example above, it would scroll down to an element like

```html
<div id="main-content">main content!</div>
```

However, iframe.js has an issue where, when one of these links are clicked, the iframe.js will reload
and the parent frame's URL will be changed.
This issue was caused by the following process:
1. iframe.js's has a window.popstate listener, which reloads the iframe
whenever a popstate event was triggered (for instance by clicking on one of these links),
2. clicking a hash link will trigger this listener, reloading the iframe
3. reloading the iframe causes the inner iframe to send messages to the outer frame
4. these messages are received by iframe.js's onMessage listener, which then performs a history.replaceState (for certain types of messages)

To avoid this situation, this PR updates the popstate listener to be a no-op if the new URL of the page is the same as the old one, ignoring any hash URLs

J=TECHOPS-7401
TEST=manual

ran the theme's test site, created a basic iframe implementation
before this change, hash urls would have the broken behavior
after the change, they would scroll down the page without reloading the iframe or changing the parent's URL
oshi97 added a commit that referenced this pull request Dec 12, 2022
* fix iframe.js not working with hash URL links (#1115)
oshi97 added a commit that referenced this pull request Dec 12, 2022
### Fixes

* fix iframe.js not working with hash URL links (#1115)
* Cherry-picks #1112 to remove IE11 acceptance testing.
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

3 participants