-
-
Notifications
You must be signed in to change notification settings - Fork 226
Description
Hi! Thanks again for the other change and all your work on this project. I know this topic is one you already investigated in #180. I'm guessing you decided against adding an alternate URL encoder to Parse just for the combination of SVG data URIs in HTML/CSS.
It's a bit too bad because in all the files I've seen Minify always ends up base64-encoding every SVG data URI, which is a ~30% size increase when the file is already encoded using one or the other of the various tools out there.
Personally I've always worked around this by just commenting out the call to DataURI
. With that in mind, what about just adding a new option to the HTML and CSS Minify modes to disable descending into data URIs? As a side effect it would save a little processing time by avoiding parsing data URIs and SVG when selected.
Maybe a better idea: DataURI
could exit early and return the original bytestream unmodified as soon as it detects that the base64Len and asciiLen are larger than the original. I hacked together a quick version below, which unfortunately does a copy of the datauri bytestream. I didn't spend long on it but I can't think of an easy way to avoid this. On the good side, the function also exits early sometimes which avoids calls to URL/base64 encoders. So is it faster or slower? As a quick test I concatenated together 2 MB of CSS containing 400 svg urls but couldn't measure a difference in real/user/sys time given all the variance you see between trials testing on a laptop. But I don't think there's a performance penalty.
diff --git a/common.go b/common.go
index d722635..aed6d89 100644
--- a/common.go
+++ b/common.go
@@ -37,4 +37,7 @@ func Mediatype(b []byte) []byte {
// DataURI minifies a data URI and calls a minifier by the specified mediatype. Specifications: https://www.ietf.org/rfc/rfc2397.txt.
func DataURI(m *M, dataURI []byte) []byte {
+ original := make([]byte, len(dataURI))
+ copy(original, dataURI)
+ originalLen := len(original)
if mediatype, data, err := parse.DataURI(dataURI); err == nil {
dataURI, _ = m.Bytes(string(mediatype), data)
@@ -45,8 +48,11 @@ func DataURI(m *M, dataURI []byte) []byte {
asciiLen += 2
}
- if asciiLen > base64Len {
+ if asciiLen > base64Len || asciiLen > originalLen {
break
}
}
+ if base64Len > originalLen && asciiLen > originalLen {
+ return original
+ }
if asciiLen > base64Len {
encoded := make([]byte, base64Len-len(";base64"))
I'm don't work in Go but I can take a try at implementing something if you like, and send a pull request.
Or feel free to close this issue out you don't like any option, I won't be offended. Thanks!