Vendor workbox

This commit is contained in:
Ken-Håvard Lieng 2018-11-08 09:39:32 +01:00
parent 9a5d7f8360
commit 9b6844449d
4 changed files with 343 additions and 32 deletions

File diff suppressed because one or more lines are too long

View File

@ -66,13 +66,13 @@ function fonts() {
function compress() { function compress() {
return gulp return gulp
.src(['dist/!(*.toml|*.json)']) .src(['dist/**/!(*.dev.js|*.map|*.toml|*.json|*.woff|*.woff2)'])
.pipe(brotli({ quality: 11 })) .pipe(brotli({ quality: 11 }))
.pipe(gulp.dest('dist')); .pipe(gulp.dest('dist'));
} }
function cleanup() { function cleanup() {
return del(['dist/*(*.js|*.css)']); return del(['dist/**/*(*.js|*.css|*.map)']);
} }
function bindata(cb) { function bindata(cb) {

View File

@ -84,6 +84,7 @@ module.exports = {
}), }),
new InjectManifest({ new InjectManifest({
swSrc: './src/js/sw.js', swSrc: './src/js/sw.js',
importWorkboxFrom: 'local',
globDirectory: './src', globDirectory: './src',
globPatterns: ['font/*.woff2'], globPatterns: ['font/*.woff2'],
exclude: [ exclude: [

View File

@ -143,23 +143,11 @@ func (d *Dispatch) initFileServer() {
files = append(files, file) files = append(files, file)
} }
fonts, err := assets.AssetDir("font") root, _ := assets.AssetDir("")
if err != nil { for _, asset := range root {
log.Fatal(err) if _, err = assets.AssetDir(asset); err == nil {
loadDir(asset)
} }
for _, font := range fonts {
p := strings.TrimSuffix(font, ".br")
file := &File{
Path: path.Join("font", p),
Asset: path.Join("font", font),
ContentType: contentTypes[filepath.Ext(p)],
CacheControl: longCacheControl,
Compressed: strings.HasSuffix(font, ".br"),
}
files = append(files, file)
} }
for _, file := range files { for _, file := range files {
@ -200,6 +188,27 @@ workbox.routing.registerNavigationRoute('/?sw');`)...)
} }
} }
func loadDir(dirName string) {
dir, err := assets.AssetDir(dirName)
if err != nil {
log.Fatal(err)
}
for _, asset := range dir {
assetName := strings.TrimSuffix(asset, ".br")
file := &File{
Path: path.Join(dirName, assetName),
Asset: path.Join(dirName, asset),
ContentType: contentTypes[filepath.Ext(assetName)],
CacheControl: longCacheControl,
Compressed: strings.HasSuffix(asset, ".br"),
}
files = append(files, file)
}
}
func decompressAsset(data []byte) []byte { func decompressAsset(data []byte) []byte {
br, err := brotli.NewReader(bytes.NewReader(data), nil) br, err := brotli.NewReader(bytes.NewReader(data), nil)
if err != nil { if err != nil {