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

reloader-injector

v0.2.0

Published

Insert a <script> to reload the current page or its styles into an HTML

Downloads

1

Readme

reloader-injector

npm version Build Status Build status codecov

A Node.js module to insert a <script> into an HTML, which helps reloading the document or its styles automatically

In most cases there is no need to use this module directly. Developers would rather use a higher-level wrapper content-reloader.

Installation

Use npm.

npm install --save-dev reloader-injector

API

const ReloaderInjector = require('reloader-injector');

class ReloaderInjector([option])

option: Object

It creates an reloaderInjector instance.

option.url

Type: string URL
Default: '/sse'

A URL at which the client script will open a connection to the server.

This module expects the server to serve no contents other than reloader-injector-related ones under this URL. If the server will respond to requests whose URLs begin with /sse, change the value of this option.

reloaderInjector.injectScriptTag(response)

response: http.ServerResponse

If the media type of the response is text/html, it inserts a <script> tag into the response body as the first child of <body>. The src attribute of the <script> points to the first key of reloaderInjector.clients.

reloaderInjector.injectLegacyScriptTag(response)

response: http.ServerResponse

If the media type of the response is text/html, it inserts a <script> tag into the response body as the first child of <body>. The src attribute of the <script> points to the second key of reloaderInjector.clients.

reloaderInjector.clients

Type: Map<string: Buffer>

This Map contains two Buffers: the first value is a JavaScript code generated by the main function of reloader-client, and the second one is a JavaScript code generated by the .legacy() method of reloader-client.

reloaderInjector.path

Type: string

A pathname of the URL corresponding to the url option.

const reloaderInjector = new ReloaderInjector();

reloaderInjector.clients; /*=> Map {
  '/sse-95f0ec37f24542c2547b712a060e43af.js' => <Buffer 63 6f 6e 73 ...>,
  '/sse-eb33250679b5d5c862ed2286a2c95e75.js' => <Buffer 2f 2a 2a 20 ...>
} */

ReloaderInjector.DOCUMENT_RELOAD_SIGNAL, ReloaderInjector.CSS_RELOAD_SIGNAL

Type: string

The same values as reloader-client's.

Usage

1. Inject a <script> tag to an HTML Response using .injectScriptTag() (or .injectLegacyScriptTag() for Microsoft browsers).

const reloaderInjector = new ReloaderInjector();

const server = createServer(({url}, res) => {
  res.setHeader('content-type', 'text/html');
  reloaderInjector.injectScriptTag(res);
  res.end('<!doctype html><html lang="en"> ... some contents ... </html>');
});

2. Serve JavaScript of reloaderInjector.clients using its keys as a pathname of endpoints.

const server = createServer(({url}, res) => {
+   for (const [clientUrl, body] of reloaderInjector.clients) {
+     if (url === clientUrl) {
+       res.writeHead(200, {
+         'content-type': 'application/javascript',
+         'content-length': body.length
+       });
+
+       res.end(body);
+       return;
+     }
+   }

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

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

License

ISC LICENSE © 2019 Shinnosuke Watanabe