SVG stands for Scalable Vector Graphics. Literally, it’s a graphic format that is scalable, jaggy free and compact (in most cases).
Most draw tools natively support loading and saving SVG files. You’ll find the format useful when you’re annoyed with jaggy images on your corporate web site.
Today I replaced a png logo on our internal system with an SVG file.
pngからsvgにしたからサイズも小さく・・・と思ったら18.26kbから431kbと激増してるじゃないか pic.twitter.com/K4M7KA0zst
— ユースケ (山本裕介) (@yusuke) 2020年8月5日
The file size was supposed to be shrunk, but the truth is that it grew from 18.26kb to 431kb. Then I was reminded of the compressed SVG format which Illustrator is offering. And the file size got minified to 3,688 bytes.
But it doesn’t get rendered correctly.
svgzにしたら3,688バイトと激減したけど、レスポンスヘッダで
Content-Encoding: gzip
が付いておらずレンダリングちゃんとされない。Webサーバで設定が必要かな pic.twitter.com/p77iAh0ZlC— ユースケ (山本裕介) (@yusuke) 2020年8月5日
It tuns out that Spring Boot and the Web server do not specify the Content-Encoding header to gzip OOTB.
I might be able to configure that on Nginx side, but in order to get the image loaded correctly on my local environment as well, I implemented a very simple ServletFilter.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright 2020 Samuraism Inc. | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
import org.springframework.stereotype.Component; | |
import javax.servlet.FilterChain; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpFilter; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.io.IOException; | |
@Component | |
public class SVGZFilter extends HttpFilter { | |
@Override | |
public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) | |
throws IOException, ServletException { | |
if (request.getRequestURI().endsWith(".svgz")) { | |
response.setHeader("Content-Encoding","gzip"); | |
} | |
chain.doFilter(request, response); | |
} | |
} |
And now it works 🙂