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

serve-reload-replace

v4.0.3

Published

simple http server with built-in live reload, server-sent events, server side includes, and more!

Downloads

3

Readme

SRR: Serve Reload Replace

simple http server with built-in live reload, server-sent events, server side includes, and more!

license release semantic

Features

  • Serve: Simple HTTP Server for static files
    • forced cache busting through Cache-Control: no-store headers
  • Reload: Automatically watches for file changes, and reloads pages
    • uses light weight Server Sent Events to notify browser with file changes
    • automatically injects watcher client
    • customize the client behavior with your own client script
  • Replace: Supports Server Side Includes directives

Install

$ npm install --global serve-reload-replace

Usage

$ srr --help

Usage: srr [options]
  --root     Path to serve & watch                                 default: $PWD
  --client   Path to custom EventSource client                     default: built-in
  --address  Specify network interface to use                      default: 0.0.0.0
  --port     Specify a port to use                                 default: 8080
  --index    Specify which file should be used as the index page   default: index.html
  --verbose  Verbose logging                                       default: false
  --help     Display Help
quick start
$ cd ~/project
$ srr

[02:02:46 PM] • Listening on 0.0.0.0 8080
[02:02:47 PM] • Watching files in /home/ahmad/project/

This will launch the server in the current working director and start watching local files for changes.

open a browser window and navigate to http://localhost:8080/ to start browsing.

The built-in EventSource client will automatically reload all pages whenever any file changes

NOTE: Future plans include selectively reloading resources in the browser.

with optional arguments & custom client
$ srr --root=~/projects/website/ --address=127.0.0.1 --port=2000 --client=js/my-client.js

[02:02:46 PM] • Listening on 127.0.0.1 2000
[02:02:47 PM] • Watching files in /home/ahmad/projects/website/

create a new file named my-client.js under ~/projects/website/js/

// connect to Server Sent Events special route
const sse = new EventSource(`${window.location.origin}/__events`);

sse.addEventListener("unlink", console.log);
sse.addEventListener("add", console.log);
sse.addEventListener("change", console.log);
sse.addEventListener("error", (event) => console.error("SSE error"));

NOTE: see Server Sent Events for more details.

open a browser window and navigate to http://127.0.0.1:2000/

[02:05:25 PM] • GET / ↦  200 OK
[02:05:25 PM] • SSE Client Connected: 1604257525819

with the server running, and your browser connected, edit / update / delete any file under your project folder and your client JS will receive events, while the server logs will show the events and the file path:

[02:10:15 PM] • change index.html
[02:11:30 PM] • add foo.html
[02:11:42 PM] • unlink foo.html

Server Sent Events

File system events are forwarded from Chokidar using Server Sent Events.

The built-in client is automatically served from the /__client endpoint, and it connects to the special path /__events which serves the events.

The built-in client simply listens to all event and executes a page reload through window.location.reload()

TODO:

  • Track actively opened files, and only notify relevant client sessions
  • Investigate using window.performance.getEntriesByType('resource') API to target specific elements per page / session (e.g. images / css)

Writing a custom SSE client

While the default behavior of the built-in client focuses on reloading the page content, you can replace it with your own client logic, simply point to the client path using --client argument.

Note: --client must be relative path to --root

client code
const sse = new EventSource(`${window.location.origin}/__events`);

sse.addEventListener("all", console.log);

See Using server-sent events article by Mozilla for more examples on what you can do.

Available events

add, addDir, change, unlink, unlinkDir, all

Note: for more details check out Chokidar's docs

Server Side Includes

The server will automatically process SSI directives:

Supported Directives

| directive | parameters | example | description | |------------|----------------|--------------------------------------|----------------------------------------------------------| | echo | var | <!--#echo var="NODE_ENV" --> | displays the value of the specified environment variable | | set | var, value | <!--#set var="foo" value="bar" --> | sets the value of an environment variable | | printenv | space | <!--#printenv space=" " --> | outputs a list of all environment variables as JSON |

API

You can use the serve-reload-replace programmatically as well:

const SRR = require('serve-reload-replace')

const instance = new SRR({
  verbose = false,
  root = process.cwd(),
  index = 'index.html',
  client
})

instance.start({
  address: '0.0.0.0',
  port: 8080
})

instance.stop()

See CLI Usage for available options.

Docker

Run as a docker image:

$ docker run -it -p 8080:8080 -v $(pwd)/www:/www ahmadnassri/serve-reload-replace
pass arguments and match the port and volume mount
$ docker run -it -p 3000:3000 -v /path/to/your/project:/my-project ahmadnassri/serve-reload-replace --port=3000 --root=/my-project

Author: Ahmad Nassri • Twitter: @AhmadNassri