Skip to content

Add precompressed brotli/gzip assets #12398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ COMMA := ,

XGO_VERSION := go-1.14.x
MIN_GO_VERSION := 001012000
MIN_NODE_VERSION := 010013000
MIN_NODE_VERSION := 010016000

DOCKER_IMAGE ?= gitea/gitea
DOCKER_TAG ?= latest
Expand Down Expand Up @@ -196,7 +196,7 @@ node-check:
$(eval NODE_VERSION := $(shell printf "%03d%03d%03d" $(shell node -v | cut -c2- | tr '.' ' ');))
$(eval NPM_MISSING := $(shell hash npm > /dev/null 2>&1 || echo 1))
@if [ "$(NODE_VERSION)" -lt "$(MIN_NODE_VERSION)" -o "$(NPM_MISSING)" = "1" ]; then \
echo "Gitea requires Node.js 10 or greater and npm to build. You can get it at https://nodejs.org/en/download/"; \
echo "Gitea requires Node.js 10.16 or greater and npm to build. You can get it at https://nodejs.org/en/download/"; \
exit 1; \
fi

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ or if sqlite support is required:
The `build` target is split into two sub-targets:

- `make backend` which requires [Go 1.12](https://golang.org/dl/) or greater.
- `make frontend` which requires [Node.js 10.13](https://nodejs.org/en/download/) or greater.
- `make frontend` which requires [Node.js 10.16](https://nodejs.org/en/download/) or greater.

If pre-built frontend files are present it is possible to only build the backend:

Expand Down
2 changes: 1 addition & 1 deletion custom/conf/app.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ KEY_FILE = https/key.pem
STATIC_ROOT_PATH =
; Default path for App data
APP_DATA_PATH = data
; Application level GZIP support
; Enable gzip compression for runtime-generated content
ENABLE_GZIP = false
; Application profiling (memory and cpu)
; For "web" command it listens on localhost:6060
Expand Down
2 changes: 1 addition & 1 deletion docs/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ params:
version: 1.12.2
minGoVersion: 1.12
goVersion: 1.14
minNodeVersion: 10.13
minNodeVersion: 10.16

outputs:
home:
Expand Down
2 changes: 1 addition & 1 deletion docs/content/doc/advanced/config-cheat-sheet.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ Values containing `#` or `;` must be quoted using `` ` `` or `"""`.
- `KEY_FILE`: **https/key.pem**: Key file path used for HTTPS. From 1.11 paths are relative to `CUSTOM_PATH`.
- `STATIC_ROOT_PATH`: **./**: Upper level of template and static files path.
- `STATIC_CACHE_TIME`: **6h**: Web browser cache time for static resources on `custom/`, `public/` and all uploaded avatars.
- `ENABLE_GZIP`: **false**: Enables application-level GZIP support.
- `ENABLE_GZIP`: **false**: Enable gzip compression for runtime-generated content.
- `LANDING_PAGE`: **home**: Landing page for unauthenticated users \[home, explore, organizations, login\].
- `LFS_START_SERVER`: **false**: Enables git-lfs support.
- `LFS_CONTENT_PATH`: **./data/lfs**: Where to store LFS files.
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ require (
github.com/quasoft/websspi v1.0.0
github.com/remyoudompheng/bigfft v0.0.0-20190321074620-2f0d2b0e0001 // indirect
github.com/sergi/go-diff v1.1.0
github.com/shurcooL/httpfs v0.0.0-20190527155220-6a4d4a70508b // indirect
github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 // indirect
github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546
github.com/stretchr/testify v1.4.0
github.com/tecbot/gorocksdb v0.0.0-20181010114359-8752a9433481 // indirect
github.com/tinylib/msgp v1.1.2 // indirect
Expand All @@ -110,7 +110,7 @@ require (
golang.org/x/text v0.3.2
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 // indirect
golang.org/x/tools v0.0.0-20200325010219-a49f79bcc224
google.golang.org/appengine v1.6.5 // indirect
google.golang.org/appengine v1.6.5
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/asn1-ber.v1 v1.0.0-20150924051756-4e86f4367175 // indirect
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
Expand Down
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -617,10 +617,10 @@ github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/shurcooL/httpfs v0.0.0-20190527155220-6a4d4a70508b h1:4kg1wyftSKxLtnPAvcRWakIPpokB9w780/KwrNLnfPA=
github.com/shurcooL/httpfs v0.0.0-20190527155220-6a4d4a70508b/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg=
github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd h1:ug7PpSOB5RBPK1Kg6qskGBoP3Vnj/aNYFTznWvlkGo0=
github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw=
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 h1:bUGsEnyNbVPw06Bs80sCeARAlK8lhwqGyi6UT8ymuGk=
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg=
github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546 h1:pXY9qYc/MP5zdvqWEUH6SjNiu7VhSjuVFTFiTcphaLU=
github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw=
github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726/go.mod h1:3yhqj7WBBfRhbBlzyOC3gUxftwsU0u8gqevxwIHQpMw=
github.com/siddontang/go-snappy v0.0.0-20140704025258-d8f7bb82a96d h1:qQWKKOvHN7Q9c6GdmUteCef2F9ubxMpxY1IKwpIKz68=
github.com/siddontang/go-snappy v0.0.0-20140704025258-d8f7bb82a96d/go.mod h1:vq0tzqLRu6TS7Id0wMo2N5QzJoKedVeovOpHjnykSzY=
Expand Down Expand Up @@ -711,8 +711,6 @@ github.com/yuin/goldmark v1.1.7/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
github.com/yuin/goldmark v1.1.22/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.25 h1:isv+Q6HQAmmL2Ofcmg8QauBmDPlUUnSoNhEcC940Rds=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32 h1:5tjfNdR2ki3yYQ842+eX2sQHeiwpKJ0RnHO4IYOc4V8=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1 h1:ruQGxdhGHe7FWOJPT0mKs5+pD2Xs1Bm/kdGlHO04FmM=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark-highlighting v0.0.0-20200307114337-60d527fdb691 h1:VWSxtAiQNh3zgHJpdpkpVYjTPqRE3P6UZCOPa1nRDio=
Expand Down
30 changes: 29 additions & 1 deletion modules/public/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"path"
"path/filepath"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -89,6 +90,8 @@ func (opts *Options) staticHandler(dir string) macaron.Handler {
}
}

var commaRe = regexp.MustCompile(`,\s*`)

func (opts *Options) handle(ctx *macaron.Context, log *log.Logger, opt *Options) bool {
if ctx.Req.Method != "GET" && ctx.Req.Method != "HEAD" {
return false
Expand All @@ -106,7 +109,32 @@ func (opts *Options) handle(ctx *macaron.Context, log *log.Logger, opt *Options)
}
}

f, err := opt.FileSystem.Open(file)
encodings := make(map[string]bool)
for _, encoding := range commaRe.Split(ctx.Req.Header.Get("Accept-Encoding"), -1) {
encodings[encoding] = true
}

var f http.File
var err error

if encodings["br"] {
f, err = opt.FileSystem.Open(file + ".br")
if f != nil {
ctx.Resp.Header().Set("Content-Encoding", "br")
}
}

if f == nil && encodings["gzip"] {
f, err = opt.FileSystem.Open(file + ".gz")
if f != nil {
ctx.Resp.Header().Set("Content-Encoding", "gzip")
}
}

if f == nil {
f, err = opt.FileSystem.Open(file)
}

if err != nil {
// 404 requests to any known entries in `public`
if path.Base(opts.Directory) == "public" {
Expand Down
83 changes: 83 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"license": "MIT",
"private": true,
"engines": {
"node": ">= 10.13.0"
"node": ">= 10.16.0"
},
"dependencies": {
"@babel/core": "7.10.5",
Expand All @@ -14,6 +14,7 @@
"@primer/octicons": "10.0.0",
"babel-loader": "8.1.0",
"clipboard": "2.0.6",
"compression-webpack-plugin": "4.0.0",
"core-js": "3.6.5",
"css-loader": "4.0.0",
"cssnano-webpack-plugin": "1.0.3",
Expand Down
6 changes: 3 additions & 3 deletions routers/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,6 @@ func NewMacaron() *macaron.Macaron {
setupAccessLogger(m)
}
m.Use(macaron.Recovery())
if setting.EnableGzip {
m.Use(gzip.Middleware())
}
if setting.Protocol == setting.FCGI || setting.Protocol == setting.FCGIUnix {
m.SetURLPrefix(setting.AppSubURL)
}
Expand All @@ -149,6 +146,9 @@ func NewMacaron() *macaron.Macaron {
ExpiresAfter: setting.StaticCacheTime,
},
))
if setting.EnableGzip {
m.Use(gzip.Middleware())
}
m.Use(public.StaticHandler(
setting.AvatarUploadPath,
&public.Options{
Expand Down
4 changes: 2 additions & 2 deletions vendor/github.com/shurcooL/vfsgen/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions vendor/github.com/shurcooL/vfsgen/generator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -638,10 +638,10 @@ github.com/quasoft/websspi/secctx
# github.com/sergi/go-diff v1.1.0
## explicit
github.com/sergi/go-diff/diffmatchpatch
# github.com/shurcooL/httpfs v0.0.0-20190527155220-6a4d4a70508b
# github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749
## explicit
github.com/shurcooL/httpfs/vfsutil
# github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd
# github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546
## explicit
github.com/shurcooL/vfsgen
# github.com/siddontang/go-snappy v0.0.0-20140704025258-d8f7bb82a96d
Expand Down Expand Up @@ -851,6 +851,7 @@ golang.org/x/xerrors/internal
# google.golang.org/appengine v1.6.5
## explicit
google.golang.org/appengine
google.golang.org/appengine/cloudsql
google.golang.org/appengine/internal
google.golang.org/appengine/internal/app_identity
google.golang.org/appengine/internal/base
Expand Down
24 changes: 23 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const fastGlob = require('fast-glob');
const wrapAnsi = require('wrap-ansi');
const CompressionPlugin = require('compression-webpack-plugin');
const CssNanoPlugin = require('cssnano-webpack-plugin');
const FixStyleOnlyEntriesPlugin = require('webpack-fix-style-only-entries');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
const PostCSSPresetEnv = require('postcss-preset-env');
const TerserPlugin = require('terser-webpack-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const {constants} = require('zlib');
const {statSync} = require('fs');
const {resolve, parse} = require('path');
const {LicenseWebpackPlugin} = require('license-webpack-plugin');
Expand Down Expand Up @@ -298,7 +300,26 @@ module.exports = {
errors: true,
},
}),
],
isProduction && new CompressionPlugin({
filename: '[path].gz',
algorithm: 'gzip',
test: /\.(js|css)$/,
compressionOptions: {
level: constants.Z_BEST_COMPRESSION,
},
}),
isProduction && new CompressionPlugin({
filename: '[path].br',
algorithm: 'brotliCompress',
test: /\.(js|css)$/,
compressionOptions: {
params: {
[constants.BROTLI_PARAM_MODE]: constants.BROTLI_MODE_TEXT,
[constants.BROTLI_PARAM_QUALITY]: constants.BROTLI_MAX_QUALITY,
}
},
}),
].filter((plugin) => !!plugin),
performance: {
hints: false,
maxEntrypointSize: Infinity,
Expand All @@ -321,6 +342,7 @@ module.exports = {
// exclude monaco's language chunks in stats output for brevity
// https://github.com/microsoft/monaco-editor-webpack-plugin/issues/113
/^js\/[0-9]+\.js$/,
/\.(gz|br)$/,
],
},
};