-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Improve Lighthouse Scrore #6296
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
Draft
FarhanAliRaza
wants to merge
5
commits into
reflex-dev:main
Choose a base branch
from
FarhanAliRaza:lighthouse-improve
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2ec0b7a
lighthouse test
FarhanAliRaza 8ca31a5
Improve Lighthouse scores and add landing page benchmark
FarhanAliRaza cb3e850
Add gzip pre-compression plugin and optimize Vite chunk splitting
FarhanAliRaza 40d5aa7
warmup request
FarhanAliRaza 3f6f2a0
Add configurable pre-compression formats and serve precompressed stat…
FarhanAliRaza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
packages/reflex-base/src/reflex_base/.templates/web/vite-plugin-compress.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| /* vite-plugin-compress.js | ||
| * | ||
| * Generate pre-compressed build assets so they can be served directly by | ||
| * production static file servers and reverse proxies without on-the-fly | ||
| * compression. The default format is gzip, with optional brotli and zstd. | ||
| */ | ||
|
|
||
| import * as zlib from "node:zlib"; | ||
| import { dirname, join } from "node:path"; | ||
| import { readdir, readFile, stat, writeFile } from "node:fs/promises"; | ||
| import { promisify } from "node:util"; | ||
|
|
||
| const gzipAsync = promisify(zlib.gzip); | ||
| const brotliAsync = | ||
| typeof zlib.brotliCompress === "function" | ||
| ? promisify(zlib.brotliCompress) | ||
| : null; | ||
| const zstdAsync = | ||
| typeof zlib.zstdCompress === "function" ? promisify(zlib.zstdCompress) : null; | ||
|
|
||
| const COMPRESSIBLE_EXTENSIONS = /\.(js|css|html|json|svg|xml|txt|map|mjs)$/; | ||
|
|
||
| // Only compress files above this size (bytes). Tiny files don't benefit | ||
| // and the overhead of Content-Encoding negotiation can outweigh the saving. | ||
| const MIN_SIZE = 256; | ||
|
|
||
| const COMPRESSORS = { | ||
| gzip: { | ||
| extension: ".gz", | ||
| compress: (raw) => gzipAsync(raw, { level: 9 }), | ||
| }, | ||
| brotli: brotliAsync && { | ||
| extension: ".br", | ||
| compress: (raw) => | ||
| brotliAsync(raw, { | ||
| params: { | ||
| [zlib.constants.BROTLI_PARAM_QUALITY]: | ||
| zlib.constants.BROTLI_MAX_QUALITY ?? 11, | ||
| }, | ||
| }), | ||
| }, | ||
| zstd: zstdAsync && { | ||
| extension: ".zst", | ||
| compress: (raw) => zstdAsync(raw), | ||
| }, | ||
| }; | ||
|
|
||
| function normalizeFormats(formats = ["gzip"]) { | ||
| const normalized = []; | ||
| const seen = new Set(); | ||
|
|
||
| for (const format of formats) { | ||
| const normalizedFormat = String(format).trim().toLowerCase(); | ||
| if (!normalizedFormat || seen.has(normalizedFormat)) { | ||
| continue; | ||
| } | ||
| if (!(normalizedFormat in COMPRESSORS)) { | ||
| throw new Error( | ||
| `Unsupported frontend compression format "${format}". ` + | ||
| 'Expected one of: "gzip", "brotli", "zstd".', | ||
| ); | ||
| } | ||
| normalized.push(normalizedFormat); | ||
| seen.add(normalizedFormat); | ||
| } | ||
|
|
||
| return normalized; | ||
| } | ||
|
|
||
| async function* walkFiles(directory) { | ||
| for (const entry of await readdir(directory, { withFileTypes: true })) { | ||
| const entryPath = join(directory, entry.name); | ||
| if (entry.isDirectory()) { | ||
| yield* walkFiles(entryPath); | ||
| continue; | ||
| } | ||
| if (entry.isFile()) { | ||
| yield entryPath; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function ensureFormatsSupported(formats) { | ||
| const unavailableFormats = formats.filter( | ||
| (format) => !COMPRESSORS[format]?.compress, | ||
| ); | ||
| if (unavailableFormats.length > 0) { | ||
| throw new Error( | ||
| `The configured frontend compression formats are not supported by this Node.js runtime: ${unavailableFormats.join(", ")}`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| async function outputDirectoryExists(outputDir) { | ||
| return Boolean( | ||
| await stat(outputDir).catch((error) => | ||
| error?.code === "ENOENT" ? null : Promise.reject(error), | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| async function compressFile(filePath, formats) { | ||
| const raw = await readFile(filePath); | ||
| if (raw.length < MIN_SIZE) return; | ||
|
|
||
| await Promise.all( | ||
| formats.map((format) => { | ||
| const compressor = COMPRESSORS[format]; | ||
| return compressor | ||
| .compress(raw) | ||
| .then((compressed) => | ||
| writeFile(filePath + compressor.extension, compressed), | ||
| ); | ||
| }), | ||
| ); | ||
| } | ||
|
|
||
| export async function compressDirectory(directory, formats = ["gzip"]) { | ||
| const normalizedFormats = normalizeFormats(formats); | ||
| ensureFormatsSupported(normalizedFormats); | ||
|
|
||
| if (!(await outputDirectoryExists(directory))) { | ||
| return; | ||
| } | ||
|
|
||
| const jobs = []; | ||
| for await (const filePath of walkFiles(directory)) { | ||
| if (!COMPRESSIBLE_EXTENSIONS.test(filePath)) continue; | ||
| jobs.push(compressFile(filePath, normalizedFormats)); | ||
| } | ||
|
|
||
| await Promise.all(jobs); | ||
| } | ||
|
|
||
| /** | ||
| * Vite plugin that generates pre-compressed files for eligible build assets. | ||
| * @param {{ formats?: string[] }} [options] | ||
| * @returns {import('vite').Plugin} | ||
| */ | ||
| export default function compressPlugin(options = {}) { | ||
| const formats = normalizeFormats(options.formats); | ||
|
|
||
| return { | ||
| name: "vite-plugin-compress", | ||
| apply: "build", | ||
| enforce: "post", | ||
|
|
||
| async writeBundle(outputOptions) { | ||
| const outputDir = | ||
| outputOptions.dir ?? | ||
| (outputOptions.file ? dirname(outputOptions.file) : null); | ||
| if (!outputDir) return; | ||
| await compressDirectory(outputDir, formats); | ||
| }, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
lighthousejob falls back tonpx --yes lighthouse@13.1.0(viaget_lighthouse_command()) when nolighthousebinary is found, relying on whatever Node.js version is pre-installed onubuntu-22.04. The exact Node.js version shipped with GitHub's runner images can change without notice and is not formally guaranteed.Adding an explicit
actions/setup-nodestep would make the workflow reproducible across runner image updates: