reloader-client v0.1.0
reloader-client
A Node.js module to generate a script to reload the current page or its styles in response to server-sent events
Installation
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:
- Open a connection to the server at a given
url
to receive events from it. - When the script receives an event from the server,
- Reload the current page if the event
data
equals toDOCUMENT_RELOAD_SIGNAL
. - Reload stylesheets without reloading the current page if the event
data
equals toCSS_RELOAD_SIGNAL
.
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>
data
must equal to eitherreloaderClient.DOCUMENT_RELOAD_SIGNAL
orreloaderClient.CSS_RELOAD_SIGNAL
.id
must be different from the previous event's.
License
ISC License © 2019 Shinnosuke Watanabe