0.1.0 • Published 5 years ago

reloader-client v0.1.0

Weekly downloads
5
License
ISC
Repository
github
Last release
5 years ago

reloader-client

npm version Build Status Build status codecov

A Node.js module to generate a script to reload the current page or its styles in response to server-sent events

Installation

Use npm.

npm install --save-dev reloader-client

API

const reloaderClient = require('reloader-client');

reloaderClient(url)

url: string URL (an absolute or relative URL)
Return: string

It returns a client-side JavaScript code that does the followings:

  1. Open a connection to the server at a given url to receive events from it.
  2. When the script receives an event from the server,

reloaderClient.legacy(url)

url: string URL
Return: string

It essentially does the same thing as reloaderClient() does, but returns a script that is compatible with Edge and Internet Explorer, neither of whom supports EventSource API.

reloaderClient.DOCUMENT_RELOAD_SIGNAL

Type: string

reloaderClient.CSS_RELOAD_SIGNAL

Type: string

Usage

1. Generate a script with reloaderClient() (or with reloaderClient.legacy() for Microsoft browsers).

$ node -p "require('reloader-client')('/sse-url')"
> const KEY = 'reloader-client-last-reload-event-id' ...

2. Include a generated script to an HTML and serve it in a server.

<head>
  <script>/* ... paste the generated code here ... */</script>
</head>

3. Whenever a reload should be performed, send an event to the client in the following form:

id: <any>
data: <reloaderClient.DOCUMENT_RELOAD_SIGNAL|reloaderClient.CSS_RELOAD_SIGNAL>

License

ISC License © 2019 Shinnosuke Watanabe