-
Notifications
You must be signed in to change notification settings - Fork 44
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
Polyfill without <noscript> #94
Comments
@vincentschroeder thanks a lot for your question. Sadly JavaScript seems the only possibility to hinder the images from being loaded at the very beginning - therefor it's necessary to manipulate an Therefor manipulating that |
Easy. And Variant 1 is correct, compare to the source code on the demo page: https://mfranzke.github.io/loading-attribute-polyfill/demo/ |
Thanks. Unfortunately, I discovered now that the polyfill is not working in SPA |
Do you have the chance to provide an example URL, or a jsfiddle? |
@vincentschroeder I could think about to expose another method publicly that would receive some DOM node only to prepare and return it depending on browser capabilities check / feature detection, either to return it directly in case of that the browser is feasible to handle the loading-attribute natively even already, or to return it with the relevant optimizations and bindings regarding IntersectionObserver. Then anyone could easily leave out the |
Hi @mfranzke, I have the issue when I'm using this in React, because it gives me this error: This is the code I have (I've only changed the default code from React):
and trying to trigger the
What am I doing wrong? |
@cyberdelphos, thanks a lot for your feedback. I'm not that fit in react - do you have the chance to prepare a JS Bin, JSFiddle or similar test environment where I could reproduce this? |
@vincentschroeder (and probably @cyberdelphos) You will need to write your noscript using
worth to mention: this lib works on Client side only. as soon as you import the javascript file, it'll run some lines accessing Sorry for the heavy react-oriented comment in here @mfranzke - Having said that, perhaps these lines (L25:L47) could be moved into |
I seem to be experiencing a similar issue as Svelte blocks out anything in tags |
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 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>
);
} |
Thanks a lot @oyeharry, you‘re solution looks quite nice (from my limited knowledge about React). |
@mfranzke It seems that we will get browser global errors from the polyfill if we try to run the above code on the SSR(NodeJS). For now, as a workaround, we have to load polyfill like the following. Will you be interested if I open a PR with some defense for browser global so that they do not execute on the server? For instance, we will only execute window, HTMLImageElement, HTMLIFrameElement, and document if we are in the browser environment or maybe run the whole script only in the browser by adding a top-level window check. import React, { useEffect, useRef } from 'react';
import ReadDOMServer from 'react-dom/server';
let loadingAttributePolyfillApi;
if (typeof window === 'object') {
/**
* import it only in the browser as this polyfill trying to execute globals
* which are only available in the browser.
*/
loadingAttributePolyfillApi = require('loading-attribute-polyfill').default;
}
export function LoadingAttrPolyfill(props) {
const { children } = props;
const staticMarkup = ReadDOMServer.renderToStaticMarkup(children);
const noScriptRef = useRef(null);
useEffect(() => {
if (noScriptRef.current && loadingAttributePolyfillApi) {
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>
);
} |
@oyeharry sounds good to me, thanks a lot for your support ! From my point of view there‘s nothing that I remember that could be beneficial to get executed on the server, as it‘s mainly about detecting and fixing browser capabilities, for which you would need that environment. |
@mfranzke Exactly. I will open a PR soon for this update. Thanks! |
Hi, is ist somehow possible to use this polyfill without adding a
<noscript>
wrapper?The text was updated successfully, but these errors were encountered: