大网数据广告活动
网站运营与故障问答

How to improve extremely slow page load time on a 23MB web page full of SVGs?

· 52CE 我爱测

I have a webpage that is full of inline-SVG around 140+ inline SVGs! Just the HTML file size is 23MB, plus there is external CSS and JavaScript loading, though that is roughly only around 30KB. The real deal is the HTML file itse…

问题原文

原帖链接 · Syed M. Sannan - alt · CC BY-SA 4.0

I have a webpage that is full of inline-SVG around 140+ inline SVGs! Just the HTML file size is 23MB, plus there is external CSS and JavaScript loading, though that is roughly only around 30KB. The real deal is the HTML file itself with thousands of lines of code mostly consisting of SVGs.

My webpage takes 2-3 minutes to load up on my (probably) average internet, how can I reduce the loading time while especially reducing the file size of the HTML file? I don't think using separate SVG files for the images would be any better because that would take even more time to load because of separate HTTP requests.

Is there any way for me to shorten the loading time from the current 2-3 minutes to under 5 seconds?

A small screenshot from the Networks panel in Chrome Dev Tools:

已采纳解答

原帖链接 · Maximillian Laumeister · CC BY-SA 4.0

From what you've stated in the comments (now chat), I understand that you cannot reduce the size of these SVG files, you cannot replace them with thumbnails (e.g. small png files), and you cannot split them up onto multiple pages. In other words you are stuck with a web page that has 23MB worth of SVG files on it, and there's no way to change that.

That leaves you with only a few options left:

  1. Make sure your server is properly compressing these files using gzip or brotli compression. You can check whether your server is properly compressing these files by following this Stack Overflow answer. SVGs are highly compressible, so this is a critical step. This alone could improve loading time by over 50%, if your server is not already doing this.

  2. Make sure these SVG files are properly optimized and minified, using something like svgo (and SVGOMG gui). This could save maybe another 20-40%.

  3. Consider linking the SVG files externally by using img tags, and use the loading: lazy directive on them. Then, at least the loading of the images will be deferred until the user scrolls down the page. (If, as you stated in the comments, your images are not initially visible in the viewport but are later shown dynamically by JavaScript, loading: lazy should still prevent them from loading until they are viewed, because it "Defers loading the image until it reaches a calculated distance from the viewport, as defined by the browser." Alternatively, you can modify your JavaScript to prevent the SVG tag from loading in at all until the script is triggered.)

If you link the SVG files externally by using img tags, that also has the added benefit of letting your HTML load to completion before the SVG files begin loading in. So parts of your HTML that are located below the SVG embeds in the document, like the footer, will load in before the SVGs finish.

To answer your concern about external SVG files generating separate HTTP requests that increase load time, this is not something you should need to worry about if your server supports HTTP/2 (so follow this Stack Overflow solution to double check that it's enabled). If so, all requests will be multiplexed over a single TCP connection.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2021-09-20)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。