Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
A grunt task and reverse proxy that enables live reloading of updated files in the browser.
branch: master

This branch is 21 commits behind webxl:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
bin
tasks
test
.gitignore
LICENSE-MIT
README.md
grunt.js
package.json

README.md

grunt-reload

A grunt task that enables live reloading of updated watch files in the browser.

Getting Started

Install this grunt plugin next to your project's grunt.js gruntfile with: npm install grunt-reload

Then add this line to your project's grunt.js gruntfile:

grunt.loadNpmTasks('grunt-reload');

Documentation

This plugin provides the grunt task 'reload'. The 'reload' task is designed to be called via the watch task configuration and before the watch command in a custom task, such as default, in order to start the server.

The reload task tells the page to refresh itself via websocket connection between the reload server and the reloadClient.js that is appended to the requested html file. When the watch task detects a changed file, it will process its configured tasks, which should include the 'reload' task if it is setup like the example below.

Configuration:

  • port: (number, default: 8001) Reverse proxy listens on this port. This is necessary for including reload client javascript.
  • proxy: (object, optional) If present, this config object will enable a reverse proxy to your development server's content
    • host: (string, default: 'localhost') development server hostname
    • port: (number, default: server.port or 80) development server port
    • includeReloadScript: (boolean, default: true) includes the client js to listen for reload commands
  • iframe: (object, optional)
    • target: (string) URL of development server
  • liveReload: (boolean, only required for LiveReload 2.x)

Reload methods

Proxy

This will automatically append the script to a requested HTML file. Here's how you would use grunt-reload with grunt-less:

// project configuration
grunt.initConfig({
    lint: {
        all:['js/*.js']
    },
    reload: {
        port: 6001,
        proxy: {
            host: 'localhost'
        }
    },
    less: {
        style: {
            src: 'style.less',
            dest: 'style.css'
        }
    },
    watch:{
        files:['index.html', 'style.less'],
        tasks:'default less reload'
    }

});

grunt.loadNpmTasks('grunt-less');
grunt.loadNpmTasks('grunt-reload');

grunt.registerTask('default', 'lint less');

The reload server would listen at http://localhost:6001 and forward to requests to http://localhost.

IFrame

Use this method if you don't want to muck around with your server's response.

...
    iframe: {
        target: 'http://localhost:9999'
    }
...

Your iframe'd dev site in this instance would be available at the default http://localhost:8001

LiveReload extension

This is useful if you want to reload CSS files in place in addition to the entire page when files change. It requires a LiveReload extension. In-line reloading of CSS & images requires grunt 0.4 and the 1.x version of LiveReload.

Set the reload server to listen on LiveReload's default port:

...
    port: 35729, // LR default
    liveReload: {}
...

Make sure you enable LR in your browser (click the LR button in Chrome).

Manual include

If you prefer hard-coding over proxies, extensions, and iframes, you can just do this:

<script>__reloadServerUrl="ws://localhost:8001";</script>
<script type="text/javascript" src="//localhost:8001/__reload/client.js"></script>

As with the extension, this makes the reload client work directly from your dev server.

Usage

grunt reload watch

If you want to run a static server using the bundled server task, and enable reloading, you can configure something like this:

...
    reload: {
        port: 6001,
        proxy: {
            host: 'localhost',
            port: 8000 // should match server.port config
        }
    },
    watch:{
        files:['index.html', 'style.less'],
        tasks:'lint less reload'
    }
...
grunt.registerTask('default', 'server reload watch');

In this case, you can just run the default task:

grunt

TODO

  • reload resources without refreshing entire page use LiveReload extensions and grunt 0.4 alpha
  • add option to run standalone web server for project use server task for now
  • write chrome extension to reload resources (css, images, templates)
    • the includeReloadScript & proxy options will probably become the fallback method of attaching the client
    • may allow one of three attach methods: extension, iframe, or proxy

Release History

  • 06/15/2012 - 0.2.0: Added support for grunt 0.4, LiveReload extensions, iframes, and custom targets
  • 06/04/2012 - 0.1.2: Removed connect 1.x requirement (no longer using connect.router). Added test. Clean up.
  • 06/03/2012 - 0.1.1: Fixes 'socket hang up' error.
  • 05/27/2012 - 0.1.0: Initial release.

License

Copyright (c) 2012 webxl
Licensed under the MIT license.

Something went wrong with that request. Please try again.