Hacker News new | ask | show | jobs
by pault 3614 days ago
Webpack and the asset pipeline overlap in responsibility, so you probably want to stick with one or the other. There may be some edge cases that I don't know about where you might want functionality from both, though.

I personally use webpack and transpile/bundle all the javascript and styles into a `build` directory that gets served by nginx.

For my side projects, I use a really simple express server with a single handler for all routes that pre-renders the react app and sends it down to the client for rehydration. After that, the client takes over and sends requests directly to the api server (rails in this case). Your boilerplate setup is an nginx reverse proxy that forwards all requests to /api/* to the rails server, and everything else goes to the express server.

For easy reproduction, you can use a docker cluster with a configuration something like this:

    version: "2"
    services:
      www:
        image: "nginx:1.10.1"
        restart: "always"
        volumes:
          - "./node/dist:/usr/share/nginx/html/dist"
          - "./nginx/default.conf:/etc/nginx/conf.d/default.conf"
        ports:
         - "8080:80"
        links:
          - "app:app"
          - "api:api"
        container_name: "react-rails_www"
      app:
        build:
          context: "."
          dockerfile: "Dockerfile-node"
        image: "username/react-redux:0.0.1"
        volumes:
          - "./node:/home/app/src"
        ports:
          - "3001"
        container_name: "react-rails_app"
        command: "npm start"
      db:
        image: "postgres"
        container_name: "react-rails_db"
      api:
        build:
          context: "."
          dockerfile: "Dockerfile-rails"
        command: "bundle exec rails s -p 3000 -b '0.0.0.0'"
        volumes:
          - "./rails:/home/app/src"
        ports:
          - "3000"
        depends_on:
          - "db"
        container_name: "react-rails_api"
And your nginx conf:

    server {

      listen 80;
      server_name localhost;
      charset utf-8;

      location /dist {
        alias /usr/share/nginx/html/dist;
      }

      location /api {
        proxy_pass http://api:3000;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
      }

      location / {
        proxy_pass http://app:3001;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
      }

    }
This is a good reference boilerplate for setting up react and webpack with pre-rendering on the server: https://github.com/colinmeinke/universal-js