From b49557a5a6632e9d480a3e61b832915c5571b9e7 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Sun, 15 Dec 2024 16:50:26 -0500 Subject: [PATCH] Web app compression (#505) --- CHANGELOG.md | 6 + .../Web/PlayletWebServer/PlayletWebServer.bs | 2 +- .../Middleware/HttpStaticFilesRouter.bs | 14 +++ playlet-web/package-lock.json | 108 ++++++++++++++++++ playlet-web/package.json | 1 + playlet-web/public/favicon.ico | Bin 15086 -> 0 bytes playlet-web/public/favicon.ico.gz | Bin 0 -> 1429 bytes playlet-web/vite.config.ts | 4 + 8 files changed, 134 insertions(+), 1 deletion(-) delete mode 100644 playlet-web/public/favicon.ico create mode 100644 playlet-web/public/favicon.ico.gz 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 157281b3505dab9c6caf134218444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