lukeo05
lukeo05

Reputation: 1863

Gulp/Symfony2: How can I avoid overwriting my js/css imports when cache busting?

I have a gulp build process for my Symfony2 application and am trying to integrate cache busting, but have hit a roadblock. I use gulp-rev to append a hash to my concatenated javascript file and output a manifest file to map between the original and the hashed filename. I currently have a twig template that has a single script tag to import all my correctly built javascript files, but I need to find a way of updating that script tag to point at the version of the file with the correct hash, ensuring that a fresh version of the file is downloaded every time the hash changes.

I have thought of a number of solutions which I've described below, but none of them feel right. Does anyone have a better/correct solution, or some reasoning as to why I should go with one of the ways I have already thought of.

So has anyone solved the cache-busting with gulp and symfony problem? How did you do it?

Upvotes: 3

Views: 758

Answers (1)

Nicolai Fröhlich
Nicolai Fröhlich

Reputation: 52493

You should make use of the two config directives:

framework.templating.assets_version
framework.templating.assets_version_format

You can read more about them in the documentation of the FrameworkBundle.

They do work with the regular {{ asset() }} function and don't require AsseticBundle.

Then just dump a parameter/config-file that assigns i.e. the md5sum of your compressed frontend source-files folder to assets_version in a git pre-commit hook.

.git/hooks/pre-commit

#!/usr/bin/env sh

echo "framework.templating.assets_version: $(tar -cf - ./src/Frontend | md5sum)" \
> app/config/assets_version.yml

app/config/config.yml

# ...
imports:
  # ...
  - { resource: './assets_version.yml' }

Upvotes: 2

Related Questions