6

I've added react-rails to an existing project that we're slowly migrating over to react.

Current webpacker.yml

default: &default
  source_path: app/javascript
  source_entry_path: packs
  public_output_path: packs
  cache_path: tmp/cache/webpacker

  # Additional paths webpack should lookup modules
  # ['app/assets', 'engine/foo/app/assets']
  resolved_paths: []

  # Reload manifest.json on all requests so we reload latest compiled packs
  cache_manifest: false

  extensions:
    - .jsx
    - .js
    - .sass
    - .scss
    - .css
    - .module.sass
    - .module.scss
    - .module.css
    - .png
    - .svg
    - .gif
    - .jpeg
    - .jpg
    - .tsx
    - .ts

development:
  <<: *default
  compile: true

  # Reference: https://webpack.js.org/configuration/dev-server/
  dev_server:
    https: false
    host: localhost
    port: 3035
    public: localhost:3035
    hmr: true
    # Inline should be set to true if using HMR
    inline: true
    overlay: true
    compress: true
    disable_host_check: true
    use_local_ip: false
    quiet: false
    headers:
      'Access-Control-Allow-Origin': '*'
    watch_options:
      ignored: /node_modules/

I boot up the rails app in one terminal and ./bin/webpack-dev-server in another and I can see the hot modules appearing in the compilation:

[./node_modules/webpack-dev-server/client/index.js?http://localhost:3035] (webpack)-dev-server/client?http://localhost:3035 7.93 kB {0} {1} [built]
[./node_modules/webpack-dev-server/client/overlay.js] (webpack)-dev-server/client/overlay.js 3.67 kB {0} {1} [built]
[./node_modules/webpack/hot/dev-server.js] (webpack)/hot/dev-server.js 1.61 kB {0} {1} [built]
[./node_modules/webpack/hot/emitter.js] (webpack)/hot/emitter.js 77 bytes {0} {1} [built]
[./node_modules/webpack/hot/log-apply-result.js] (webpack)/hot/log-apply-result.js 1.31 kB {0} {1} [built]
[./node_modules/webpack/hot/log.js] (webpack)/hot/log.js 1.04 kB {0} {1} [built]
   [0] multi (webpack)-dev-server/client?http://localhost:3035 webpack/hot/dev-server ./app/javascript/packs/application.js 52 bytes {1} [built]
   [1] multi (webpack)-dev-server/client?http://localhost:3035 webpack/hot/dev-server ./app/javascript/packs/server_rendering.js 52 bytes {0} [built]

The problem is when I'm running ./bin/webpack-dev-server I get a full page refresh every time I update my react files (instead of the components just being updated). The full page refresh only happens while dev server is running. Also, before the full page refresh I don't see component update.

So the question is, why is webpack-dev-server signaling a browser page refresh and why are the components not hot-reloading?

Matt Coady
  • 3,418
  • 5
  • 38
  • 63

1 Answers1

3

This is how Webpack HMR works, if you want to enable HMR on your react modules you can try React Hot Loader with the Webpack plugin

. Install react-hot-loader with yarn

. Edit the .babelrc file and add react-hot-loader/babel to the list of plugins.

. Make your React Component 'hot'.

import React from 'react'
import { hot } from 'react-hot-loader'

class Example extends React.Component {
}

export default hot(module)(Example);
vzamanillo
  • 9,905
  • 1
  • 36
  • 56
  • Update: the solution is considered [Old API](https://github.com/gaearon/react-hot-loader#old-api). For `react-hot-loader` version 4.5.4+ it's now `export default hot(Example)` – kino1 Aug 09 '19 at 09:52
  • 1
    This solution isn't really working for me... Do you have a repo you can link as an example perhaps? – rstojano Aug 12 '19 at 20:49
  • 1
    @rstojano I got it to work! Here is an example repo: https://github.com/edelgado/react-rails-hmr – Enrique Delgado Nov 06 '19 at 22:50
  • 2
    Thanks @rstojano! I just submitted a PR with your example updated to Rails 6 - fork is here: https://github.com/Leap-Forward/react-rails-hmr – David Hersey Apr 14 '20 at 21:39
  • one more solution https://github.com/railsjazz/rails_live_reload – Igor Kasyanchuk May 31 '22 at 11:06