压缩静态资源是节省带宽和改善用户体验的好方法。这也是加快页面加载速度的最简单方法,这会对SEO产生积极影响。最佳且常用的压缩方式是GZIP。默认情况下,Spring Boot禁用了这种压缩,但是幸运的是,我们可以通过在application.properties文件中设置以下参数来启用它。
# Whether response compression is enabled. server.compression.enabled=<b>true</b> # List of user-agents to exclude from compression. server.compression.excluded-user-agents= # Comma-separated list of MIME types that should be compressed. Default mime-types are: text/html, text/xml, text/plain, text/css server.compression.mime-types=text/html,text/xml,text/plain,text/css,text/javascript,application/javascript # Minimum <font>"Content-Length"</font><font> value that is required <b>for</b> compression to be per<b>for</b>med. server.compression.min-response-size=2048 </font>
如果您使用的是YAML配置文件,那么这些属性将如下所示:
server: compression: enabled: <b>true</b> excluded-user-agents: mime-types: text/html,text/xml,text/plain,text/css,text/javascript,application/javascript min-response-size: 2048
要了解所有配置是否正确,请检查响应头以了解选择压缩的文件。应该有Content-Encoding带gzip值的标头,并且这些文件的大小一定要比压缩前小。