Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Rails with Webpacker not hot reloading

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?

like image 375
Matt Coady Avatar asked Feb 14 '19 19:02

Matt Coady


People also ask

Does react support hot reload?

React Native solves this problem for the developers. Hot reloading allows you to see the changes that you have made in the code without reloading your entire app. Whenever you make any changes, all you need to do is save your code.

How does hot module reload work?

Hot Module Replacement (HMR) exchanges, adds, or removes modules while an application is running, without a full reload. This can significantly speed up development in a few ways: Retain application state which is lost during a full reload.

What is Webpack hot reload?

Developing a JavaScript application involves reloading the browser each time you save code changes in order to refresh the user interface. Developer tools like Webpack can even run in watch mode to monitor your project files for changes.


1 Answers

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);
like image 79
vzamanillo Avatar answered Oct 16 '22 06:10

vzamanillo