Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • SensioLabs Professional services to help you with Symfony
    • 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
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by SensioLabs
  1. Home
  2. Documentation
  3. Frontend
  4. Using a CDN
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud

Using a CDN

Edit this page

Using a CDN

Are you deploying to a CDN? That's awesome :) Once you've made sure that your built files are uploaded to the CDN, configure it in Encore:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// webpack.config.js
  // ...

  Encore
      .setOutputPath('public/build/')
      // in dev mode, don't use the CDN
      .setPublicPath('/build');
      // ...
  ;

+ if (Encore.isProduction()) {
+     Encore.setPublicPath('https://my-cool-app.com.global.prod.fastly.net');
+
+     // guarantee that the keys in manifest.json are *still*
+     // prefixed with build/
+     // (e.g. "build/dashboard.js": "https://my-cool-app.com.global.prod.fastly.net/dashboard.js")
+     Encore.setManifestKeyPrefix('build/');
+ }

That's it! Internally, Webpack will now know to load assets from your CDN - e.g. https://my-cool-app.com.global.prod.fastly.net/dashboard.js.

Note

It's still your responsibility to put your assets on the CDN - e.g. by uploading them or by using "origin pull", where your CDN pulls assets directly from your web server.

You do need to make sure that the script and link tags you include on your pages also use the CDN. Fortunately, the entrypoints.json paths are updated to include the full URL to the CDN.

When deploying to a subdirectory of your CDN, you must add the path at the end of your URL - e.g. Encore.setPublicPath('https://my-cool-app.com.global.prod.fastly.net/awesome-website') will generate assets URLs like https://my-cool-app.com.global.prod.fastly.net/awesome-website/dashboard.js

If you are using Encore.enableIntegrityHashes() and your CDN and your domain are not the same-origin, you may need to set the crossorigin option in your webpack_encore.yaml configuration to anonymous or use-credentials to overcome CORS errors.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version
    We stand with Ukraine.
    Version:

    Symfony 6.2 is backed by

    Symfony 6.2 is backed by

    Symfony Code Performance Profiling

    Symfony Code Performance Profiling

    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Symfony footer

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

    Avatar of Kevin Wojniak, a Symfony contributor

    Thanks Kevin Wojniak for being a Symfony contributor

    1 commit • 2 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