-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Provide migration guide for setupMiddlewares #4129
Comments
Beforemodule.exports = {
//...
devServer: {
onAfterSetupMiddleware: function (devServer) {
if (!devServer) {
throw new Error('webpack-dev-server is not defined');
}
devServer.app.get('/some/after/path', function (req, res) {
res.json({ custom: 'response-after' });
});
},
onBeforeSetupMiddleware: function (devServer) {
if (!devServer) {
throw new Error('webpack-dev-server is not defined');
}
devServer.app.get('/some/before/path', function (req, res) {
res.json({ custom: 'response-before' });
});
},
},
}; Aftermodule.exports = {
// ...
devServer: {
setupMiddlewares: (middlewares, devServer) => {
if (!devServer) {
throw new Error('webpack-dev-server is not defined');
}
// onBeforeSetupMiddleware
devServer.app.get('/some/before/path', (_, response) => {
response.json({ custom: 'response-before' });
});
// onAfterSetupMiddleware
middlewares.push({
name: 'after-middleware',
// `path` is optional
path: '/some/after/path',
middleware: (req, res) => {
res.json({ custom: 'response-after' });
},
});
// another after middleware
middlewares.push((req, res) => {
res.send('Hello World!');
});
return middlewares;
},
},
}; I believe this should be the migration. /cc @alexander-akait |
Thanks @snitin315 🙏🏻 How would you migrate a before and after block with
|
|
Note that It seems {
onAfterSetupMiddleware: server => {
server.app.get('/__index__.html', (req, res) => res.send('foo'));
},
config.historyApiFallback = {
index: '/__index__.html',
disableDotRule: true,
};
} The route {
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 |
Yes, it is good solution 👍 |
(node:9304) [DEP_WEBPACK_DEV_SERVER_ON_AFTER_SETUP_MIDDLEWARE] DeprecationWarning: 'onAfterSetupMiddleware' option is deprecated. Please use the 'setupMiddlewares' option. Can any one please tell me how solve to this error my localhost is not opening after i get this error. |
i am facing same issue how i use that code in node module files |
where i can find file to isert modified code |
|
@snitin315 I can not find where to replace the code with the updated segment, what is the module this goes in? |
@darkcohiba inside |
@snitin315 this is my webpack config js dev server, I don't see what I need to replace; `// @remove-on-eject-begin
const fs = require('fs'); const host = process.env.HOST || '0.0.0.0'; module.exports = function (proxy, allowedHost) {
}; |
@snitin315 I don't think the question to migrate onBeforeSetupMiddleware with use instead of get has been answered. How would migrate something like this? onBeforeSetupMiddleware: function (devServer) {
devServer.app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
next();
});
}
}, I tried using unshift as mentioned in the DevServer documentation, but without success. |
FIgured it out! setupMiddlewares: function (middlewares) {
middlewares.unshift(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
next();
});
return middlewares;
}, |
Documentation Is:
Please Explain in Detail...
The recent change for issue #4068 deprecates onAfterSetupMiddleware and onBeforeSetupMiddleware. The commit making the change provides a new example, but there's no migration guide on how to migrate away from the deprecated configuration options.
Your Proposal for Changes
The text was updated successfully, but these errors were encountered: