-
Notifications
You must be signed in to change notification settings - Fork 809
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
Hot reloading not working with laravel/sail [laravel 8] #2719
Comments
Hi @moracabanas, I have gotten one step closer to getting this working by:
Now when I run I will report back if I figure out how to solve this next issue. Edit: |
My setup is exactly the same as @moracabanas, except that I am using Laravel Mix 6.0.6. I finally got this to work (more or less) after tinkering around a lot. I don't get any output regarding HMR when I run What I do see is that webpack picks up my changes because I see a fresh Initially I couldn't get anything to load when visiting I also tried adding the So I added the following code into
By doing the above and after running After that, I found out that I can just visit So I added And indeed, after changing something in a Vue template, the change is reflected instantly in the browser! Hopefully this can help you out too? It looks like it is currently not possible to easily change the URL to something else besides PS: if you edit the |
Update! I read the Laravel Mix code a bit better.. It looks like the Webpack Config options can override the options set by
Those 2 combined, along with the 8080 port binding in Do keep in mind that you just need to visit the normal URL without :8080 appended. I didn't know this at first. |
Quick tip: If you also want to reload the browser automatically and quickly when you edit
and in the webpack config:
Definitely beats adding an additional BrowserSync, which would take a bit of effort to get it to work together well with devServer HMR. (I couldn't get it working well, so I looked for alternatives and found chokidar). |
Thanks you for your work you put into the research about this issue. I am looking into it today. EDIT: I also used the laravel mix 6 with Laravel 8 upgrade from this guide. And then I downgraded bach to the tailwind patch because I thought it was a Mix 6 issue. |
The solution in @jameshulse comment solved the problem for me. |
I will test it too. Maybe this behavior could be related to limited docker resources, in my case I explicitly limited WSL2 to take only 4Gb of ram and 4 cores. |
This is not working for me. After getting running const mix = require('laravel-mix');
const chokidar = require('chokidar');
/*
|--------------------------------------------------------------------------
| Mix Asset Management
|--------------------------------------------------------------------------
|
| Mix provides a clean, fluent API for defining some Webpack build steps
| for your Laravel applications. By default, we are compiling the CSS
| file for the application as well as bundling up all the JS files.
|
*/
mix.js('resources/js/app.js', 'public/js')
.postCss('resources/css/app.css', 'public/css', [
//
]);
mix.options({
hmrOptions: {
host: 'localhost',
port: 8080,
},
});
mix.webpackConfig({
devServer: {
host: '0.0.0.0',
port: 8080,
onBeforeSetupMiddleware(server) {
chokidar.watch([
'./resources/views/**/*.blade.php'
]).on('all', function() {
server.sockWrite(server.sockets, 'content-changed');
})
},
},
}); Note I only add your suggested changes following Laravel's webpack config override guide using mix. Then I run App is properly working on This is the latest Laravel 8 with Mix 6.0.6 by default. It appreciate it so much if anyone could point me in the right direction about this. |
But HMR does work for you? (For example for Vue files and/or for CSS changes?) And you also set up the 8080 port binding in Docker? It does look very similar to what I have here. The only difference is that I import the webpackConfig from a separate file, but I don't think that this is an issue: webpack.mix.js
webpack.config.js
There's a few extra rules in there, but they are just for my specific project (postCss, vue-i18n) Can you tell me what OS and Laravel versions you are running? PS: In case you are only working with CSS and Blade files (not Vue or anything) you could probably also use something like BrowserSync instead |
I got lost as well. After setting up everything as @synio-wesley described above, I can see that changes are detected:
I can see the newest version of js file under Maybe this has something to do with the way I embade js script in a blade template: I've been pulling my hairs out for hours trying to figure out where webpack is puts this app.js file. It must be somewhere if it's available throught Any help would be greatly appreciate 🙏 webpack.mix.js mix.js("resources/js/app.js", "public/js")
.react()
.sass("resources/sass/app.scss", "public/css");
mix.options({
hmrOptions: {
host: "localhost",
port: 8080,
},
});
mix.webpackConfig({
devServer: {
host: "0.0.0.0",
port: 8080,
},
}); |
It is very important that you use Laravel's global This is referenced (for production purposes) here: https://laravel.com/docs/8.x/mix#versioning-and-cache-busting Does that work for you? |
Thank you @synio-wesley. I made a step forward.
So my react app is not rendering at all, but I can see that files like |
What finally, simply worked for me was the following:
Note that the |
@alexpcoleman It works! 👍 |
I tried this method and couldnt get it to work, are you using the command |
@cthom-dev try this:
Btw, thanks @synio-wesley so much! The use of |
With laravel-mix 6.0.16 running Windows WSL2 with Ubuntu and sail @alexpcoleman solution worked. mix.webpackConfig({
...
devServer: {
host: "0.0.0.0"
}
}); I did have to expose 8080 in docker-compose.yml. I didn't need to specify the port in devServer config as 8080 seems to be the default. |
I think it could be useful for us to have some kind of CLI flag that sets these or for us to somehow detect we're in a docker container / sail and change the dev server defaults. A PR doing this would be very welcome. |
I've managed to get this working (kind of) but I have two issues that I haven't seen mentioned yet.
Problem
Any ideas? Thanks! |
The last part to get this setup for me so it simply runs with was to run command
everyone in this thread thank you for your help |
Here's what works on mine and what I found out about the difference between hmrOptions My environment is: docker-compose.yml
webpack.mix.js
Accessing http://laravel.test:8085 would result in replacing
Looks like the devServer configuration is for configuring the container side on where |
Hello, for everybody still having problems with the solution above (for a reason it didn't work for me). In my case I am using inertia.js, which seems to cause problems when not running the For me watch-poll and browserSync did the trick for 100% hot reload experience, by calling here my webpack.mix.js
and my docker compose:
|
why im getting this error when i execute |
A million thanks to all of you, I've been trying to solve this for a long time and today I got it thanks to the addition of several answers here! |
@dranzd thanks a lot, your solution is what worked for me. |
Was able to get HMR working after upgrading mix version 2 to 6! However, my console no longer indicates / shows the [HMR] on page load though I do see component changes on the fly. Anyone else experience this? This is while running |
In case anyone else got the onAfterSetupMiddleware(server) {
chokidar.watch([
'./resources/views/**/*.blade.php'
], { ignoreInitial: true }).on('all', function() {
server.sendMessage(server.webSocketServer.clients, 'content-changed');
})
}, |
@synio-wesley comment worked for me. |
Hi! Unfortunately, this solution doesn't really work for me. The server detects changes and reloads the browser accordingly, but what I expected to happen is a hot replacement of the file that has been changed, so app state doesn't get lost. I added all the bits that you guys provided along the way and edited them accordingly. Any help would be greatly appreciated! webpack.mix.js const mix = require('laravel-mix');
/*
|--------------------------------------------------------------------------
| Mix Asset Management
|--------------------------------------------------------------------------
|
| Mix provides a clean, fluent API for defining some Webpack build steps
| for your Laravel application. By default, we are compiling the Sass
| file for the application as well as bundling up all the JS files.
|
*/
mix
.js('resources/js/app.js', 'public/js')
.sass('resources/sass/app.scss', 'public/css');
mix
.autoload({ jquery: ['$', 'window.jQuery', 'jQuery'] })
.extract();
mix.webpackConfig(require('./webpack.config')); webpack.config.js const path = require('path');
const chokidar = require('chokidar');
module.exports = {
resolve: {
alias: {
'@': path.resolve('resources/js'),
},
},
devServer: {
host: '0.0.0.0',
onBeforeSetupMiddleware(server) {
chokidar.watch([
'./resources/views/**/*.blade.php'
]).on('all', function () {
server.sendMessage(server.webSocketServer.clients, 'content-changed');
})
},
},
}; |
I got it to work for me using the following documentation: Going through this documentation:
You need to run this command: You do not need to have any extra webpack configuration files. Also, don't forget to change the Let me know if it works for anyone else. |
Above answer is working perfectly but there are two issues here.
So in that case we can create dummy javascript file and call in blade like this for example. and then inside webpack.mix.js, add entry for that dummy file which is made only for HMR purpose. This file will be used only for HMR purpose and not for commit. In that way we can comment out other unnecessary files for time being from webpack.mix.js which are not needed to be recompiled every time only a single file is changed, because webpack compiles all files which are under mix stream even if change is only made in css file. And even, if there is any error in other files it will not stop working of HMR. And not to mention that all commented out files from webpack.mix.js will be under |
node -v
): v15.5.0npm -v
): 7.3.0Description:
Hot reloading is not working using laravel/sail.
Steps To Reproduce:
I have tried with
sail npm run watch
sail npm run hot
and nothing seems to work.Also when I run
npm run hot
it output this:but clicking on
http://localhost:8080/
actually openshttp://localhost:14252/
which is so weird.I also tried to add
EXPOSE 8080
to theDockerfile
and8080:8080
to thedocker-compose.yml
then I rebuilt the image and but still not working and I can't see any side effect.Any blade template I made changes on and save it, there is no autoreload on any browser. I have to activelly reload the page to check every change I made.
The text was updated successfully, but these errors were encountered: