AnderssonPeter / CompressedStaticFiles

asp.net core middleware to send compressed static files to the browser without having to compress on demand, also has support for sending more advanced image formats when the browser indicates that i has support for it.
Apache License 2.0
88 stars 18 forks source link

Compressing Files if not already available on server? #33

Closed aloksharma1 closed 2 years ago

aloksharma1 commented 2 years ago

Hello, i think this feature will be helpful in case a static file is not already compressed on server, we can run a DI based or prefixed file minimizer (css/js minification through nuglify) & compress it for future usage when such files are not already present. If you would like, i can make a PR on the same too. thanks

AnderssonPeter commented 2 years ago

Hi, thanks for your suggestion and time but this had been requested before and dismissed, the whole idea here is that the static content is compressed at build time not runtime. (this way we can use more advanced compression that simply takea to long to run on demand).

Lets say we had dynamic compression, what whould happen of the file is compressed at runtime then we deploy a new version where the source file is changed..

Now we must either validate all compressed files or we might cause hard to debug issues with the wrong content being served.

aloksharma1 commented 2 years ago

Thanks for the input, in this case i will apply my own watchers and compression mechanism separately.

aloksharma1 commented 2 years ago

ok,

i have done a few server side solutions, but the fastest to implement is this batch script that i made for windows server. it matches file datetime, deletes compressed if older then main file and recompress.. you just need to change compression provider (zopfli/brotli/PNGZopfli) and extensions as per your need on server side. you can also configure this as task scheduler to service to run everyday or by every interval to keep your files upto date.

hope this helps others:

@ECHO OFF

REG COPY "HKCU\Control Panel\International" "HKCU\Control Panel\International-Temp" /f >nul
REG ADD "HKCU\Control Panel\International" /v sShortDate /d "yyyyMMdd" /f >nul
REG ADD "HKCU\Control Panel\International" /v sTimeFormat /d "HHmmss" /f >nul

FOR /R ".\wwwroot\" %%i IN (*.css,*.txt,*.js) DO (
  IF EXIST %%i.gz (
    FOR %%b IN (%%i.gz) DO (
      IF "%%~ti" GTR "%%~tb" (
        ECHO %%i.gz is older, replacing
        DEL %%i.gz
        timeout /T 2 & start "%%i" /BELOWNORMAL /Min /B c:\zopfli.exe %%i
      )
    )
  ) ELSE (
    ECHO creating %%i.gz
    timeout /T 2 & start "%%i" /BELOWNORMAL /Min /B c:\zopfli.exe %%i
  )
)

REG COPY "HKCU\Control Panel\International-Temp" "HKCU\Control Panel\International" /f >nul

i will put my file watcher version by coming month(s).