Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatible with onAfterSetupMiddleware #4140

Closed
godky opened this issue Dec 29, 2021 · 1 comment · Fixed by #4141
Closed

Compatible with onAfterSetupMiddleware #4140

godky opened this issue Dec 29, 2021 · 1 comment · Fixed by #4141

Comments

@godky
Copy link
Contributor

godky commented Dec 29, 2021

Note that app.get(...) in onAfterSetupMiddleware is not the same as app.get(...) in setupMiddlewares.

It seems app.get() in setupMiddlewares are always applied before middlewares are applied, if we have a config like this:

{
    onAfterSetupMiddleware: server => {
        server.app.get('/__index__.html', (req, res) => res.send('foo'));
    },
    config.historyApiFallback = {
        index: '/__index__.html',
        disableDotRule: true,
    };
}

The route /__index__.html will take place for history api fallback, however if we changed it to this:

{
    setupMiddlewares: (middlewares, server) => {
        server.app.get('/__index__.html', (req, res) => res.send('foo'));
        return middlewares;
    },
    config.historyApiFallback = {
        index: '/__index__.html',
        disableDotRule: true,
    };
}

History api fallback are broken, we have to transform it into a middleware like:

middlewares.unshift({
    path: '/__index__.html',
    handler: (req, res, next) => {
        if (req.method === 'GET') {
            res.send('foo');
        }
        else {
            next();
        }
    }
});

In this way we have to add a request.method check in middleware implementation.

Originally posted by @otakustay in #4129 (comment)

@godky
Copy link
Contributor Author

godky commented Dec 29, 2021

Although there is a deprecate prompt, but the behavior of projects that used onAfterSetupMiddleware before will be inconsistent with the new version of webpack-dev-server

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant