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
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)
as far as I can tell,
loading
isn't a valid attribute for the SVG image
elementaccording to mdn, when inside an html file, the
<image>
tag is treated as an alias of <img>
which has that attributeyou 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.
yes
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
that actually makes a lot of sense
oh, well, i guess i have to implement this in javascript
loading="lazy" will work when we scroll to the bottom. on top images will already loaded
i tried it, it doesnt work no matter where i put
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
did you open the jsfiddle? It's a single image with a second hidden using CSS
there's nothing to scroll
Ohh that will not work
html loading="lazy" will only works when you scrool to that section you need js to switch between images
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
bro what's you goal?
it still prevents
display: none;
images from being loaded, even if they would otherwise be on screen.my goal is that the image that isnt being used in an <use> tag isnt downloaded at all
should not download ?
i think it shouldnt
i think that it should only download when you repeat the non-hidden svg and use the kitty1 image
showing one on top but should get download other?
you see the 2
<symbol>
tags?yeah
those indicate a piece of svg that you can then refer to with
<use>
and as far as i know, they arent renderedthen will render why they won't render?
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
you rapping that both img in svg tag
yes
and mdn says that the symbol isnt to be rendered, only the use tags are rendered
yeah
but the image is still downloaded anyways
and the lazy loading doesnt work (but he already explained a very likely reason why)
actually img is getting downloaded not not visible in my case
thats the problem: its not supposed to download it
i think
i cant see that, im on my phone
ok
you did mistake
which one?
now it's not downloading it
cache
if it caches the image, it doesnt download it
not image that should be img in tag
like this
that doesnt work
then i cant use it
now it's following mdn
what you can't use?
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 needis there any reason you can't just use img tags directly?
yes, re-use of symbols is one
but multiple instances of the same img URL will just cache
but its not just images
are you adding extra bits using the SVG or something?
so just add img by css use that css class where you want that img
for example, i have a dummy icon that i want to use when there arent icons for it
that is inside an svg
yeah that will be under lazy loading
at rendering time, i wont always know what i will need
so then you use the SVG for the dummy icons and the images for the regular icons?
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
ohk
whats the size of img's
im seeing the html ballooning to almost 23kb, and i still have to render a load of other elements
752 bytes to 279kb
physical size?
svg doesnt have that concept
the external images are svgs
and you don't want to use single line of js?
want? no
but, i see i will have to
did you try svg in alt=""
what do you mean?
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
yes, because i probably misunderstood how the image tag works in svg
you cant "reach there" to something that doesnt exist
wtf
that's why i was asking physical size of images
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>
yeah then use img's in alt tag only
therefore, it basically isnt "there" in the document
there's no alt tag in svg or html
in my case only one img is getting load
not both
but you edited the code
and it no longer uses svg
no i'm using svg now
can you show me your code?
1 sec
let me try that
i cant reproduce your results
im working from another pc with the usb ports disabled
k k
even firefox has the same behavior
image should available in view port and should not get download
what you want broooo
i want it to work, but its fine
i will change my approach to this
wait
let me know what you want
i'm i correct?
html
css
js
your img activating button
give it a try
that's not what i want
what you want
????
i just want that the unused image isnt downloaded
but you are using it bro
no, 1 of the 2 images is being used, but 2 images are downloaded
need to contact mdn
raise your query at mdn
i think im trying to milk beer from a rock
i will reimplement what i need in javascript
go on bro
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
lol
on which website we use 1000 checkbox
its for people to enable and disable stuff
are you making game by checkbox
no
its something for work
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
@mirza2282 make your own post please, don't highjack others' posts
webpack would probably go in #os-and-tools
ohk
sorry
and thanks
Have you tried:
no, i haven't
HOLY
that actually works
Suck
You told only you want that in svg tags
And image yag
Tag*
<image>?<img>
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
Pretty sure Discord just uses img tags for PFPs, icons, and emoji
no, they use an svg with a foreignobject
for what?
profile pictures
all of them
really?
and the parent elements?
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
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
and even then I think it's just so that they can easily add the status icon
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
https://jsfiddle.net/5roy3x2z/ <-- doesnt work
Edit fiddle - JSFiddle - Code Playground
Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle code editor.
https://svgwg.org/svg2-draft/struct.html#SymbolElement <-- the spec
i will reimplement this with intersection observers and other stuff
Apparently you can't
<use>
a <foreignObject>
. This also doesn't work:
yeah, i just gave up, and wrote everything in javascript
hehe