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-clientAPI
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
urlto receive events from it. - When the script receives an event from the server,
- Reload the current page if the event
dataequals toDOCUMENT_RELOAD_SIGNAL. - Reload stylesheets without reloading the current page if the event
dataequals 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>datamust equal to eitherreloaderClient.DOCUMENT_RELOAD_SIGNALorreloaderClient.CSS_RELOAD_SIGNAL.idmust be different from the previous event's.
License
ISC License © 2019 Shinnosuke Watanabe