diff --git a/CHANGELOG.md b/CHANGELOG.md index e6be1d17..ea4ebcc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Added + +- Compression (gzip) to static web app files. Reduces app size significantly + ## [0.33.0] - 2024-12-14 ### Added diff --git a/playlet-lib/src/components/Web/PlayletWebServer/PlayletWebServer.bs b/playlet-lib/src/components/Web/PlayletWebServer/PlayletWebServer.bs index dc6a75a2..ae55a15b 100644 --- a/playlet-lib/src/components/Web/PlayletWebServer/PlayletWebServer.bs +++ b/playlet-lib/src/components/Web/PlayletWebServer/PlayletWebServer.bs @@ -59,7 +59,7 @@ function SetupRoutes(server as object) server.UseRouter(new Http.RegistryRouter(server)) etags = new Http.EtagUtils() - server.UseRouter(new Http.HttpStaticFilesRouter("/", "libpkg:/www", etags, { staticFiles: true })) + server.UseRouter(new Http.HttpStaticFilesRouter("/", "libpkg:/www", etags, { staticFiles: true, staticCompressed: true })) server.UseRouter(new Http.HttpStaticFilesRouter("/config", "libpkg:/config", etags, { staticFiles: true })) server.UseRouter(new Http.HttpStaticFilesRouter("/locale", "libpkg:/locale", etags, { staticFiles: true })) server.UseRouter(new Http.HttpStaticFilesRouter("/logs", "cachefs:/logs", etags, { fsCheck: true })) diff --git a/playlet-lib/src/components/Web/WebServer/Middleware/HttpStaticFilesRouter.bs b/playlet-lib/src/components/Web/WebServer/Middleware/HttpStaticFilesRouter.bs index a91ce0a2..a1c7d6ff 100644 --- a/playlet-lib/src/components/Web/WebServer/Middleware/HttpStaticFilesRouter.bs +++ b/playlet-lib/src/components/Web/WebServer/Middleware/HttpStaticFilesRouter.bs @@ -11,6 +11,7 @@ namespace Http private showHidden private fsCheck private staticFiles + private staticCompressed function new(route as string, root as string, etags as object, options = {} as object) super() @@ -30,6 +31,8 @@ namespace Http m.fsCheck = ValidBool(options.fsCheck) ' staticFiles: if true, Etag tags will be cached to skip reading the file entirely. m.staticFiles = ValidBool(options.staticFiles) + ' staticCompressed: the files are .gz and already compressed. + m.staticCompressed = m.staticFiles and ValidBool(options.staticCompressed) end function @get("*") @@ -47,6 +50,10 @@ namespace Http decoded_path = path_in_route.DecodeUri() full_path = StringUtils.PathJoin(m.root, decoded_path) + if m.staticCompressed + full_path += ".gz" + end if + if m.staticFiles ' Although we have an Etag middleware, having a check here allows us to ' skip reading the file entirely if the Etag matches. @@ -69,6 +76,10 @@ namespace Http if m.staticFiles m.etags.AddEtagFromBody(request, response, full_path) end if + + if m.staticCompressed + response.headers["Content-Encoding"] = "gzip" + end if return true end if end if @@ -110,6 +121,9 @@ namespace Http if m.staticFiles m.etags.AddEtagFromBody(request, response, full_path) end if + if m.staticCompressed + response.headers["Content-Encoding"] = "gzip" + end if return true else response.Default(403, "Not a regular file.") diff --git a/playlet-web/package-lock.json b/playlet-web/package-lock.json index 14e49156..2377b914 100644 --- a/playlet-web/package-lock.json +++ b/playlet-web/package-lock.json @@ -28,6 +28,7 @@ "tslib": "^2.8.1", "typescript": "^5.7.2", "vite": "^5.4.11", + "vite-plugin-compression": "^0.5.1", "vite-tsconfig-paths": "^5.1.4" } }, @@ -2592,6 +2593,37 @@ } ] }, + "node_modules/chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "dev": true, + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, + "node_modules/chalk/node_modules/ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "dev": true, + "dependencies": { + "color-convert": "^2.0.1" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/chalk/ansi-styles?sponsor=1" + } + }, "node_modules/chokidar": { "version": "3.6.0", "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.6.0.tgz", @@ -2978,6 +3010,20 @@ "url": "https://github.com/sponsors/rawify" } }, + "node_modules/fs-extra": { + "version": "10.1.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-10.1.0.tgz", + "integrity": "sha512-oRXApq54ETRj4eMiFzGnHWGy+zo5raudjuxN0b8H7s/RU2oW0Wvsx9O0ACRN/kRq9E8Vu/ReskGB5o3ji+FzHQ==", + "dev": true, + "dependencies": { + "graceful-fs": "^4.2.0", + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" + }, + "engines": { + "node": ">=12" + } + }, "node_modules/fsevents": { "version": "2.3.3", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", @@ -3057,6 +3103,21 @@ "integrity": "sha512-uHJgbwAMwNFf5mLst7IWLNg14x1CkeqglJb/K3doi4dw6q2IvAAmM/Y81kevy83wP+Sst+nutFTYOGg3d1lsxg==", "dev": true }, + "node_modules/graceful-fs": { + "version": "4.2.11", + "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.11.tgz", + "integrity": "sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==", + "dev": true + }, + "node_modules/has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", + "dev": true, + "engines": { + "node": ">=8" + } + }, "node_modules/hasown": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.0.tgz", @@ -3219,6 +3280,18 @@ "node": ">=6" } }, + "node_modules/jsonfile": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz", + "integrity": "sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==", + "dev": true, + "dependencies": { + "universalify": "^2.0.0" + }, + "optionalDependencies": { + "graceful-fs": "^4.1.6" + } + }, "node_modules/kleur": { "version": "4.1.5", "resolved": "https://registry.npmjs.org/kleur/-/kleur-4.1.5.tgz", @@ -4087,6 +4160,18 @@ "node": ">=16 || 14 >=14.17" } }, + "node_modules/supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "dev": true, + "dependencies": { + "has-flag": "^4.0.0" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/supports-preserve-symlinks-flag": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz", @@ -4496,6 +4581,15 @@ "node": ">=4" } }, + "node_modules/universalify": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.1.tgz", + "integrity": "sha512-gptHNQghINnc/vTGIk0SOFGFNXw7JVrlRUtConJRlvaw6DuX0wO5Jeko9sWrMBhh+PsYAZ7oXAiOnf/UKogyiw==", + "dev": true, + "engines": { + "node": ">= 10.0.0" + } + }, "node_modules/update-browserslist-db": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/update-browserslist-db/-/update-browserslist-db-1.1.1.tgz", @@ -4591,6 +4685,20 @@ } } }, + "node_modules/vite-plugin-compression": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/vite-plugin-compression/-/vite-plugin-compression-0.5.1.tgz", + "integrity": "sha512-5QJKBDc+gNYVqL/skgFAP81Yuzo9R+EAf19d+EtsMF/i8kFUpNi3J/H01QD3Oo8zBQn+NzoCIFkpPLynoOzaJg==", + "dev": true, + "dependencies": { + "chalk": "^4.1.2", + "debug": "^4.3.3", + "fs-extra": "^10.0.0" + }, + "peerDependencies": { + "vite": ">=2.0.0" + } + }, "node_modules/vite-tsconfig-paths": { "version": "5.1.4", "resolved": "https://registry.npmjs.org/vite-tsconfig-paths/-/vite-tsconfig-paths-5.1.4.tgz", diff --git a/playlet-web/package.json b/playlet-web/package.json index 2001e0ab..37fc05e0 100644 --- a/playlet-web/package.json +++ b/playlet-web/package.json @@ -26,6 +26,7 @@ "tslib": "^2.8.1", "typescript": "^5.7.2", "vite": "^5.4.11", + "vite-plugin-compression": "^0.5.1", "vite-tsconfig-paths": "^5.1.4" }, "dependencies": { diff --git a/playlet-web/public/favicon.ico b/playlet-web/public/favicon.ico deleted file mode 100644 index 157281b3..00000000 Binary files a/playlet-web/public/favicon.ico and /dev/null differ diff --git a/playlet-web/public/favicon.ico.gz b/playlet-web/public/favicon.ico.gz new file mode 100644 index 00000000..5033f80e Binary files /dev/null and b/playlet-web/public/favicon.ico.gz differ diff --git a/playlet-web/vite.config.ts b/playlet-web/vite.config.ts index df42b3d7..bf5a1d56 100644 --- a/playlet-web/vite.config.ts +++ b/playlet-web/vite.config.ts @@ -1,5 +1,6 @@ import { defineConfig, UserConfig } from 'vite' import { svelte } from '@sveltejs/vite-plugin-svelte' +import viteCompression from 'vite-plugin-compression'; import tsconfigPaths from 'vite-tsconfig-paths' import ip from 'ip'; import { existsSync, readFileSync } from 'fs' @@ -15,6 +16,9 @@ const config: UserConfig = { svelte(), legacy({ targets: ['defaults', 'not IE 11'], + }), + viteCompression({ + deleteOriginFile: true }) ], build: {