Skip to content
Merged
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
2 changes: 1 addition & 1 deletion lib/web/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ func NewHandler(cfg Config, opts ...HandlerOption) (*APIHandler, error) {

fs := http.FileServer(cfg.StaticFS)

fs = makeGzipHandler(fs)
fs = makeBrotliHandler(fs, cfg.StaticFS)
fs = makeCacheHandler(fs, etag)

http.StripPrefix("/web", fs).ServeHTTP(w, r)
Expand Down
69 changes: 69 additions & 0 deletions lib/web/brotlihandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Teleport
* Copyright (C) 2025 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package web

import (
"io"
"mime"
"net/http"
"path"
"slices"
"strings"
)

var compressedFileExtensions = []string{
".js",
".svg",
".wasm",
}

// makeBrotliHandler serves pre-compressed .br files for supported file types.
func makeBrotliHandler(handler http.Handler, fs http.FileSystem) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ext := path.Ext(r.URL.Path)
isRequestForCompressedFile := slices.Contains(compressedFileExtensions, ext)
clientAcceptsBrotli := strings.Contains(r.Header.Get("Accept-Encoding"), "br")
if !isRequestForCompressedFile || !clientAcceptsBrotli {
handler.ServeHTTP(w, r)
return
}

brPath := r.URL.Path + ".br"
brFile, err := fs.Open(brPath)
if err != nil {
handler.ServeHTTP(w, r)
return
}
defer brFile.Close()

contentType := mime.TypeByExtension(ext)
if contentType == "" {
contentType = "application/octet-stream" // same default as http.DetectContentType
}

w.Header().Set("Content-Encoding", "br")
w.Header().Set("Content-Type", contentType)

if r.Method == http.MethodHead {
return
}

io.Copy(w, brFile)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

io.Copy can error but we aren't handling it here. are we ok with silent failing? second, is there any benefit to using something like http.ServeContent instead?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't think we needed all of the extra stuff http.ServeContent offers here as we handle the e-tag elsewhere and don't need range support (and http.ServeContent doesn't error as it doesn't return the error from the io.CopyN call), but I don't mind changing it

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thats fine then. its not like we can set headers or anything after io.Copy has failed and if ServeContent doesnt return the io.Copy errors then we can skip it too. The client will just see a corrupted response (not like theyd be able to see anything else at this point tho so), ok LGTM

})
}
82 changes: 0 additions & 82 deletions lib/web/gziphandler.go

This file was deleted.

17 changes: 17 additions & 0 deletions pnpm-lock.yaml

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

7 changes: 6 additions & 1 deletion web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ To build the Teleport open source version
pnpm build-ui-oss
```

The resulting output will be in the `webassets` folder.
The resulting output will be in the `webassets` folder. By default, the webassets are compressed with Brotli. If you
want to disable this for faster local builds, set the environment variable `VITE_DISABLE_COMPRESSION` to any value:

```
VITE_DISABLE_COMPRESSION=1 pnpm build-ui-oss
```

### Docker Build

Expand Down
1 change: 1 addition & 0 deletions web/packages/build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"jsdom": "^26.1.0",
"rollup-plugin-visualizer": "^6.0.3",
"typescript-eslint": "^8.35.1",
"vite-plugin-compression": "^0.5.1",
"vite-plugin-wasm": "^3.4.1",
"vite-tsconfig-paths": "^5.1.4"
}
Expand Down
13 changes: 13 additions & 0 deletions web/packages/build/vite/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { resolve } from 'path';

import { visualizer } from 'rollup-plugin-visualizer';
import { defineConfig, type UserConfig } from 'vite';
import compression from 'vite-plugin-compression';
import wasm from 'vite-plugin-wasm';

import { generateAppHashFile } from './apphash';
Expand Down Expand Up @@ -98,6 +99,18 @@ export function createViteConfig(

if (mode === 'production') {
config.base = '/web';

if (!process.env.VITE_DISABLE_COMPRESSION) {
config.plugins.push(
compression({
algorithm: 'brotliCompress',
deleteOriginFile: true,
filter: /\.(js|svg|wasm)$/,
threshold: 1024 * 10, // 10KB
verbose: false,
})
);
}
} else {
config.plugins.push(htmlPlugin(target));
// siteName matches everything between the slashes.
Expand Down
Loading