Search code examples
ruby-on-railsdockerwebpackerreact-railshot-reload

HRM is not working on Ruby on Rails with Webpacker and Docker


I have a container in docker with Ruby on Rails 6, Webpacker and react-rails, when I reload the page it lates more than 20 seconds to compile so I want to add hot reload to my project but it is just not working. The webpack-dev-server es running well but the page is not updating the changes.

I am using foreman to run bin/webpack-dev-server and rails server and I am using react-refresh-webpack-plugin to refresh react. When I change code the page is not receiving the changes even if I reload the page.

docker-compose.yml

version: '3.3'
services:    
    mysql:
        image: mysql:latest
        restart: always    
        command: mysqld --default-authentication-plugin=mysql_native_password    
        ports:
            - 3307:3306        
        environment: 
            MYSQL_ROOT_PASSWORD: password
            MYSQL_DATABASE: development 
    ruby:        
        build: .               
        image: alexisnoe27/ubuntu_rails:master        
        command: bash -c ' 
            cd rails_app && 
            bundle update &&
            yarn &&
            rm -f tmp/pids/server.pid &&   
            bundle exec foreman start -f Procfile.dev'             
        environment: 
            MYSQL_USER: root
            MYSQL_PASSWORD: password
            MYSQL_PORT: 3306
            MYSQL_HOST: mysql
            MYSQL_DATABASE: development
            RAILS_ENV: development
            NODE_ENV: development
            WEBPACKER_DEV_SERVER_HOST: 0.0.0.0
        expose:
            - 3000       
            - 3035     
        ports: 
            - 3000:3000            
            - 3035:3035
        depends_on: 
            - mysql
        links: 
            - mysql
        volumes: 
            - ./:/rails_app               
        tty: true
        stdin_open: true

Procfile.dev

webpack: bin/webpack-dev-server
web: bundle exec rails s -b 0.0.0.0 -p 3000

webpacker.yml

# Note: You must restart bin/webpack-dev-server for changes to take effect

default: &default
  source_path: app/javascript
  source_entry_path: packs
  public_root_path: public
  public_output_path: packs
  cache_path: tmp/cache/webpacker  
  webpack_compile_output: true

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

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

  # Extract and emit a css file
  extract_css: false

  static_assets_extensions:
    - .jpg
    - .jpeg
    - .png
    - .gif
    - .tiff
    - .ico
    - .svg
    - .eot
    - .otf
    - .ttf
    - .woff
    - .woff2

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

development:
  <<: *default
  compile: true

  # Reference: https://webpack.js.org/configuration/dev-server/
  dev_server:
    https: false
    host: localhost
    hmr: true
    port: 3035
    public: localhost:3035
    # 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
    pretty: false
    headers:
      'Access-Control-Allow-Origin': '*'
    watch_options:
      ignored: '**/node_modules/**'


test:
  <<: *default
  compile: true

  # Compile test packs to a separate directory
  public_output_path: packs-test

production:
  <<: *default

  # Production depends on precompilation of packs prior to booting for performance.
  compile: false

  # Extract and emit a css file
  extract_css: true

  # Cache manifest.json for performance
  cache_manifest: true

development.js

process.env.NODE_ENV = process.env.NODE_ENV || 'development'
const environment = require('./environment')

const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
const isWebpackDevServer = process.env.WEBPACK_DEV_SERVER;

if (isWebpackDevServer) {
    console.log('true')
    environment.plugins.append(
        'ReactRefreshWebpackPlugin',
        new ReactRefreshWebpackPlugin({
            overlay: {
                sockPort: 3035
            }
        })
    );
}

module.exports = environment.toWebpackConfig()

console output

enter image description here


Solution

  • I could solve it, webpack was not watching my files, I added aggregateTimeout: 300 and poll: 500 in watch_options of my webpacker.yml, and it worked.

    # Note: You must restart bin/webpack-dev-server for changes to take effect
    
    default: &default
      source_path: app/javascript
      source_entry_path: packs
      public_root_path: public
      public_output_path: packs
      cache_path: tmp/cache/webpacker  
      webpack_compile_output: true
    
      # Additional paths webpack should lookup modules
      # ['app/assets', 'engine/foo/app/assets']
      additional_paths: []
    
      # Reload manifest.json on all requests so we reload latest compiled packs
      cache_manifest: false
    
      # Extract and emit a css file
      extract_css: false
    
      static_assets_extensions:
        - .jpg
        - .jpeg
        - .png
        - .gif
        - .tiff
        - .ico
        - .svg
        - .eot
        - .otf
        - .ttf
        - .woff
        - .woff2
    
      extensions:
        - .jsx
        - .mjs
        - .js
        - .sass
        - .scss
        - .css
        - .module.sass
        - .module.scss
        - .module.css
        - .png
        - .svg
        - .gif
        - .jpeg
        - .jpg
    
    development:
      <<: *default
      compile: true
    
      # Reference: https://webpack.js.org/configuration/dev-server/
      dev_server:
        https: false
        host: localhost
        hmr: true
        port: 3035
        public: localhost:3035
        # 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
        pretty: false
        headers:
          'Access-Control-Allow-Origin': '*'
        watch_options:
          ignored: '**/node_modules/**'
          aggregateTimeout: 300
          poll: 500
    
    
    test:
      <<: *default
      compile: true
    
      # Compile test packs to a separate directory
      public_output_path: packs-test
    
    production:
      <<: *default
    
      # Production depends on precompilation of packs prior to booting for performance.
      compile: false
    
      # Extract and emit a css file
      extract_css: true
    
      # Cache manifest.json for performance
      cache_manifest: true