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 webpack-dev-server and HMR
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
  • Hot Module Replacement HMR

Using webpack-dev-server and HMR

Edit this page

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

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

Using webpack-dev-server and HMR

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

1
2
3
4
5
# if you use the Yarn package manager
$ yarn encore dev-server

# if you use the npm package manager
$ 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.

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

1
2
3
4
5
# if you use the Yarn package manager
$ yarn encore dev-server --https --port 9000

# if you use the npm package manager
$ npm run dev-server -- --https --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
// webpack.config.js
// ...

Encore
    // ...

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

0.28.4

The Encore.configureDevServerOptions() method was introduced in Encore 0.28.4.

Hot Module Replacement HMR

Encore does support HMR for Vue.js, but does not work for styles anywhere at this time. To activate it, pass the --hot option:

1
$ ./node_modules/.bin/encore dev-server --hot

If you want to use SSL with self-signed certificates, add the --https, --pfx=, and --allowed-hosts options to the dev-server command in the package.json file:

1
2
3
4
5
6
7
8
{
      ...
      "scripts": {
-        "dev-server": "encore dev-server",
+        "dev-server": "encore dev-server --https --pfx=$HOME/.symfony/certs/default.p12 --allowed-hosts=mydomain.wip",
          ...
      }
  }

If you experience issues related to CORS (Cross Origin Resource Sharing), add the --disable-host-check and --port options to the dev-server command in the package.json file:

1
2
3
4
5
6
7
8
{
      ...
      "scripts": {
-        "dev-server": "encore dev-server",
+        "dev-server": "encore dev-server --port 8080 --disable-host-check",
          ...
      }
  }

Caution

Beware that it's not recommended to disable host checking in general, but here it's required to solve the CORS issue.

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

    Become certified from home

    Symfony Code Performance Profiling

    Symfony Code Performance Profiling

    Symfony footer

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

    Avatar of Andrey Chernykh, a Symfony contributor

    Thanks Andrey Chernykh for being a Symfony contributor

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