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. Using webpack-dev-server and HMR

Using webpack-dev-server and HMR

Edit this page

While developing, instead of using npx encore dev --watch, you can use the webpack-dev-server:

1
$ npm run dev-server

This builds and serves the front-end assets from a new server. This server runs at localhost:8080 by default, meaning your build assets are available at localhost:8080/build. This server does not actually write the files to disk; instead it serves them from memory, allowing for hot module reloading.

As a consequence, the link and script tags need to point to the new server. If you're using the encore_entry_script_tags() and encore_entry_link_tags() Twig shortcuts (or are processing your assets through entrypoints.json in some other way), you're done: the paths in your templates will automatically point to the dev server.

dev-server Options

The dev-server command supports all the options defined by webpack-dev-server. You can set these options via command line options:

1
$ npm run dev-server -- --port 9000

You can also set these options using the Encore.configureDevServerOptions() method in your webpack.config.js file:

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

Encore
    // ...

    .configureDevServerOptions(options => {
        options.server = {
            type: 'https',
            options: {
                key: '/path/to/server.key',
                cert: '/path/to/server.crt',
            }
        }
    })
;

Enabling HTTPS using the Symfony Web Server

If you're using the Symfony web server locally with HTTPS, you'll need to also tell the dev-server to use HTTPS. To do this, you can reuse the Symfony web server SSL certificate:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// webpack.config.js
  // ...
+ const path = require('path');

  Encore
      // ...

+     .configureDevServerOptions(options => {
+         options.server = {
+             type: 'https',
+             options: {
+                 pfx: path.join(process.env.HOME, '.symfony5/certs/default.p12'),
+             }
+         }
+     })

Note

If you are using Node.js 17 or newer and dev-server fails to start with TLS error, the certificate file might be generated by an old version of symfony-cli. Upgrade symfony-cli to the latest version, delete the old ~/.symfony5/certs/default.p12 file, and start symfony server again.

This generates a new default.p12 file suitable for use with recent Node.js versions.

CORS Issues

If you experience issues related to CORS (Cross Origin Resource Sharing), set the following option:

1
2
3
4
5
6
7
8
9
10
11
// webpack.config.js
// ...

Encore
    // ...

    .configureDevServerOptions(options => {
        options.allowedHosts = 'all';
        // in older Webpack Dev Server versions, use this option instead:
        // options.firewall = false;
    })

Beware that this is not a recommended security practice in general, but here it's required to solve the CORS issue.

Hot Module Replacement HMR

Hot module replacement is a superpower of the dev-server where styles and (in some cases) JavaScript can automatically update without needing to reload your page. HMR works automatically with CSS (as long as you're using the dev-server and Encore 1.0 or higher) but only works with some JavaScript (like Vue.js).

Live Reloading when changing PHP / Twig Files

To utilize the HMR superpower along with live reload for your PHP code and templates, set the following options:

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

Encore
    // ...

    .configureDevServerOptions(options => {
        options.liveReload = true;
        options.static = {
            watch: false
        };
        options.watchFiles = {
            paths: ['src/**/*.php', 'templates/**/*'],
        };
    })

The static.watch option is required to disable the default reloading of files from the static directory, as those files are already handled by HMR.

1.0.0

Before Encore 1.0, you needed to pass a --hot flag at the command line to enable HMR. You also needed to disable CSS extraction to enable HMR for CSS. That is no longer needed.

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

    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).

    Make sure your project is risk free

    Make sure your project is risk free

    Version:

    Table of Contents

    • dev-server Options
    • Enabling HTTPS using the Symfony Web Server
    • CORS Issues
    • Hot Module Replacement HMR
      • Live Reloading when changing PHP / Twig Files

    Symfony footer

    Avatar of Arnaud Kleinpeter, a Symfony contributor

    Thanks Arnaud Kleinpeter (@nanocom) for being a Symfony contributor

    18 commits • 213 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