admin管理员组文章数量:1318156
I have a basic Service worker script that caches my fonts.css file.
The service worker are getting registered on page load in my index.html file but I am also loading the fonts.css file directly in my index.html.
Is it right that the service worker file will intercept the request and responds with the cached fonts.css file?
Or does caching the fonts.css file has no effect as the Browser still has to request the listed fonts?
I believe the latter is whats going on but it would be great if someone could clear thing up for me. Would be the best approach to cache the whole fonts folder not only the fonts.css file?
I have a basic Service worker script that caches my fonts.css file.
The service worker are getting registered on page load in my index.html file but I am also loading the fonts.css file directly in my index.html.
Is it right that the service worker file will intercept the request and responds with the cached fonts.css file?
Or does caching the fonts.css file has no effect as the Browser still has to request the listed fonts?
I believe the latter is whats going on but it would be great if someone could clear thing up for me. Would be the best approach to cache the whole fonts folder not only the fonts.css file?
Share Improve this question edited Feb 5, 2019 at 8:36 Niklas asked Mar 29, 2018 at 9:50 NiklasNiklas 1,30919 silver badges35 bronze badges1 Answer
Reset to default 8The Service Worker will need to cache the CSS file and the associated font files (.woff2, .ttf, etc) specified within that CSS file that the browser loads.
For example, when using Google Fonts Open Sans CSS file, various URLs list the font files (URLs differ based on the browser you're using and font types supported).
...
@font-face {
src: url(https://fonts.gstatic./s/opensans/v15/memnYaGs126MiZpBA-UFUKWyV9hvIqOxjaPXZSk.woff2);
src: url(https://fonts.gstatic./s/opensans/v15/memnYaGs126MiZpBA-UFUKWyV9hnIqOxjaPXZSk.woff2);
}
...
In this example, your Service Worker could cache all files that match domains "fonts.googleapis." and "fonts.gstatic."
self.addEventListener('fetch', event => {
if (/fonts.(googleapis|gstatic)./.test(event.request.url)) {
// process caching font files
}
})
本文标签: javascriptCachingOptimising Fonts with Service WorkerStack Overflow
版权声明:本文标题:javascript - CachingOptimising Fonts with Service Worker - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742043187a2417641.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论