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

How to use intersection-observer polyfill in gatsby v2 #10435

Closed
arturhenryy opened this issue Dec 12, 2018 · 2 comments
Closed

How to use intersection-observer polyfill in gatsby v2 #10435

arturhenryy opened this issue Dec 12, 2018 · 2 comments
Labels
type: question or discussion Issue discussing or asking a question about Gatsby

Comments

@arturhenryy
Copy link

Summary

i have to use the intersection-observer polyfill in gatsby v2 and i dont know how to import it correctly. i am already using the a Provider in the gatsby-browser.js so i cannot use require here because otherwise webpack fails bundling because of mixed import / require.

gatsby-browser.js

import React from "react";
import { Provider } from "react-redux";
import createStore from "./src/store/createStore";

export const wrapRootElement = ({ element }) => {
  const store = createStore();
  const ConnectedRootElement = <Provider store={store}>{element}</Provider>;
  return ConnectedRootElement;
};

suggested way to import the polyfill

exports.onClientEntry = () => {
  // IntersectionObserver polyfill for gatsby-image (Safari, IE)
  if (typeof window.IntersectionObserver === `undefined`) {
    import(`intersection-observer`)
    console.log(`👍 IntersectionObserver is polyfilled`)
  }

  // Object-fit/Object-position polyfill for gatsby-image (IE)
  const testImg = document.createElement(`img`)
  if (
    typeof testImg.style.objectFit === `undefined` ||
    typeof testImg.style.objectPosition === `undefined`
  ) {
    import(`object-fit-images`)()
    console.log(`👍 Object-fit/Object-position are polyfilled`)
  }
}
@m-allanson
Copy link
Contributor

Does it work if you do it like this?

export const onClientEntry = async () => {
  if (typeof IntersectionObserver === `undefined`) {
    await import(`intersection-observer`);
  }
}

@m-allanson m-allanson added the type: question or discussion Issue discussing or asking a question about Gatsby label Dec 12, 2018
@arturhenryy
Copy link
Author

@m-allanson thanks that worked. thank you for the help mate.

pfac added a commit to subvisual/subvisual.com that referenced this issue Apr 4, 2019
Why:

* The site is not working in Safari, missing the IntersectionObserver
  polyfill, which we are apparently not importing correctly.

This change addresses the issue by:

* Implementing the configuration suggested in
  gatsbyjs/gatsby#10435 (comment)
chochihim added a commit to measurableai/mai-website that referenced this issue Feb 5, 2020
chochihim added a commit to measurableai/mai-website that referenced this issue Feb 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question or discussion Issue discussing or asking a question about Gatsby
Projects
None yet
Development

No branches or pull requests

2 participants