LiveReload with BrowserSync and Gulp
In my previous blog post, I have written about how to use LiveReload Chrome extension, with Guard and some Ruby gems to make a web page automatically reload in a browser, Chrome browser to be specific. Just by reading this sentence, it already sounds like a complicated task. And indeed it is. Luckily, I have found a better solution: BrowserSync.
With LiveReload, you have to install browser extension, but BrowserSync uses Socket.io, so it can supports more than one browser at once. This is great for working with responsive design, where screens with different sizes are needed to be tested.
No need to install extension and support more than one browser are really big plus.
The following is a short instruction and some examples on how to use both BrowserSync and Gulp to automatically reload a web documentation page generated by Docco in any connected browser.
Install necessary modules as dependencies in package.json
:
|
|
Update gulpfile.json
to include a BrowserSync task:
|
|
The default task depends on task browser-sync
. It is necessary to first run the server before any livereload can happen.
To run the (default) task:
|
|
When changes were made to any of .js
file, it will first generate latest documentation in docs
directory, and then BrowserSync will automatically reload the browser.
Reload is not just happening to one browser, but all connected browsers, this is done via Socket.io. This is great for cross browser testing:
When you’re making responsive websites, there’s a lot of tweaking and testing to do. BrowserSync makes your workflow faster by synchronising URLs, interactions and code changes across multiple devices. - http://www.browsersync.io/
But there is a quirk, browserSync.reload
must be the last task in the watch array:
|
|
When the task executes:
|
|
From the log above, you can see that task #1 started earlier than task #2, but finished later. But BrowserSync reload happens after all previous tasks. Gulp is yet to support running tasks in series. But with browserSync.reload
, it always runs at last.
If changing the line with watched tasks to:
|
|
An error will be thrown:
|
|
Make sure browserSync.reload
is the last task in the list.
So, no additional extension to plug, no gems to mine, and no complex setup task, just npm install
.
Tools should be easy to use!