lazy-loaded `<image>` in `<symbol>` always loads

the issue is simple: i have an svg with a bunch of <symbol> that have an <image> tag with loading="lazy". all images in the <symbol>s load regardless of <use>ing the <symbol> or not. https://jsfiddle.net/cqugnLb9/ <-- disable the cache in f12 and check the result i even tried the content-visibility rule, but doesn't work. how can i stop this from happening, without javascript?
Edit fiddle - JSFiddle - Code Playground
Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code editor.
112 Replies
ἔρως
ἔρωςOP16mo ago
without javascript would be extremely useful, since i will be using this for lots of small images (about 200), and dont need that kind of load some of the target devices (cheaper phones and 3+ year old iphones)
Jochem
Jochem16mo ago
as far as I can tell, loading isn't a valid attribute for the SVG image element
ἔρως
ἔρωςOP16mo ago
according to mdn, when inside an html file, the <image> tag is treated as an alias of <img> which has that attribute
Jochem
Jochem16mo ago
you mean this note?
Note: The HTML spec defines <image> as a synonym for <img> while parsing HTML. This specific element and its behavior only apply inside SVG documents or inline SVGs.
ἔρως
ἔρωςOP16mo ago
yes
Jochem
Jochem16mo ago
I read that as "you can write <img> or <image> in HTML, and it will work the same. This article doesn't describe that usage, but describes the separate <image> tag used inside of SVG" as in, I could use <image> outside of <svg></svg>, and it will act like an <img> tag, but inside an SVG it acts like the SVG <image> tag which is described in the article
ἔρως
ἔρωςOP16mo ago
that actually makes a lot of sense oh, well, i guess i have to implement this in javascript
Mirza
Mirza16mo ago
loading="lazy" will work when we scroll to the bottom. on top images will already loaded
ἔρως
ἔρωςOP16mo ago
i tried it, it doesnt work no matter where i put
Mirza
Mirza16mo ago
open dev tools go to the network section then scroll to that part where image is there you will notice that image is getting loade when you scrool/reach to that part
Jochem
Jochem16mo ago
did you open the jsfiddle? It's a single image with a second hidden using CSS there's nothing to scroll
Mirza
Mirza16mo ago
Ohh that will not work html loading="lazy" will only works when you scrool to that section you need js to switch between images
ἔρως
ἔρωςOP16mo ago
its not just that its an image that is inside a symbol (aka - not in the dom) but it is still being downloaded anyways if you check the network tab in f12, you see 2 images loading while 1 isnt in use at all but it is still loaded
Mirza
Mirza16mo ago
bro what's you goal?
Jochem
Jochem16mo ago
it still prevents display: none; images from being loaded, even if they would otherwise be on screen.
ἔρως
ἔρωςOP16mo ago
my goal is that the image that isnt being used in an <use> tag isnt downloaded at all
Mirza
Mirza16mo ago
should not download ?
ἔρως
ἔρωςOP16mo ago
i think it shouldnt i think that it should only download when you repeat the non-hidden svg and use the kitty1 image
Mirza
Mirza16mo ago
showing one on top but should get download other?
ἔρως
ἔρωςOP16mo ago
you see the 2 <symbol> tags?
Mirza
Mirza16mo ago
yeah
ἔρως
ἔρωςOP16mo ago
those indicate a piece of svg that you can then refer to with <use> and as far as i know, they arent rendered
Mirza
Mirza16mo ago
then will render why they won't render?
ἔρως
ἔρωςOP16mo ago
mdn describes the symbol as a graphical template object that then it is instantiated by an use tag so, until a use tag refers to it, the symbol shouldnt be rendered that's what i understand however, the 2 images are downloaded anyways, even if only 1 of the symbols has an use tag associated to it
Mirza
Mirza16mo ago
you rapping that both img in svg tag
ἔρως
ἔρωςOP16mo ago
yes and mdn says that the symbol isnt to be rendered, only the use tags are rendered
Mirza
Mirza16mo ago
yeah
ἔρως
ἔρωςOP16mo ago
but the image is still downloaded anyways and the lazy loading doesnt work (but he already explained a very likely reason why)
Mirza
Mirza16mo ago
actually img is getting downloaded not not visible in my case
ἔρως
ἔρωςOP16mo ago
thats the problem: its not supposed to download it i think i cant see that, im on my phone
Mirza
Mirza16mo ago
ok you did mistake
ἔρως
ἔρωςOP16mo ago
which one?
Mirza
Mirza16mo ago
now it's not downloading it
ἔρως
ἔρωςOP16mo ago
cache if it caches the image, it doesnt download it
Mirza
Mirza16mo ago
not image that should be img in tag
<img href="https://placekitten.com/200/300" width="200" height="300" loading="lazy" />

<img href="https://placekitten.com/250/350" width="250" height="350" loading="lazy" />

<svg width="200" height="300">
<use href="#kitty2" />
</svg>
<img href="https://placekitten.com/200/300" width="200" height="300" loading="lazy" />

<img href="https://placekitten.com/250/350" width="250" height="350" loading="lazy" />

<svg width="200" height="300">
<use href="#kitty2" />
</svg>
like this
ἔρως
ἔρωςOP16mo ago
that doesnt work then i cant use it
Mirza
Mirza16mo ago
now it's following mdn what you can't use?
ἔρως
ἔρωςOP16mo ago
because i need to use it multiple times in the same page and sometimes, a dummy link that's also in another svg that's why im using svg symbols so i can just <use> the ones i need
Jochem
Jochem16mo ago
is there any reason you can't just use img tags directly?
ἔρως
ἔρωςOP16mo ago
yes, re-use of symbols is one
Jochem
Jochem16mo ago
but multiple instances of the same img URL will just cache
ἔρως
ἔρωςOP16mo ago
but its not just images
Jochem
Jochem16mo ago
are you adding extra bits using the SVG or something?
Mirza
Mirza16mo ago
so just add img by css use that css class where you want that img
ἔρως
ἔρωςOP16mo ago
for example, i have a dummy icon that i want to use when there arent icons for it that is inside an svg
Mirza
Mirza16mo ago
yeah that will be under lazy loading
ἔρως
ἔρωςOP16mo ago
at rendering time, i wont always know what i will need
Jochem
Jochem16mo ago
so then you use the SVG for the dummy icons and the images for the regular icons?
ἔρως
ἔρωςOP16mo ago
i have other code that relies on this as well for the page that this is hurting the most, i will have to load about 320 icons i see that i will likely have to re-write everything in javascript and load the data over json
Mirza
Mirza16mo ago
ohk whats the size of img's
ἔρως
ἔρωςOP16mo ago
im seeing the html ballooning to almost 23kb, and i still have to render a load of other elements 752 bytes to 279kb
Mirza
Mirza16mo ago
physical size?
ἔρως
ἔρωςOP16mo ago
svg doesnt have that concept the external images are svgs
Mirza
Mirza16mo ago
and you don't want to use single line of js?
ἔρως
ἔρωςOP16mo ago
want? no but, i see i will have to
Mirza
Mirza16mo ago
did you try svg in alt=""
ἔρως
ἔρωςOP16mo ago
what do you mean?
Mirza
Mirza16mo ago
you are adding loading="lazy" which have to prevent download img until you reach there but in our case it's not working you can use alt="" tag as alt img until we reach there
ἔρως
ἔρωςOP16mo ago
yes, because i probably misunderstood how the image tag works in svg you cant "reach there" to something that doesnt exist
Mirza
Mirza16mo ago
wtf that's why i was asking physical size of images
ἔρως
ἔρωςOP16mo ago
the <symbol> is the same-ish at a <template> it isnt rendered unless an <use> element has the link attribute pointing to the id of the <symbol>
Mirza
Mirza16mo ago
yeah then use img's in alt tag only
ἔρως
ἔρωςOP16mo ago
therefore, it basically isnt "there" in the document there's no alt tag in svg or html
Mirza
Mirza16mo ago
No description
Mirza
Mirza16mo ago
in my case only one img is getting load not both
ἔρως
ἔρωςOP16mo ago
but you edited the code and it no longer uses svg
Mirza
Mirza16mo ago
no i'm using svg now
ἔρως
ἔρωςOP16mo ago
can you show me your code?
Mirza
Mirza16mo ago
1 sec
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>

<body>

<svg hidden>
<!-- kitty not in use, but loads anyway -->
<symbol id="kitty1" viewBox="0 0 250 350">
<image href="https://placekitten.com/250/350" width="250" height="350" />
</symbol>

<!-- kitty in use -->
<symbol id="kitty2" viewBox="0 0 200 300">
<image href="https://placekitten.com/200/300" width="200" height="300" loading="lazy" alt="" />
</symbol>
</svg>

<svg width="200" height="300">
<use href="#kitty2"/>
</svg>
</body>

</html>
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>

<body>

<svg hidden>
<!-- kitty not in use, but loads anyway -->
<symbol id="kitty1" viewBox="0 0 250 350">
<image href="https://placekitten.com/250/350" width="250" height="350" />
</symbol>

<!-- kitty in use -->
<symbol id="kitty2" viewBox="0 0 200 300">
<image href="https://placekitten.com/200/300" width="200" height="300" loading="lazy" alt="" />
</symbol>
</svg>

<svg width="200" height="300">
<use href="#kitty2"/>
</svg>
</body>

</html>
ἔρως
ἔρωςOP16mo ago
let me try that
ἔρως
ἔρωςOP16mo ago
i cant reproduce your results
No description
ἔρως
ἔρωςOP16mo ago
im working from another pc with the usb ports disabled
Mirza
Mirza16mo ago
k k
ἔρως
ἔρωςOP16mo ago
even firefox has the same behavior
No description
Mirza
Mirza16mo ago
image should available in view port and should not get download what you want broooo
ἔρως
ἔρωςOP16mo ago
i want it to work, but its fine i will change my approach to this
Mirza
Mirza16mo ago
wait let me know what you want i'm i correct? html
<div class="image-container">
<img src="your-svg-image.svg" alt="SVG Image" data-load="true">
</div>
<div class="image-container">
<img src="your-svg-image.svg" alt="SVG Image" data-load="true">
</div>
css
.image-container img[data-load="false"] {
display: none;
}
.image-container img[data-load="false"] {
display: none;
}
js
// Function to enable or disable image loading
function toggleImageLoading() {
const image = document.querySelector('.image-container img');
const currentLoadValue = image.getAttribute('data-load');

if (currentLoadValue === 'true') {
image.setAttribute('data-load', 'false');
} else {
image.setAttribute('data-load', 'true');
}
}
// Function to enable or disable image loading
function toggleImageLoading() {
const image = document.querySelector('.image-container img');
const currentLoadValue = image.getAttribute('data-load');

if (currentLoadValue === 'true') {
image.setAttribute('data-load', 'false');
} else {
image.setAttribute('data-load', 'true');
}
}
your img activating button
<button onclick="toggleImageLoading()">Toggle Image Loading</button>
<button onclick="toggleImageLoading()">Toggle Image Loading</button>
give it a try
ἔρως
ἔρωςOP16mo ago
that's not what i want
Mirza
Mirza16mo ago
what you want ????
ἔρως
ἔρωςOP16mo ago
i just want that the unused image isnt downloaded
Mirza
Mirza16mo ago
but you are using it bro
ἔρως
ἔρωςOP16mo ago
no, 1 of the 2 images is being used, but 2 images are downloaded
Mirza
Mirza16mo ago
need to contact mdn raise your query at mdn
ἔρως
ἔρωςOP16mo ago
i think im trying to milk beer from a rock i will reimplement what i need in javascript
Mirza
Mirza16mo ago
go on bro
ἔρως
ἔρωςOP16mo ago
this is going to be used on a page with maybe 1000 checkboxes (i didnt design this) and the html is already very bloated with less than 300 checkboxes
Mirza
Mirza16mo ago
lol on which website we use 1000 checkbox
ἔρως
ἔρωςOP16mo ago
its for people to enable and disable stuff
Mirza
Mirza16mo ago
are you making game by checkbox
ἔρως
ἔρωςOP16mo ago
no its something for work
Mirza
Mirza16mo ago
if done and if you can share with us please want to know what was that btw do you know how to config webpack @epic2937 @Jochem
Jochem
Jochem16mo ago
@mirza2282 make your own post please, don't highjack others' posts webpack would probably go in #os-and-tools
Mirza
Mirza16mo ago
ohk sorry and thanks
dys 🐙
dys 🐙16mo ago
Have you tried:
<foreignObject>
<img xmlns="http://www.w3.org/1999/xhtml" loading="lazy"
src="myimg.png"/>
</foreignObject>
<foreignObject>
<img xmlns="http://www.w3.org/1999/xhtml" loading="lazy"
src="myimg.png"/>
</foreignObject>
ἔρως
ἔρωςOP16mo ago
no, i haven't HOLY that actually works
Mirza
Mirza16mo ago
Suck You told only you want that in svg tags And image yag Tag* <image>?<img>
ἔρως
ἔρωςOP16mo ago
that there is valid svg i tried to replicate this into my environment and it didnt work i then tried to use it in the jsfiddle and i can replicate it there, again i did some research and i was a bonehead idiot apparently, images and foreignobject dont always mesh well i will need to check how discord is doing it and replicate it
Jochem
Jochem16mo ago
Pretty sure Discord just uses img tags for PFPs, icons, and emoji
ἔρως
ἔρωςOP16mo ago
no, they use an svg with a foreignobject
Jochem
Jochem16mo ago
for what?
ἔρως
ἔρωςOP16mo ago
profile pictures all of them
Jochem
Jochem16mo ago
really?
No description
ἔρως
ἔρωςOP16mo ago
and the parent elements?
Jochem
Jochem16mo ago
a div and an li the entire chat is an ol, with li's for each message, and then a div wrapper, an img for the avatar, an h3 for the username and timestamp (wrapped in some spans and a time element), and a separate div for the message content as a sibling for the avatar/username wrapper if you're doing avatars, you should just include the avatar URL with your message JSON and populate an img. The browser will cache them for you
ἔρως
ἔρωςOP16mo ago
i will re-check later on, because they were using an svg i was wrong, only the profile image uses an svg when you click on someone
Jochem
Jochem16mo ago
and even then I think it's just so that they can easily add the status icon
ἔρως
ἔρωςOP16mo ago
yes i figured that what i want to do isnt possible anyways the foreign object tag is ignored when inside a symbol, even though mdn says it is allowed according to the spec, this MUST work
ἔρως
ἔρωςOP16mo ago
Edit fiddle - JSFiddle - Code Playground
Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code editor.
ἔρως
ἔρωςOP16mo ago
https://svgwg.org/svg2-draft/struct.html#SymbolElement <-- the spec i will reimplement this with intersection observers and other stuff
dys 🐙
dys 🐙16mo ago
Apparently you can't <use> a <foreignObject>. This also doesn't work:
<svg viewBox="0 0 200 300">
<defs>
<g id="kitty">
<foreignObject width="200" height="300">
<img xmlns="http://www.w3.org/1999/xhtml" src="https://placekitten.com/200/300" />
</foreignObject>
</g>
</defs>
<use href="#kitty"/>
</svg>
<svg viewBox="0 0 200 300">
<defs>
<g id="kitty">
<foreignObject width="200" height="300">
<img xmlns="http://www.w3.org/1999/xhtml" src="https://placekitten.com/200/300" />
</foreignObject>
</g>
</defs>
<use href="#kitty"/>
</svg>
ἔρως
ἔρωςOP16mo ago
yeah, i just gave up, and wrote everything in javascript
Mirza
Mirza15mo ago
hehe
Want results from more Discord servers?
Add your server