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

@vanruesc/stay

v0.1.14

Published

Stay is a small but effective library for the creation of dynamic xhr-driven web applications.

Downloads

1

Readme

Stay

Build status Windows build status GitHub version npm version Dependencies

Stay is a slim and effective module for the creation of dynamic xhr-driven web applications. It expects the server to be able to send a page's content as a simple JSON string in which the key names correspond with the IDs of the target DOM containers.

Installation

Download the minified library and include it in your project:

<script src="/js/stay.min.js"></script>

You can also install this module from npm.

$ npm install @vanruesc/stay

Usage

The client part

Creating an instance of Stay usually suffices.

import Stay from "@vanruesc/stay";

var stay;

try {

    stay = new Stay();

} catch(error) {

    // XHR not supported.
    console.warn(error);

}

You may also configure Stay's behaviour and take full control of the navigation flow.

import Stay from "@vanruesc/stay";

var stay;

try {

    stay = new Stay({

	    // Logs to console by default
	    stderr: "myDomElement",

    	// Default is "/json"
    	infix: "/asyncRequests",

	    // Default is 60000ms, 0 means no timeout
    	timeoutPost: 0,

    	// Default is 5000ms
    	timeoutGet: 0,

	    // Default is true
    	autoUpdate: false

    });

    stay.addEventListener("navigate", function() {

    	console.log("Page navigation has started.");
        startMyFancyLoadingAnimation();

    });

    stay.addEventListener("receive", function(event) {

        maybeDoSomethingWithThe(event.response);

    	/* If autoUpdate is disabled, the programmer has to decide 
	     * when to update the page content. The update() method MUST 
    	 * be called at some point to unlock the system!
    	 *
    	 * This event will always be dispatched. See the docs for 
    	 * payload information if you intend to process the server 
    	 * response.
    	 *
    	 * Please note: do not make your users wait artificially!
	     */

    	stay.update(event.response);

    });

    stay.addEventListener("load", function() {

        stopMyFancyLoadingAnimation();
    	console.log("The requested page has been loaded.");

    });

} catch(error) {

    console.warn(error);

}

The server part

Every GET and POST endpoint needs to be available as a condensed JSON resource. This includes dynamically generated pages and error pages. Serving a JSON version of each resource should be seen as an additional feature and nothing more.

Stay is rather tolerant when it comes to different URI patterns, but a well-structured URI configuration is the foundation of a good web application. Take a look at some recommendations for good URI design if you haven't already! These guidelines are a good starting point.

The following example shows what's going on behind the scenes of Stay:

<a href="/foo/bar">Hyperlink</a>

This link will internally be converted to:

"http[s]://www.your-domain.com[:port]/json/foo/bar"

The modified URI won't be seen by the user and the infix can be freely chosen by you. If we assume that the original URI points to a simple HTML page which looks like this:

<html>
	<head>
		<meta charset="utf-8">
		<title>Foo</title>
	</head>
	<body>
		<div id="main">Bar!</div>
	</body>
</html>

then the JSON equivalent must look like this:

{
    "meta": {
        "title": "Foo"
    },
    "main": "Bar!"
}

Stay would look at this JSON response and then try to replace the children of #main with the received content which is a simple text node in this case, but could be any HTML content. The meta object's title property is used to adjust the current page's title. Furthermore, the browser history will be managed for you to support the back and forward browser controls.

Although the above example HTML is minimal, it highlights the main aspects of asynchronous web applications:

  • More efficient bandwidth usage
  • Better loading performance
  • More control over the navigation process
  • Consolidation of a main page structure
  • Still highly customisable!

Media and External Resources

Stay detects external resources and doesn't touch them. The user will experience a synchronous navigation. Hyperlinks to internal resources such as images or executable files are problematic because they can't be identified as such by their URI alone. You may, however, define an arbitrary number of regular expressions to exclude specific URIs.

stay.exclusions.push(/\/nonJSON\//);

When linking a resource that can't be represented in JSON format, you should consider moving it to a dedicated file server. Since Stay ignores external resources by default, the file would just open as expected.

Documentation

API

Contributing

Maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.

License

Apache 2.0