Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rumkin/pill

Add dynamic content loading to static sites with only 1 KiB of JS
https://github.com/rumkin/pill

dynamic-loading static-sites

Last synced: 16 days ago
JSON representation

Add dynamic content loading to static sites with only 1 KiB of JS

Awesome Lists containing this project

README

        


Pill logo



badge: npm version

badge: size 1.45 KiB
badge: deps 0

Pill adds dynamic content loading to static sites and makes content loading
smooth for users. It's pretty small only _1.45 KiB_ minified and gzipped. It fits perfectly
for static sites with WebComponents.

* 🐥 **Tiny**: `1.45 KiB` gzipped.
* 🥤 **Easy-to-use**: single function call.
* 🎮 **Handful**: hooks and callbacks could modify the default behavior.

> Pill development started with the [tweet](https://twitter.com/sitnikcode/status/1109626507331338240)
by Andrey Sitnik [@ai](https://github.com/ai).

How pill works. It:

1. Intercepts navigation attempts: links clicks and history navigation.
2. Loads requested url using `fetch`.
3. Grabs content from received HTML.
4. Replaces current page content.

Initialize in one line:
```javascript
pill('#content') // Yep, that's it.
```

## Table of Contents

* [Install](#install)
* [Usage](#usage)
* [Corner cases](#corner-cases)
* [API](#api)
* [License](#license)

## Install

* Include script from unpkg.com:
```html

```

> ⚠️ Remember about security! Add [subresource integrity](https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity) (SRI) checksum
> from [checksum.txt](https://unpkg.com/pill@1/dist/checksum.txt).

* Install via npm:

```
npm i pill
```

## Usage

1. Inject pill's `` into page.
2. Create content root element and give it id.
3. Create loading indicator element.
4. Initialize pill:
```javascript
// Get loading indicator element
const indicator = document.querySelector('#indicator')
// Assign Pill to specified selector
pill('#content', {
onLoading() {
// Show loading indicator
indicator.style.display = 'initial'
},
onReady() {
// Hide loading indicator
indicator.style.display = 'none'
}
})
```

### Complete example

```html
<html>
<head>
<title>Home</title>
<script src="https://unpkg.com/pill@1/dist/pill.min.js">

/* global styles */
#indicator {
position: fixed;
top: 0;
right: 0;
display: none;
}



Loading...


/* page styles */




const indicator = document.querySelector('#indicator')

pill('#content', {
onLoading() {
// Show loading indicator
indicator.style.display = 'initial'
},
onReady() {
// Hide loading indicator
indicator.style.display = 'none'
}
})






pill('#content', {
onMounting(page, url, element) {
// Init page, for example bind event listeners, start timers, etc.
App.initPage(url, element)
},
onUnmounting(page, url, element) {
// Uninitialise page, for example remove event listeners, stop timers, etc.
App.destroyPage(url, element)
},
})
App.initPage(new URL(window.location), document.querySelector('#content'))