What is the best method for caching inline SVG in web browsers?

Over the years, SVGs have remained popular due to their scalability. One key advantage of inline SVG is the ability to manipulate it with CSS and JS. Additionally, using the <use> tag allows for referencing the original element when repeating the same SVG in an HTML document, ultimately reducing the number of HTTP requests.

However, there are suggestions (without detailed explanations) that imply while using inline SVG may save on HTTP requests, it may not be cacheable as a separate entity by browsers - making it less reusable across different pages.

Given my extensive use of inline SVG for a project, I am keen to understand how this renowned html5 element can be cached within browsers, especially compared to SVGs used with the <img> tag or as background-image, which are cacheable.

If the DOM is cacheable, why can't the SVG DOM be as well? (The SVG DOM is built upon and compatible with DOM Level 2. Reference: https://www.w3.org/TR/SVG/svgdom.html)

One approach I have considered for cacheability is utilizing the Data URI scheme. However, this method sacrifices the ability to handle CSS and JS for styling and manipulation.

Various suggestions online propose using JavaScript to load cacheable resources, replacing placeholder elements like the <object> tag, and leveraging tools such as localStorage, CacheStorage, and Service Worker. Despite these options, guidance on achieving an ideal solution would be greatly appreciated.

Could someone provide further insight?

-

-

-

Reference: Caching SVG Sprite in localStorage

Reference: Inline SVG and caching

Reference: SVG ON THE WEB

Reference: Do Inline SVGs Weigh Down Websites?

Answer №1

When it comes to basic HTTP caching, the system operates on a URL basis with a simple rule - either the entire resource is taken from cache or it is reloaded entirely.

By "inlining" your SVGs, you are effectively merging them into the HTML document itself, eliminating their status as separate external resources that can be individually cached or reloaded.

For example, if you have three HTML pages all containing the same inlined SVG image, the code for that image will be loaded three times because it is part of each individual HTML document.

However, if the image was embedded as an external resource (such as img, background-image, object, etc.), it would only need to be loaded once on the first HTML page the browser loads. Subsequent pages would recognize that the external resource is already in the cache and does not need to be reloaded again.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilize JavaScript to trigger a div pop-up directly beneath the input field

Here is the input box code: <input type='text' size='2' name='action_qty' onmouseup='showHideChangePopUp()'> Along with the pop-up div code: <div id='div_change_qty' name='div_change_qty&ap ...

Ways to adjust flex division to occupy the entire height of its container

https://i.sstatic.net/4YInI.png https://i.sstatic.net/hu8mG.png https://i.sstatic.net/Q51ha.png I am looking to activate an onClick event on the body where discussions and icons are located. While triggering onClick on the body itself poses no issue, I a ...

Altering the properties of every item within a v-for loop's array

I'm currently exploring Vue's approach to writing JavaScript. Let's consider this situation: In the .vue template <button v-on:click="biggerFont()" class="btn btn-s btn-default" type="button" name="button">A</button> < ...

Try incorporating a variety of colors to enhance the appearance of your paper-menu in Polymer 1

I'm currently working on creating a customized menu using Polymer 1.0. This is how my menu structure looks like: <paper-menu> <template is="dom-repeat" items="{{menu}}" as="item"> <paper-item> <div>{{item.title}}</div& ...

In HTML, the size and position of an <a> element cannot be altered or adjusted

I am having trouble with an anchor element that is contained within a div. No matter what I try, I cannot resize or move it up or down without it covering its sibling element. I have attempted using transform-translate and margin properties, but to no avai ...

How can I show the post JSON response that was retrieved in ReactJS on the same form component that was used to submit the value?

Thank you for your assistance in advance. I am currently developing a web crawler that will operate in the following steps: The user inputs the seed URL (front-end) The user clicks the submit button (front-end) The seed URL is processed by the backend usi ...

An effective way to determine the size of a browser through javascript

In an effort to enhance the responsiveness of a website, I have included the following code snippet on one of my pages: document.write(screen.width+'x'+screen.height); However, I am encountering an issue where the code displays my screen resolu ...

What are the ways in which the "npm install <module>" command can be utilized in the creation of web pages for browsers?

When incorporating a resource into a web page, I have the option to either link to the file (locally, hosted by the web server, or on a CDN) <link rel="stylesheet" href="https://somecdn.com/mycssresource.min.css"> <script src="alocalscript.js"> ...

What is the best approach to implement pagination in the UI-Bootstrap Typeahead Directive?

In my current Angular Project, I am utilizing the UI-Bootstrap's Typeahead Directive for a specific purpose. However, I am encountering an issue when dealing with a large amount of similar data using this directive. It seems that displaying only the ...

Implementing pagination in a node.js application using pg-promise and fetching data from

Upon reviewing the documentation at https://github.com/vitaly-t/pg-promise/wiki/Data-Imports, I found a comprehensive guide on importing data using pg-promise. Although the example provided works for the showcased scenario, I am unsure how to adapt it to ...

I am trying to access a value saved in a service in Angular 8 component and use it in other services. Can anyone help

Setting a value through a component export class UniqueComponent { constructor(service:UniqueService){ } count =0 ; onRefresh(){ this.service.count = 1; } } Using the value in the service UniqueService{ count:any; doSomething(){ //using count ...

Tips for setting environmental variables to match ID values in html documents

In my API reference call, I have stored the key in a separate file and protected it using .gitignore since I am using GitHub. However, I am facing an issue on how to transfer that key from my JavaScript file into the HTML data-key attribute using a variab ...

Replacing a div with another div can be achieved in a single swap

My goal is to swap one div with another div upon clicking using the provided code. It functions properly for a single instance. However, the issue arises when the class appears multiple times, causing all instances to change due to sharing the same class n ...

NodeJS threw an error: The call stack size has exceeded the maximum limit

My tool of choice is socket.io Error Encountered: We are facing a RangeError while calling `socket.disconnect()`; Please help resolve this issue. Thank you! ...

When trying to click the button in Navbar.jsx, I encounter an error when attempting to invoke the setShowCart(true) function

I've encountered an issue while trying to call the setShowCart(true) function in Navbar.jsx. I'm unsure of how to fix this. import React from 'react' import Link from 'next/link'; import {AiOutlineShopping} from 'react-ic ...

Customizing Bootstrap tooltip appearances with CSS

After successfully setting up Bootstrap 4.5.0, I decided to experiment with customizing the styles of tooltips. The code snippet below shows how I attempted this. Initially, I included the necessary stylesheets at the top of the page: <link rel=&q ...

Arrange objects on a grid

I've got an array filled with objects, each containing 3 images, a name, and some details. I'm attempting to display these objects on Bootstrap cards, but currently each card is showing up on its own line. My goal is to arrange the cards in a gri ...

Boost the scrolling velocity of my sidebar using Jquery

Hello there, I am completely new to the world of web development and particularly to using JavaScript and jQuery. I am interested in learning how to adjust the speed at which my Sidebar scrolls down as the user navigates through the page. Here is the jQue ...

Jest does not recognize AnimationEvent as a defined element

I am currently facing an issue while attempting to simulate an animationEvent for a test within my Angular application. The error message I receive is: ReferenceError: AnimationEvent is not defined. Given that this feature is experimental, it seems like ...

Customize the MUI Slider Component with unique colored thumbs

Check out MUI's slider component at this link. I'm using it to let users select a value range with two thumbs. You can find the documentation for MUI's multi-thumb slider here. If you want to customize the style of the slider thumb, visit ...