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.

Polyfill without <noscript>

See original GitHub issue

Hi, is ist somehow possible to use this polyfill without adding a <noscript> wrapper?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:15 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
oyeharrycommented, Jan 2, 2022

I was having a similar issue while integrating this polyfill with React. However, I was able to solve the issue by doing the following. It works well on SSR and CSR with React 16.8.x and should work with 17.x.x too. Thank you for the beautiful polyfill.

import React, { useEffect, useRef } from 'react';
import ReadDOMServer from 'react-dom/server';
import loadingAttributePolyfillApi from 'loading-attribute-polyfill';

export function LoadingAttrPolyfill(props) {
  const { children } = props;
  const staticMarkup = ReadDOMServer.renderToStaticMarkup(children);
  const noScriptRef = useRef(null);

  useEffect(() => {
    if (noScriptRef.current) {
      loadingAttributePolyfillApi.prepareElement(noScriptRef.current);
    }
  }, []);

  return <noscript ref={noScriptRef} dangerouslySetInnerHTML={{ __html: staticMarkup }} />;
}

export default function ImageCarousel() {
  return (
    <div>
      <LoadingAttrPolyfill>
        <img src='https://picsum.photos/200/300' alt='Random 1' />
      </LoadingAttrPolyfill>
      <LoadingAttrPolyfill>
        <picture>
          <source
            width='846'
            height='480'
            srcset='https://picsum.photos/846/480'
            media='(min-width: 768px)'
          />
          <img width='375' height='250' src='https://picsum.photos/375/250' alt='Random 2' />
        </picture>
      </LoadingAttrPolyfill>
    </div>
  );
}
1reaction
oyeharrycommented, Jan 2, 2022

@mfranzke Exactly. I will open a PR soon for this update. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Polyfilling picture without the overhead - W3C
* The noscript idea here isn't entirely new… Head London cleverly proposed it a while back. For some reason, it feels a little...
Read more >
polyfill.io: Security and Privacy Info - NoScript
This service is experimental and far from being complete yet. Currently it provides links to other resources helping to assess the security and...
Read more >
An easier way of using polyfills | Hacker News
User agents allow them to settle on a list of known needed polyfills without any code needed for the user beyond a simple...
Read more >
Polyfill JavaScript Only When You Need To - CSS-Tricks
Here the standard procedure: Write ES6 → compile everything down to ES5 (e.g. Babel) → send that to the browser. This may not...
Read more >
Using Polyfills to Code with JavaScript - Dummies.com
Learn how you can use polyfills in JavaScript coding when you're dealing with browsers that can't accept certain HTML tags or APIs.
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