npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

browser-refresh

v1.7.3

Published

Node.js module to enable server updates and restart and browser refresh in response to file modifications

Downloads

3,487

Readme

browser-refresh

This module improves productivity by enabling instant web page refreshes anytime a front-end resource is modified on the server. This module supports live reloading of CSS and JavaScript without doing a full page refresh. This module utilizes the very efficient chokidar module for watching for changes to the file system. Web sockets are used to communicate with the browser. Minimal application code changes are required to benefit from this module.

Overview

Like nodemon, this module provides a drop-in replacement for the node command.

Compared to nodemon, the browser-refresh module has the following benefits:

  • It starts as a web sockets server and provides a web sockets client
  • It sets an environment variable for the spawned child process to let it know that it was launched using browser-refresh
  • Instead of configuring which directories/files to watch, you instead configure which directories/files to not watch using an optional .browser-refresh-ignore file (same format as .gitignore and .npmignore).
  • There is an optional taglib for Marko and Dust that injects the browser-refresh client if the application was launched using browser-refresh. Please see: browser-refresh-taglib
  • The browser-refresh process waits for the child process to tell it that it is ready to start serving traffic so that the web browser page is not refreshed too soon. This is done by the child process using process.send('online')

File patterns to ignore are automatically loaded from the first file that exists in the following list:

  1. .browser-refresh-ignore file in the current working directory
  2. .gitignore file in the current working directory

If no ignore file is found then the following ignore file patterns are used:

node_modules/
static/
.cache/
.*
*.marko.js
*.dust.js
*.coffee.js

NOTE:

Patterns to ignore files with a directory should have / at the end. For example, to ignore node_modules directory use node_modules/.

Installation

First, install the global command line interface for the browser-refresh module:

npm install browser-refresh -g

Add the following code snippet to the appropriate location based on when your application is ready to start serving traffic:

if (process.send) {
    process.send('online');
}

For example:

app.listen(port, function() {
    console.log('Listening on port %d', port);

    if (process.send) {
        process.send('online');
    }
});

Alternatively, pass an object that specifies a url for browser-refresh to launch the first time your app starts:

if (process.send) {
    process.send({ event:'online', url:'http://localhost:8080/' });
}

Finally, add the following script to your page(s). Just before the closing </body> tag is a good place.

'<script src="{process.env.BROWSER_REFRESH_URL}"></script>'

When browser-refresh launches your app a new BROWSER_REFRESH_URL environment variable is added with the URL that should be used to include the client-side script. The value of BROWSER_REFRESH_URL will be similar to http://localhost:12345/browser-refresh.js (the port is random). You should use whatever means your templating language or UI library provides to add the script to your page(s).

If you're using Marko, checkout browser-refresh-taglib which allows you to simply drop the following tag into your template instead of using the above <script> tag:

<browser-refresh/>

Configuration

Some of the features of the browser-refresh module can be configured by creating a .browser-refresh JSON configuration file at the root of your project.

SSL Support

To enable SSL support you must provide values sslCert and sslKey in your .browser-refresh configuration file.

  • sslCert: The path to a SSL certificate
  • sslKey: The path to a SSL key

Example:

{
    "sslCert": "server.crt",
    "sslKey": "server.key"
}

Usage

Once you have installed browser-refresh using the directions provided above, you can then start your application as normal, except replace node with browser-refresh. For example:

# Old: node server.js
# New:
browser-refresh server.js

If the main property is configured correctly in your application's package.json then you can simply start your application using the following command:

browser-refresh

The browser-refresh command will pass all command line arguments to the child process. Therefore, you can pass any number of arguments to your application:

browser-refresh server.js --foo --bar

After launching your application using the browser-refresh command, you can then load any web page as normal. If the <browser-refresh> tag (or {@browser-refresh/} helper) were used then any time a resoure is modified then the application will be restarted and, then, when the server is ready a message will be sent to all of the connected browsers via a web socket connection to trigger a reload of the same web page.

Controlling Reloading

By default, this module does not try to be clever when handling a file modification. That is, by default, a full server restart and a full web page refresh are used whenever any type of file is modified on the server. This ensures that the server state and the client-side page state will always be correct and avoids frustrating edge cases. However, the browser-refresh module allows for modules to register "special reload" handlers that can short-circuit a full server restart. To disable a full server restart for a particular file pattern, the child process needs to send a message to the browser-refresh launcher process using the browser-refresh-client module.

For example, to enable special reloading, the following code can be used:

require('browser-refresh-client')
    .enableSpecialReload('*.foo *.bar')
    .onFileModified(function(path) {
        // Handle the modification of either a *.foo file or
        // a *.bar file...
    });

Both the marko and lasso modules provide support for enabling special reload handlers when using the browser-refresh module. Example usage:

require('marko/browser-refresh').enable();
require('lasso/browser-refresh').enable('*.marko *.css *.less *.styl *.scss *.sass *.png *.jpeg *.jpg *.gif *.webp *.svg');

To add your own special reload handlers for the browser-refresh module, please use the following code as a guide:

Refreshing CSS and Images

For improved developer productivity, this module supports refreshing of CSS and images without doing a full page refresh (similar to LiveReload). This is an opt-in feature that can be enabled using code similar to the following:

var patterns = '*.css *.less *.styl *.scss *.sass *.png *.jpeg *.jpg *.gif *.webp *.svg';

require('browser-refresh-client')
    .enableSpecialReload(patterns, { autoRefresh: false })
    .onFileModified(function(path) {
        // Code to handle the file modification goes here.

        // Now trigger a refresh when we are ready:
        if (isImage(path)) {
            browserRefreshClient.refreshImages();
        } else if (isStyle(path)) {
            browserRefreshClient.refreshStyles();
        } else {
            browserRefreshClient.refreshPage();
        }
    });

If you are using require('lasso/browser-refresh').enable(patterns), it is doing this for you automatically. Please see: lasso/browser-refresh/index.js

Passing arguments to node

Any flags (arguments that start with -) before the script path will be passed to the node executable:

browser-refresh --debug index.js

Maintainers

Contribute

Pull requests, bug reports and feature requests welcome.

License

ISC