Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. Frontend
  4. Asset Versioning
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia

Table of Contents

  • Loading Assets from the manifest.json File
  • Learn more

Asset Versioning

Edit this page

Warning: You are browsing the documentation for Symfony 4.0, which is no longer maintained.

Read the updated version of this page for Symfony 6.2 (the current stable version).

Asset Versioning

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 change, 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 a manifest.json file with a map to the new filenames.

Loading Assets from the manifest.json File

Whenever you run Encore, a manifest.json file is automatically created in your outputPath directory:

1
2
3
4
{
    "build/app.js": "/build/app.123abc.js",
    "build/dashboard.css": "/build/dashboard.a4bf2d.css"
}

In your app, you need to read this file to dynamically render the correct paths in your script and link tags. If you're using Symfony, just activate the json_manifest_file versioning strategy:

1
2
3
4
5
6
# config/packages/framework.yaml
framework:
    # ...
    assets:
        # feature is supported in Symfony 3.3 and higher
        json_manifest_path: '%kernel.project_dir%/public/build/manifest.json'

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

1
2
3
<script src="{{ asset('build/app.js') }}"></script>

<link href="{{ asset('build/dashboard.css') }}" rel="stylesheet" />

Learn more

  • The Asset Component
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:
Take the exam at home

Take the exam at home

Measure & Improve Symfony Code Performance

Measure & Improve Symfony Code Performance

↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

Avatar of Robert-Jan de Dreu, a Symfony contributor

Thanks Robert-Jan de Dreu for being a Symfony contributor

3 commits • 27 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?
    • Symfony at a Glance
    • Symfony Components
    • Case Studies
    • 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
    • SymfonyConnect
    • Support
    • How to be Involved
    • Code of Conduct
    • Events & Meetups
    • Projects using Symfony
    • Downloads Stats
    • Contributors
    • Backers
  • Blog
    • Events & Meetups
    • A week of symfony
    • Case studies
    • Cloud
    • Community
    • Conferences
    • Diversity
    • Documentation
    • Living on the edge
    • Releases
    • Security Advisories
    • SymfonyInsight
    • Twig
    • SensioLabs
  • Services
    • SensioLabs services
    • Train developers
    • Manage your project quality
    • Improve your project performance
    • Host Symfony projects
    Deployed on
Follow Symfony
Search by Algolia