mirror of
https://github.com/miniflux/v2.git
synced 2025-08-11 17:51:01 +00:00
perf(static): minimize the SVG
Since tdewolff/minify supports SVG minimization, let's make use of it. As we need to keep the license in the SVG because we're nice netizens, we can at least use SPDX identifiers instead of using it verbatim. This does save a couple of kB.
This commit is contained in:
parent
485baf9654
commit
68984da332
5 changed files with 41 additions and 72 deletions
|
@ -156,16 +156,16 @@ func Parse() {
|
||||||
slog.Info("The default value for DATABASE_URL is used")
|
slog.Info("The default value for DATABASE_URL is used")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := static.CalculateBinaryFileChecksums(); err != nil {
|
if err := static.GenerateBinaryBundles(); err != nil {
|
||||||
printErrorAndExit(fmt.Errorf("unable to calculate binary file checksums: %v", err))
|
printErrorAndExit(fmt.Errorf("unable to generate binary files bundle: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := static.GenerateStylesheetsBundles(); err != nil {
|
if err := static.GenerateStylesheetsBundles(); err != nil {
|
||||||
printErrorAndExit(fmt.Errorf("unable to generate stylesheets bundles: %v", err))
|
printErrorAndExit(fmt.Errorf("unable to generate stylesheets bundle: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := static.GenerateJavascriptBundles(); err != nil {
|
if err := static.GenerateJavascriptBundles(); err != nil {
|
||||||
printErrorAndExit(fmt.Errorf("unable to generate javascript bundles: %v", err))
|
printErrorAndExit(fmt.Errorf("unable to generate javascript bundle: %v", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err := database.NewConnectionPool(
|
db, err := database.NewConnectionPool(
|
||||||
|
|
|
@ -1,29 +1,7 @@
|
||||||
<!--
|
<!--
|
||||||
|
SPDX-FileCopyrightText: Copyright (c) 2020-2025 Paweł Kuna
|
||||||
MIT License
|
SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
Copyright (c) 2020 Paweł Kuna
|
|
||||||
|
|
||||||
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.
|
|
||||||
|
|
||||||
Source: https://github.com/tabler/tabler-icons
|
Source: https://github.com/tabler/tabler-icons
|
||||||
|
|
||||||
-->
|
-->
|
||||||
<svg xmlns="http://www.w3.org/2000/svg">
|
<svg xmlns="http://www.w3.org/2000/svg">
|
||||||
<symbol id="icon-read" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
<symbol id="icon-read" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||||||
|
|
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 13 KiB |
|
@ -6,13 +6,15 @@ package static // import "miniflux.app/v2/internal/ui/static"
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"embed"
|
"embed"
|
||||||
"fmt"
|
"log/slog"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"miniflux.app/v2/internal/crypto"
|
"miniflux.app/v2/internal/crypto"
|
||||||
|
|
||||||
"github.com/tdewolff/minify/v2"
|
"github.com/tdewolff/minify/v2"
|
||||||
"github.com/tdewolff/minify/v2/css"
|
"github.com/tdewolff/minify/v2/css"
|
||||||
"github.com/tdewolff/minify/v2/js"
|
"github.com/tdewolff/minify/v2/js"
|
||||||
|
"github.com/tdewolff/minify/v2/svg"
|
||||||
)
|
)
|
||||||
|
|
||||||
type asset struct {
|
type asset struct {
|
||||||
|
@ -22,9 +24,9 @@ type asset struct {
|
||||||
|
|
||||||
// Static assets.
|
// Static assets.
|
||||||
var (
|
var (
|
||||||
StylesheetBundles map[string]asset
|
StylesheetBundles map[string]asset
|
||||||
JavascriptBundles map[string]asset
|
JavascriptBundles map[string]asset
|
||||||
binaryFileChecksums map[string]string
|
BinaryBundles map[string]asset
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed bin/*
|
//go:embed bin/*
|
||||||
|
@ -36,40 +38,42 @@ var stylesheetFiles embed.FS
|
||||||
//go:embed js/*.js
|
//go:embed js/*.js
|
||||||
var javascriptFiles embed.FS
|
var javascriptFiles embed.FS
|
||||||
|
|
||||||
// CalculateBinaryFileChecksums generates hash of embed binary files.
|
func GenerateBinaryBundles() error {
|
||||||
func CalculateBinaryFileChecksums() error {
|
|
||||||
dirEntries, err := binaryFiles.ReadDir("bin")
|
dirEntries, err := binaryFiles.ReadDir("bin")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
binaryFileChecksums = make(map[string]string, len(dirEntries))
|
BinaryBundles = make(map[string]asset, len(dirEntries))
|
||||||
|
|
||||||
|
minifier := minify.New()
|
||||||
|
minifier.Add("image/svg+xml", &svg.Minifier{
|
||||||
|
KeepComments: true, // needed to keep the license
|
||||||
|
})
|
||||||
|
|
||||||
for _, dirEntry := range dirEntries {
|
for _, dirEntry := range dirEntries {
|
||||||
data, err := LoadBinaryFile(dirEntry.Name())
|
name := dirEntry.Name()
|
||||||
|
data, err := binaryFiles.ReadFile("bin/" + name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
binaryFileChecksums[dirEntry.Name()] = crypto.HashFromBytes(data)
|
if strings.HasSuffix(name, ".svg") {
|
||||||
|
// minifier.Bytes returns the data unchanged in case of error.
|
||||||
|
data, err = minifier.Bytes("image/svg+xml", data)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("Unable to minimize the svg file", slog.String("filename", name), slog.Any("error", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
BinaryBundles[name] = asset{
|
||||||
|
Data: data,
|
||||||
|
Checksum: crypto.HashFromBytes(data),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadBinaryFile loads an embed binary file.
|
|
||||||
func LoadBinaryFile(filename string) ([]byte, error) {
|
|
||||||
return binaryFiles.ReadFile("bin/" + filename)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetBinaryFileChecksum returns a binary file checksum.
|
|
||||||
func GetBinaryFileChecksum(filename string) (string, error) {
|
|
||||||
data, found := binaryFileChecksums[filename]
|
|
||||||
if !found {
|
|
||||||
return "", fmt.Errorf(`static: unable to find checksum for %q`, filename)
|
|
||||||
}
|
|
||||||
return data, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GenerateStylesheetsBundles creates CSS bundles.
|
// GenerateStylesheetsBundles creates CSS bundles.
|
||||||
func GenerateStylesheetsBundles() error {
|
func GenerateStylesheetsBundles() error {
|
||||||
var bundles = map[string][]string{
|
var bundles = map[string][]string{
|
||||||
|
|
|
@ -16,19 +16,13 @@ import (
|
||||||
|
|
||||||
func (h *handler) showAppIcon(w http.ResponseWriter, r *http.Request) {
|
func (h *handler) showAppIcon(w http.ResponseWriter, r *http.Request) {
|
||||||
filename := request.RouteStringParam(r, "filename")
|
filename := request.RouteStringParam(r, "filename")
|
||||||
etag, err := static.GetBinaryFileChecksum(filename)
|
value, ok := static.BinaryBundles[filename]
|
||||||
if err != nil {
|
if !ok {
|
||||||
html.NotFound(w, r)
|
html.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
response.New(w, r).WithCaching(etag, 72*time.Hour, func(b *response.Builder) {
|
response.New(w, r).WithCaching(value.Checksum, 72*time.Hour, func(b *response.Builder) {
|
||||||
blob, err := static.LoadBinaryFile(filename)
|
|
||||||
if err != nil {
|
|
||||||
html.ServerError(w, r, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
switch filepath.Ext(filename) {
|
switch filepath.Ext(filename) {
|
||||||
case ".png":
|
case ".png":
|
||||||
b.WithoutCompression()
|
b.WithoutCompression()
|
||||||
|
@ -36,8 +30,7 @@ func (h *handler) showAppIcon(w http.ResponseWriter, r *http.Request) {
|
||||||
case ".svg":
|
case ".svg":
|
||||||
b.WithHeader("Content-Type", "image/svg+xml")
|
b.WithHeader("Content-Type", "image/svg+xml")
|
||||||
}
|
}
|
||||||
|
b.WithBody(value.Data)
|
||||||
b.WithBody(blob)
|
|
||||||
b.Write()
|
b.Write()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,22 +13,16 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (h *handler) showFavicon(w http.ResponseWriter, r *http.Request) {
|
func (h *handler) showFavicon(w http.ResponseWriter, r *http.Request) {
|
||||||
etag, err := static.GetBinaryFileChecksum("favicon.ico")
|
value, ok := static.BinaryBundles["favicon.ico"]
|
||||||
if err != nil {
|
if !ok {
|
||||||
html.NotFound(w, r)
|
html.NotFound(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
response.New(w, r).WithCaching(etag, 48*time.Hour, func(b *response.Builder) {
|
response.New(w, r).WithCaching(value.Checksum, 48*time.Hour, func(b *response.Builder) {
|
||||||
blob, err := static.LoadBinaryFile("favicon.ico")
|
|
||||||
if err != nil {
|
|
||||||
html.ServerError(w, r, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
b.WithHeader("Content-Type", "image/x-icon")
|
b.WithHeader("Content-Type", "image/x-icon")
|
||||||
b.WithoutCompression()
|
b.WithoutCompression()
|
||||||
b.WithBody(blob)
|
b.WithBody(value.Data)
|
||||||
b.Write()
|
b.Write()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue