-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
HtmlWebpackPlugin + webpack-dev-middleware other pathname than / #145
Comments
The reason is that webpack-dev-middle ware doesn't write the index.html on disk but keeps it in memory for better performance. You could try:
|
I know ,) It's good for dev process of course. And yes, of course, I tried. But plugin do not write index.html when webpack-dev-middle work. The is the reason why I make issue. |
Did you try the proposed solution? |
I thought about it. It's really not a bug. But sorry may be my suggestion is not great. I hope you can make better. But anyway. // file like HtmlWebpackPlugin.config.js
export default {
filename: '..',
minify: {...}
} // webpack.config.js
import htmlConfig from HtmlWebpackPlugin.config
...
plugins: [
...,
new HtmlWebpackPlugin(htmlConfig)
]
... // webpack-dev-server.js
...
app.use(new HtmlWebpackPlugin(htmlConfig).middleware) |
Maybe it's separate package like webpack-html-middleware. I'm really not sure ,)) |
@jantimon Your solution doesn't work. Still have app.use(require('webpack-dev-middleware')(compiler, {
noInfo: true,
publicPath: config.output.publicPath
}));
app.use(require('webpack-hot-middleware')(compiler));
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname, 'index.html'));
});
plugins: [
new HtmlWebpackPlugin({
template: 'src/index.html',
filename: 'index.html',
inject: 'body'
}),
] |
Hi, I found a solution which should solve it for you: app.get('*', function (req, res, next) {
req.url = 'index.html';
next('route');
}); |
Now it says: |
Hello @jantimon, I tried this way before. Unfortunately does not work (for me). req.url = 'index.html';
// and
req.url = '/'; and etc. result is: Cannot GET /bla-bla-bla |
var express = require('express');
var app = express();
var webpack = require('webpack');
var path = require('path');
var compiler = webpack(require('./webpack.config.js'));
app.use(require('webpack-dev-middleware')(compiler, {
noInfo: true,
publicPath: '/'
}));
app.use('*', function (req, res, next) {
var filename = path.join(compiler.outputPath,'index.html');
compiler.outputFileSystem.readFile(filename, function(err, result){
if (err) {
return next(err);
}
res.set('content-type','text/html');
res.send(result);
res.end();
});
});
app.listen(3000); |
@jantimon Sorry, it's work. Thank-you! ps: I missed |
It works! Thank you @jantimon. This solution should go to README. |
Well actually it is just webpack-dev-server.. This is just a plugin ;) |
this solution is awesome, thks @jantimon |
thks! @jantimon |
1 similar comment
thks! @jantimon |
sweet! @jantimon |
谢谢!@jantimon |
This was so hard to find, thanks a lot @jantimon. |
@jantimon
however, I'm sure that the file is there, because it works for the urls which are not 404. My OS is Any suggestion is appreciated. Regards, |
Wow ... I searched for hours until I find this snippet. @jantimon THANK YOU! it is crazy how hard it is to find this. Saved my bacon! Probably hard to find because it is missing keywords like express, SPA, routing. |
Considering the fact how much upvotes this issue still gets, adding this to docs might be a good idea :) |
Because my express URL path ( var express = require('express');
var app = express();
var webpack = require('webpack');
var path = require('path');
var compiler = webpack(require('./webpack.config.js'));
var devMiddleware = require('webpack-dev-middleware');
app.use(devMiddleware(compiler, {
noInfo: true,
publicPath: '/'
}));
app.use('*', function (req, res, next) {
var filename = path.join(compiler.outputPath,'index.html');
devMiddleware.waitUntilValid(() => {
compiler.outputFileSystem.readFile(filename, function(err, result){
if (err) {
return next(err);
}
res.set('content-type','text/html');
res.send(result);
res.end();
});
});
});
app.listen(3000); |
@jantimon I love you thank you so much. |
Use html-webpack-harddisk-plugin after HtmlWebpackPlugin. It keeps plugins: [
new HtmlWebpackPlugin({
alwaysWriteToDisk: true
}),
new HtmlWebpackHarddiskPlugin()
] ...achieve this: app.get('*', (req, res) => {
// for example
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
}) |
@walandemar Thank you. That works great. |
@walandemar Thank you,it works for me!!! |
Error: no such file or directory |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Hello,
First, thank you for your wonderful work!
Second ,) My issue, I tried to use HtmlWebpackPlugin with dev server as middleware.
All is fine when path / but when path is /login webpack-dev-middleware return 404
Then I need something like:
but for HtmlWebpackPlugin or may be if it's possible, fix it by option of HtmlWebpackPlugin.
The text was updated successfully, but these errors were encountered: