Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Frontend
  4. Encore
  5. Asset Versioning with Webpack Encore

Asset Versioning with Webpack Encore

Edit this page

Tired of deploying and having browser's cache the old version of your assets? By calling enableVersioning(), each filename will now include a hash that changes whenever the contents of that file change (e.g. app.123abc.js instead of app.js). This allows you to use aggressive caching strategies (e.g. a far future Expires) because, whenever a file changes, its hash will change, ignoring any existing cache:

1
2
3
4
5
6
7
// webpack.config.js

  // ...
  Encore
      .setOutputPath('public/build/')
      // ...
+     .enableVersioning()

To link to these assets, Encore creates two files entrypoints.json and manifest.json.

Loading Assets from entrypoints.json & manifest.json

Whenever you run Encore, two configuration files are generated in your output folder (default location: public/build/): entrypoints.json and manifest.json. Each file is similar, and contains a map to the final, versioned filenames.

The first file – entrypoints.json – is used by the encore_entry_script_tags() and encore_entry_link_tags() Twig helpers. If you're using these, then your CSS and JavaScript files will render with the new, versioned filename. If you're not using Symfony, your app will need to read this file in a similar way.

The manifest.json file is only needed to get the versioned filename of other files, like font files or image files (though it also contains information about the CSS and JavaScript files):

1
2
3
4
5
{
    "build/app.js": "/build/app.123abc.js",
    "build/dashboard.css": "/build/dashboard.a4bf2d.css",
    "build/images/logo.png": "/build/images/logo.3eed42.png"
}

In your app, you need to read this file if you want to be able to link (e.g. via an img tag) to certain assets. If you're using Symfony, just activate the json_manifest_file versioning strategy:

1
2
3
4
5
# this file is added automatically when installing Encore with Symfony Flex
# config/packages/assets.yaml
framework:
    assets:
        json_manifest_path: '%kernel.project_dir%/public/build/manifest.json'

That's it! Be sure to wrap each path in the Twig asset() function like normal:

1
<img src="{{ asset('build/images/logo.png') }}" alt="ACME logo">

Troubleshooting

Asset Versioning and Deployment

When deploying a new version of your application, versioned assets will include a new hash, making the previous assets no longer available. This is usually not a problem when deploying applications using a rolling update, blue/green or symlink strategies.

However, even when applying those techniques, there could be a lapse of time when some publicly/privately cached response requests the previous version of the assets. If your application can't afford to serve any broken asset, the best solution is to use a CDN (or custom made service) that keeps all the old assets cached for some time.

Learn more

  • The Asset Component
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version

    Symfony 7.1 is backed by

    Measure & Improve Symfony Code Performance

    Measure & Improve Symfony Code Performance

    Be safe against critical risks to your projects and businesses

    Be safe against critical risks to your projects and businesses

    Version:

    Table of Contents

    • Loading Assets from entrypoints.json & manifest.json
    • Troubleshooting
      • Asset Versioning and Deployment
    • Learn more

    Symfony footer

    Avatar of inspiran, a Symfony contributor

    Thanks inspiran for being a Symfony contributor

    1 commit • 71 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony