webpack-dev-server: Cannot run at non-web root URL - remote development

  • Operating System: tested on macOS and remote dev server Debian Stretch
  • Node Version: 12.x
  • NPM Version: 6.9
  • webpack Version: 4.41.2
  • webpack-dev-server Version: 3.9.0
  • Browser: Chrome
  • This is a bug
  • This is a modification request

Code

// webpack.config.js
const webpack = require("webpack");
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");

const config = {
  entry: ["react-hot-loader/patch", "./src/index.js"],
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "[name].[contenthash].js"
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        use: "babel-loader",
        exclude: /node_modules/
      },
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader"]
      }
    ]
  },
  resolve: {
    extensions: [".js", ".jsx"],
    alias: {
      "react-dom": "@hot-loader/react-dom"
    }
  },
  devServer: {
    contentBase: "./dist",
    publicPath: "/testing"
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: require("html-webpack-template"),
      inject: false,
      appMountId: "app"
    })
  ],
  optimization: {
    runtimeChunk: "single",
    splitChunks: {
      cacheGroups: {
        vendor: {
          test: /[\\/]node_modules[\\/]/,
          name: "vendors",
          chunks: "all"
        }
      }
    }
  }
};

module.exports = (env, argv) => {
  if (argv.hot) {
    // Cannot use 'contenthash' when hot reloading is enabled.
    config.output.filename = "[name].[hash].js";
  }

  return config;
};

// additional code, remove if not needed.

Expected Behavior

Setting publicPath should allow the dev server to serve the app at a non-web-root url (i.e. domain.com/urlnamehere vs. domain.com/ ).

This is particularly an issue when working with remote development a la VS Code as the app can’t run on root and must be run at an alternate URL. This isn’t a technical requirement, but a require in my environment.

Actual Behavior

When using the publicPath, for example, of /testing, and setting Nginx with a proxy_pass, the app is “served” by Nginx, but displays 'Cannot get /testing`.

The built app works with webpack prod build. It serves the app fine. It’s the dev server that won’t.

Note that on the same server, with the same configuration, I have an Express API running at a non-web-root URL; I am 100% familiar with how to configure this on the Nginx side.

Locally, setting publicPath to /testing simply shows a white page, whether it’s accessed at root or at /testing.

**If this isn’t the right way to do this, please tell me how I can run the dev server on a remote server at /testing (and not web root). Thanks. **

For Bugs; How can we reproduce the behavior?

Change the publicPath value, watch it not work.

For Features; What is the motivation and/or use-case for the feature?

My primary need is developing remotely. This is 100% necessary as I am working with an iframe which requires same-origin to develop features which target the iframe; bringing the iframe in remotely makes the content of the iframe inaccessible due to browser security.

It’s also silly to not be able to use an alternate URL path to serve from.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 21 (10 by maintainers)

Most upvoted comments

I’m not sure how to communicate this any other way.

Maybe you can create couple containers (docker) and provide small readme what is you expected?

As I said above, regarding my Nginx config:

When I hit domain.com/webpack Nginx sends the request to port 3000, WDS responds.

Everything works, except WDS doesn’t render the site, and console shows it’s not looking in the relative path for it’s files - it’s looking at domain.com/ instead of domain.com/webpack.

WDS is clearly able to run on the non-web root URL, it simply doesn’t seem to be able to translate that to finding it’s files.

  • I’ve worked around this, sort of, by instead pointing Nginx to the dist directory and manually reloading the browser after running a new build with webpack -p --mode production. It’s not sexy, and it’s slow and tedious but it allows me to do remote development.

Ideally, WDS would allow specifying a non-web root URL as its web root.

An example might be WordPress - it allows setting a base URL, or what it calls a “home” and “site” URL. This concept is similar, but not the same. Maybe it’s something you’re familiar with, though.