简体   繁体   中英

How to enable GZip compression for Gitlab Pages?

When using Gitlab Pages to render my site which is slow in page-ranking. I can't find any solution on how to do following in GitLab ( non-enterprise version )

  1. Specify HTTP Cache Headers for various page resources like for an image, so that it can be cached.

  2. Specify/Enable compression for GZip as page-ranking mentions compression disabled in gitlab.io.

  1. It looks like specifying HTTP Cache Headers is still not possible. But at least they have hardcoded "max-age=600" for all the resources here .
  2. You can compress contents of your public folder via .gitlab-ci.yml :

    script:

    • npm install
    • npm run build
    • gzip -k -6 -r public

GitLab has support for serving compressed assets if you pre-compress them in the pages CI Job already. See the documentation .

Note that you can and also should use brotli compression as it's optimized for web content and supported by most modern browsers.

There is also a suggested snippet for your .gitlab-ci.yml :

pages:
  # Other directives
  script:
    # Build the public/ directory first
    - find public -type f -regex '.*\.\(htm\|html\|txt\|text\|js\|css\)$' -exec gzip -f -k {} \;
    - find public -type f -regex '.*\.\(htm\|html\|txt\|text\|js\|css\)$' -exec brotli -f -k {} \;

I haven't found a way of influencing cache behavior. I am also looking for this.

Enable GZip compression for GitLab Pages

If you add the precompressed .gz versions of the files of your static site, then nginx can serve them instead of the regular ones. Add this line to your .gitlab-ci.yml file:

image: alpine:latest

pages:
  stage: deploy
  script:
    - mkdir .temp
    - cp -r * .temp
    - mv .temp public
    
  artifacts:
    paths:
      - public
  only:
    - master

This command compresses all files found in the public directory with the maximum compression ratio.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM