0

I am trying to pre-generate compressed brotli files in designated directories for static html, css and js files.

I found a rust tool here – https://github.com/neosmart/static-compress

Besides this, there’s also an option within the W3TC WordPress plugin. However, despite brotli PHP module being loaded and available, W3TC is not creating separate gzip or brotli files.

I used to have on-the-fly compression, but that option is no longer there.

I thought of creating a bash script and having it run every 6 hours or so, but then, I realized that it might not be as simple as it seems, as there are cases where the original file may change, and there might already be an .br file in the folder belonging to a previous version of the original file (source file) and so on.

Looks like there’s no other WordPress plugin than W3TC to do this either, and W3TC seems to be buggy on this aspect [I have enabled brotli compression in Browser Cache section and also enabled preload, but it's been 12 hours and not even a single cached file has a corresponding br or gz version in the servers cache directory].

MeSo2
  • 254
  • 1
  • 3
  • 18
Sus
  • 21
  • 2

2 Answers2

1

lsyncd is a tool that watches designated directories for changes in files. If it detects a change, it can run any command.

So, one can set it up to watch your HTML directory and run brotli on the file every time it changes.

It requires some LUA programming to actually implement the functionality.

inotify-tools could be another usable tool for the purpose.

Most reliable way to do this is to have a proper deployment process for your files. Deployment process takes files from your code repository and then performs any needed operations like compression. After that it copies files to the server. Setting this up requires more effort than using the inotify-based solutions above. However, it allows most flexibility to automate things during deployment.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
0

It depends on your operating system.

For CentOS/RHEL, the canonical tool to do what you want is the brotli program found within brotli package. brotli -h output:

Usage: brotli [OPTION]... [FILE]...
Options:
  -#                          compression level (0-9)
  -c, --stdout                write on standard output
  -d, --decompress            decompress
  -f, --force                 force output file overwrite
  -h, --help                  display this help and exit
  -j, --rm                    remove source file(s)
  -k, --keep                  keep source file(s) (default)
  -n, --no-copy-stat          do not copy source file(s) attributes
  -o FILE, --output=FILE      output file (only if 1 input file)
  -q NUM, --quality=NUM       compression level (0-11)
  -t, --test                  test compressed file integrity
  -v, --verbose               verbose mode
  -w NUM, --lgwin=NUM         set LZ77 window size (0, 10-24)
                              window size = 2**NUM - 16
                              0 lets compressor choose the optimal value
  --large_window=NUM          use incompatible large-window brotli
                              bitstream with window size (0, 10-30)
                              WARNING: this format is not compatible
                              with brotli RFC 7932 and may not be
                              decodable with regular brotli decoders
  -S SUF, --suffix=SUF        output file suffix (default:'.br')
  -V, --version               display version and exit
  -Z, --best                  use best compression level (11) (default)
Simple options could be coalesced, i.e. '-9kf' is equivalent to '-9 -k -f'.
With no FILE, or when FILE is -, read standard input.
All arguments after '--' are treated as files.

I realized that it might not be as simple as it seems, as there are cases where the original file may change

Of course, your script should check file modification time of the original files, recreate brotli versions upon change, etc

Danila Vershinin
  • 5,286
  • 5
  • 17
  • 21