Book Image

NW.js Essentials

Book Image

NW.js Essentials

Overview of this book

Table of Contents (17 chapters)
NW.js Essentials
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Live reloading NW.js


While developing the frontend of the application, it comes in really handy to be able to reload the application interface at each change in the code. There are many approaches to live reload; let's see a couple of them.

If you're working on a single folder, all you need to do is leverage Node.js by adding the following code snippet directly at the end of your file:

<script>
var path = './',
  fs = require('fs');
fs.watch(path, function() {
  if (location) location.reload();
});
</script>

If you need a more complete solution by watching files recursively, you'll have to adopt a watch library, such as Gaze, Gulp, or Chokidar. Let's see another example, leveraging Gulp this time. First, you need to install Gulp (manually or through the manifest file) and then add the following piece of code at the end of your file:

<script>
var gulp = require('gulp');
gulp.task('reload', function () {
  if (location) location.reload();
});
gulp.watch('**/*', ['reload']);
...