SACn
SACn

Reputation: 1924

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.

Upvotes: 9

Views: 3693

Answers (3)

Scolytus
Scolytus

Reputation: 17177

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.

Upvotes: 7

user11141611
user11141611

Reputation:

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
    - gzip -k -9 $(find public -type f)
  artifacts:
    paths:
      - public
  only:
    - master

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

Upvotes: 2

berzhy
berzhy

Reputation: 114

  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

Upvotes: 5

Related Questions