question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make polyfill optional when using in old browsers

See original GitHub issue

Hi there! First of all, thanks for providing the library - it’s excellent!

I have an idea that I’d like to vet with you. I recently used the lib in a website to make a sticky nav. To prevent the site from breaking I need to make it work in IntersectionObserver-less browsers (like IE). Now, I don’t actually care if the nav is sticky in these browsers, I just want my code to run. Adding the polyfill is of course an option, but there’s no optimal way of adding the polyfill without also affecting performance (slightly) for modern browser.

So here’s my idea: make the default behavior of the library so that it will report inView: true for browsers where IntersectionObserver isn’t supported. Optionally, we could hide it behind a configuration option of some sort. This will make more things just work™️ when the intersection observer behavior is an enhancement and not a requirement.

With a pointer or two, I’d be happy to come up with a PR for this.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
thebuildercommented, Mar 26, 2020

Am I crazy here? For the moment I’m just going to use an error boundary, roughly like this:

import { ErrorBoundary } from 'react-error-boundary';

const LazyImageThatThrowsErrors = ({ src }) => {
  // This hook throws a runtime error if the browser doesn't support
  // IntersectionObserver.
  const [ref, inView] = useInView();
  return <img ref={ref} src={inView ? src : undefined} />;
};

const LazyImage = ({ src }) => {
  return (
    <ErrorBoundary FallbackComponent={() => <img src={src} />}>
      <LazyImageThatThrowsErrors src={src} />
    </ErrorBoundary>
  );
};

That might work? Honestly, I would just do a check for Intersection support, and adjust accordingly:

const Image= ({ src }) => {
  const [ref, inView] = useInView();
  const unsupported = typeof window.IntersectionObserver === 'undefined'
  
  return <img ref={!unsupported  ? ref : undefined} src={inView || unsupported  ? src : undefined} />;
};

As long as you don’t assign the ref, the hook will IntersectionObserver will never be instantiated. This example will fail if rendered serverside - Move the check into a useEffect if you need to do that.

0reactions
tshddxcommented, Mar 26, 2020

You’d wrap you application/component in an error boundary. But that would just stop it from breaking other parts of the application. I’m also not sure it would trigger the error boundary, since it happens as a side effect (not the render method itself).

My goal is to have browsers which don’t support IntersectionObserver to simply revert to normal <img src="foo.jpg" /> behavior.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Polyfills, transpilation, and browser support | by Dominic Fraser
This allows developers to write using the latest syntax but ship to users code that is compatible in older browsers. Babel has become...
Read more >
How to load polyfills only when needed
I know three ready-to-use approaches for that: polyfill.io; the module / nomodule pattern; the useBuiltIns option in @babel/preset-env ...
Read more >
Make `Math.trunc` polyfill optional? · Issue #16144 · nim-lang/Nim ...
Summary There is a Math.trunc polyfill that goes in every Nim JS file (except if -d:nodejs is passed) for IE support. It should...
Read more >
Container queries begin to land in stable browsers while the ...
This polyfill only works with inline and same-origin CSS. Cross origin stylesheets, and stylesheets in iframes (unless a polyfill is manually ...
Read more >
Alternative or polyfill for Array.from on the Internet Explorer
The OP basically needs to create simple array from his array-like object. ... While it's not supported on IE, you may use the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found