question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BrowserSync: Sass compilation causing whole page reload

See original GitHub issue
  • Laravel Mix Version: 1.4.2 (github:jeffreyway/laravel-mix#5aad0dfde5349e284e0876388a6f307547f7c002)
  • Node Version (node -v): 8.2.1
  • NPM Version (npm -v): 5.3.0
  • OS: macOS Sierra 10.12.6

Description:

When using browserSync, css changes are injected however the page is also reloaded due to the fact that the JS bundle is re-emitted even when the only file that changed was a sass file.

Steps To Reproduce:

webpack.mix.js:

mix
	.js('resources/assets/js/main.js', 'public/assets/js/bundle.js')
	.extract(['jquery', 'babel-polyfill'])
	.autoload({
		jquery: ['$', 'jQuery', 'window.jQuery']
	})
	.sass('resources/assets/sass/style.scss', 'public/assets/css')
	.disableNotifications()
	.options({
			processCssUrls: false
	})
	.sourceMaps()
	.browserSync({
		proxy: process.env.APP_URL,
        files: [
            'app/**/*.php',
            'resources/views/**/*.php',
            'public/assets/js/**/*.js',
            'public/assets/css/**/*.css'
        ]
    })

Run npm run watch, site is served at localhost:3000, save a sass file and watch the browser refresh rather than just inject the changes.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:18
  • Comments:55 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
cgodocommented, Apr 25, 2021

I finally found a workaround for my case.

I believe the issue is that laravel-mix’s CustomTasksPlugin runs all post-build tasks again for every rebuild. In these tasks is where all files built using .babel(), .scripts(), .combine(), .styles() and even .copy() are built again.

Here is a drop in plugin that ensures these tasks only run at the first build. Subsequent watch builds won’t rebuild all those files again unless you actually modified one of their files. This fixed all my browserSync reload problems.

mix.webpackConfig({
  plugins: [{
    apply(compiler) {
      // Intercept done hook to modify CustomTasksPlugin tap function
      compiler.hooks.done.intercept({
        register: (tapInfo) => {
          let firstRun = true;
          if (tapInfo.name === "CustomTasksPlugin") {
            const fn = tapInfo.fn;
            tapInfo.fn = (stats, callback, ...args) => {
              // Only run tap function (tasks) for first build
              if (firstRun) {
                fn(stats, callback, ...args);
                firstRun = false;
              } else {
                callback();
              }
            };
          }
          return tapInfo;
        }
      })
    }
  }],
});
4reactions
Grawlcommented, Apr 25, 2018

@karneaud the only workaround is to use mix.standaloneSass or just move styles build process out of Webpack

Read more comments on GitHub >

github_iconTop Results From Across the Web

BrowserSync reloading the whole page instead of injecting CSS
The problem is that I'm trying to use BrowserSync to reload the page when a .php file changes and to inject my css...
Read more >
BrowserSync/Live-Server not updating in Gulp File
Hi TeddyS31,. Gulp will create real-time changes in your live server browser if you run command gulp mdb-go in the background. It will...
Read more >
Quick setup for Gulp 4 + Browsersync + Sass - Coder Coder
It works by spinning up a local server and syncing it up to your browser. Then, anytime you make a change in your...
Read more >
Sage 10 browsersync issue with reload - Roots Discourse
Is there something to change in the mix config ? And is there a way to make the compiling faster. Apparently If I...
Read more >
Gulp for Beginners | CSS-Tricks
If you're crazy enough, you can even build a static site ... We've now managed to compile all Sass files into CSS files...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found