Skip to content

behavior of static and historyApiFallback #2716

Open

Description

@knagaitsev

Expected Behavior

Static serving middleware for the dev server should only need to be applied to the express server once via this.app.use(...)

Actual Behavior

static middleware is applied to the express server multiple times in order to make historyApiFallback work as expected. This is explained here: #2670 (comment)

A similar thing seems to have been added for the middleware feature as seen here, as it is added multiple times:

runnableFeatures.push('setup', 'before', 'headers', 'middleware');
if (this.options.proxy) {
runnableFeatures.push('proxy', 'middleware');
}
if (this.options.contentBase !== false) {
runnableFeatures.push('contentBaseFiles');
}
if (this.options.historyApiFallback) {
runnableFeatures.push('historyApiFallback', 'middleware');

Solution

We need to find a way to either apply all of this middleware once without having any breaking changes, or have intentional slight breaking changes while only applying the middleware once. We should also look into if applying the same middleware many times causes performance losses, or if it is an acceptable thing to do.

Activity

  1. csr632 commented on Feb 1, 2021

    @csr632

    Yes, if the proxy feature or the historyApiFallback feature is enabled, http-proxy-middleware will be applied to the express server multiple times.

  2. alexander-akait commented on Sep 13, 2021

    @alexander-akait
    Member

    historyApiFallback also break HEAD request in some cases, we need fix it, more information here webpack/webpack-dev-middleware#749

  3. alexander-akait commented on Jun 28, 2022

    @alexander-akait
    Member

    Partially fixed #4501

  4. BRMatt commented on Sep 28, 2022

    @BRMatt

    I ran into this issue earlier and spent a few hours trying to debug it. In my case it turned out that configuring the webpack-dev-server proxy option with context and router options, but not target causes webpack-dev-server to ignore the context option when configuring the proxy:

    const getProxyMiddleware = (proxyConfig) => {
    // It is possible to use the `bypass` method without a `target` or `router`.
    // However, the proxy middleware has no use in this case, and will fail to instantiate.
    if (proxyConfig.target) {
    const context = proxyConfig.context || proxyConfig.path;
    return createProxyMiddleware(
    /** @type {string} */ (context),
    proxyConfig
    );
    }
    if (proxyConfig.router) {
    return createProxyMiddleware(proxyConfig);
    }
    };

    My understanding is that if you're using router you don't need to specify target, but if I don't then webpack-dev-server configures the proxy to intercept every request, rather than just the requests that match the context.

    In my case I'm only proxying a single endpoint, so I've configured context and target to the same value, and let the router reconfigure the target on each request. This appears to fix the conflict with historyApiFallback.

  5. alexander-akait commented on Sep 28, 2022

    @alexander-akait
    Member

    I think we can improve/fix:

    return createProxyMiddleware(proxyConfig); 
    

    So we will always pass options even you don't have router, what do you think?

  6. added a commit that references this issue on Apr 11, 2024
    a39fb68
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions