-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Watch rails views and reload page on changes #1879
Comments
From what I can tell the stock (webpack) devServer only pays attention to files that are resolved by webpack. I use the webpack |
Not quite as nice as using webpack itself, but I've got this working by using watchexec. I run up webpack-dev-server, then run:
|
There's an even easier way, just set the environment.config.devServer.watchContentBase = true
environment.config.devServer.contentBase = [
path.join(__dirname, '../../app/views')
] |
Here's an example that won't cause webpack to do a full recompile. This will simply trigger a browser reload. Your specific watches may vary, but they're pretty easy to configure using an array of glob syntax w/ chokidar (which is already included by webpack) - just note that it doesn't accept regexes: development.js: const chokidar = require('chokidar')
environment.config.devServer.before = (app, server) => {
chokidar.watch([
'config/locales/*.yml',
'app/views/**/*.slim'
]).on('change', () => server.sockWrite(server.sockets, 'content-changed'))
} |
Hey, I can't seem to get this working. The "chokidar" isn't watching any changes in *.html.erb files |
Yes, my glob was for slim templates, which are what I use. I've modified the code above to look for erb files (though anyone can & should modify these globs for their needs) |
Thank you for that. I'm not sure what the issue was but after closing my terminal and restarting my server, it worked like a charm! |
thanks, @swrobel that worked great! Can confirm this works on Rails 6.0.3.1, webpacker 4.2.2 👍 My process.env.NODE_ENV = process.env.NODE_ENV || 'development'
const environment = require('./environment')
const chokidar = require('chokidar')
environment.config.devServer.before = (app, server) => {
chokidar.watch([
'config/locales/*.yml',
'app/views/**/*.erb'
]).on('change', () => server.sockWrite(server.sockets, 'content-changed'))
}
module.exports = environment.toWebpackConfig() |
Thanks @cabgfx |
Using
|
For what it's worth, I had to add the option "awaitWriteFinish" or else the reload did not always successfully show the changed file (and I had to either manually refresh, or hit save a second time). The relevant section looks like this on webpacker 6:
|
Make sure of
|
|
THANKS all! much smoother work with that settings. Question: Does anyone know if that could be possible? |
Adding |
Setting This is what we're currently using with Rails 6.1, webpacker 6.0.0-rc.5, webpacker 5, webpack-dev-server 4: // config/webpack/development.js
module.exports =
process.env.RAILS_ENV == "development"
? merge(webpackConfig, {
devServer: {
onBeforeSetupMiddleware: (devServer) => {
chokidar
.watch([
"config/locales/*.yml",
"app/views/**/*.html.erb",
"app/assets/**/*.scss",
])
.on("change", () =>
devServer.sendMessage(
devServer.webSocketServer.clients,
"content-changed"
)
)
},
},
})
: webpackConfig
This did not work for us. If somebody can get this to work reliably with |
For anyone having CI issues/packs-tests issues, I had to wrap the above code in a condition so RSpec didn't complain. process.env.NODE_ENV = process.env.NODE_ENV || 'development'
const environment = require('./environment')
const chokidar = require('chokidar')
if (process.env.RAILS_ENV === 'development') {
environment.config.devServer.before = (app, server) => {
chokidar.watch([
'config/locales/*.yml',
'app/views/**/*.erb'
],
).on('change', () => server.sockWrite(server.sockets, 'content-changed'))
}
}
module.exports = environment.toWebpackConfig() |
I'm currently using webpacker with page reloading on css/js changes and it all works just great. Thanks for this amazing piece of software. I was wondering - would that be possible to do the same for rails views? It would be great if updating erb or slim view could trigger a webpack page refresh (ideally without rebuilding entire js/css bundle).
I'm fully aware that's not the point of using webpack(er) and know there are solutions like guard and livereload for that but i would love to avoid using yet another deamon running in the background.
Thanks in advance for any clues!
The text was updated successfully, but these errors were encountered: