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

plob

v1.1.2

Published

SPA state manager

Downloads

4

Readme

plob

plob is a simple(ish) SPA (single-page application) state manager, which handles page loading & url changing for you.

Getting Started

Add the package via either NPM or a direct link in the head tag.

npm install plob
const plob = require('plob')

CDN (or similar for local hosting):

<script src="https://cdn.jsdelivr.net/npm/plob@latest/src/plob.min.js"></script>

Now, you can simply start plob with the following:

plob.start()

This can be run at any time, but will wait for the DOM content to load before actually starting if it hasn't loaded already.

Links

Any a tag with a path within the root path specifified in the options will automatically be overridden. Basically, you don't have to do anything.

If you're adding a link after plob has started, run plob._override() to override links again. Links that are already upgraded won't be double-upgraded.

Options

The plob.start(options) function takes an object of options as input to configure plob's behaviour.

option | default | description ------ | ------- | ----------- root | '/' | root URL of the SPA system containers | [document.body] | containers for page content to be rendered in logging | false | enables logging loadtimes | false | if logging is enabled, displays load times for pages unsupported_cb | - | function to be run if plob is unsupported trailing_slash | false | whether to go to /foo/bar/ instead of /foo/bar load_overlay | - | initial loading element to hide once first page is loaded

Adding Pages

Pages take the form of asynchronous functions which have a 'render element' passed into them. They should then use this 'render element' (which will be one of the containers specific in options) as their root element in which the page is generated.

Pages can be added like so:

function my_page(render_elm, plob_options) {
    const h2 = document.createElement('h2')
    h2.innerText = 'Hello, world!'

    const img = document.createElement('img')
    img.src = '/example/grumpy_cat.jpg'

    render_elm.appendChild(h2)
    render_elm.appendChild(img)
}

plob.pages.push({regex: '.*', loader: my_page})

The page object can also have a name and an id parameter, which serve no functional value within plob besides logging. A priority number (which defaults to 1) can also be included, which will be used to sort the importance of pages when multiple regexs match for the current page. This is useful for pages such as 404.

Page Object

key | type | example | description --- | ---- | ------- | ----------- id | *? | 'abcd123' | page ID. Not required for any plob functionality name | *? | 'abcd123' | page name. Not required for any plob functionality regex | RegExp | '/mypage.*' | regular expression to match the URLs this page applies to loader | Function | - | function to run in order to load the page into a given element priority | Number | 10 | relative priority compared to other pages container | Node or Selector | '#special_container' | a specific container to use for this page, will never be overridden by another page or cleared at all

Note: it is highly recommended to use an Asynchronous Function for the loader if the page relies on asynchrous code, otherwise it may appear before it has fully loaded.

404 Page

A 404 page can easily be added with a wildcard regex and a negative priority, like so:

plob.pages.push({regex: '.*', loader: el => el.innerText = 'page not found', priority: -1})

See example/index.html for a working example of this.