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-remark-autolink-headers): remove hardcoded "let" keyword #21083

Merged
merged 2 commits into from
Jan 31, 2020
Merged

fix(gatsby-remark-autolink-headers): remove hardcoded "let" keyword #21083

merged 2 commits into from
Jan 31, 2020

Conversation

ivan-aksamentov
Copy link
Contributor

Description

In gatsby-remark-autolink-headers, this code snippet

const script = `
document.addEventListener("DOMContentLoaded", function(event) {
var hash = window.decodeURI(location.hash.replace('#', ''))
if (hash !== '') {
var element = document.getElementById(hash)
if (element) {
let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
let clientTop = document.documentElement.clientTop || document.body.clientTop || 0
var offset = element.getBoundingClientRect().top + scrollTop - clientTop
// Wait for the browser to finish rendering before scrolling.
setTimeout((function() {
window.scrollTo(0, offset - ${offsetY})
}), 0)
}
}
})
`

contains 2 instances of hardcoded "let" keyword, which is being rendered into the HTML without transpilation. This makes the script crash in old browsers, such as IE (all versions).

This commit replaces "let" to "var". Care should be taken because this is an unsafe transformation, due to var scope hoisting. My initial tests show that everything is good.

I initially encountered this error while porting https://nextstrain.org to IE: nextstrain/nextstrain.org#113

This fix should help scientists researching 2019-nCoV outbreak (some of them still use Internet Explorer)

Documentation

N/A

Related Issues

Fixes #21058

This resolves #21058

In `gatsby-remark-autolink-headers`, this code snippet https://github.com/gatsbyjs/gatsby/blob/5e7ccd13f79e54036869a72e423bf7cf4ab486af/packages/gatsby-remark-autolink-headers/src/gatsby-ssr.js#L51-L67
contains 2 instances of hardcoded "let" keyword, which  is being rendered into the HTML without transpilation. This makes the script crash in old browsers, such as IE (all versions).

This commit replaces "let" to "var". Care should be taken because this is an unsafe transformation, due to var scope hoisting. My initial tests show that everything is good.

I initially encountered this error while porting https://nextstrain.org to IE: nextstrain/nextstrain.org#113

This fix should help scientists researching 2019-nCoV outbreak (some of them still use Internet Explorer)
@pieh
Copy link
Contributor

pieh commented Jan 31, 2020

Ok, this makes sense - can we add comment above

  const script = `
    document.addEventListener("DOMContentLoaded", function(event) {	    document.addEventListener("DOMContentLoaded", function(event) {

part to mention that using var is intentional (and why we can't use let)

@ivan-aksamentov
Copy link
Contributor Author

ivan-aksamentov commented Jan 31, 2020

@pieh Hi Michal, thanks for the quick response!
I added a comment explaining the change.

Copy link
Contributor

@pieh pieh left a comment

Choose a reason for hiding this comment

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

Looks good! Thanks @ivan-aksamentov!

@pieh
Copy link
Contributor

pieh commented Jan 31, 2020

Yup, we already used var there (but not consistently). The comment is mostly as a measure to avoid regressions in the future if someone tries to change it to let again.

@pieh pieh added the bot: merge on green Gatsbot will merge these PRs automatically when all tests passes label Jan 31, 2020
@gatsbybot gatsbybot merged commit 3a70011 into gatsbyjs:master Jan 31, 2020
@gatsbot
Copy link

gatsbot bot commented Jan 31, 2020

Holy buckets, @ivan-aksamentov — we just merged your PR to Gatsby! 💪💜

Gatsby is built by awesome people like you. Let us say “thanks” in two ways:

  1. We’d like to send you some Gatsby swag. As a token of our appreciation, you can go to the Gatsby Swag Store and log in with your GitHub account to get a coupon code good for one free piece of swag. We’ve got Gatsby t-shirts, stickers, hats, scrunchies, and much more. (You can also unlock even more free swag with 5 contributions — wink wink nudge nudge.) See gatsby.dev/swag for details.
  2. We just invited you to join the Gatsby organization on GitHub. This will add you to our team of maintainers. Accept the invite by visiting https://github.com/orgs/gatsbyjs/invitation. By joining the team, you’ll be able to label issues, review pull requests, and merge approved pull requests.

If there’s anything we can do to help, please don’t hesitate to reach out to us: tweet at @gatsbyjs and we’ll come a-runnin’.

Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot: merge on green Gatsbot will merge these PRs automatically when all tests passes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

gatsby-remark-autolink-headers: SSR assumes let keyword is available
3 participants