仅使用 1 KiB 的 JS 将动态内容加载到静态站点中
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.
1.45 KiB gzipped.Pill development started with the tweet by Andrey Sitnik @ai.
How pill works. It:
fetch.Initialize in one line:
pill('#content') // Yep, that's it.Include script from unpkg.com:
⚠️ Remember about security! Add subresource integrity (SRI) checksum from checksum.txt.
Install via npm:
npm i pill…html
## API
### `pill()`
```text
(selector:string, options:PillOptions) -> voidInitialize pill. Start listening for navigation attempts and history state changes. Puts loaded
content into selector element.
You can handle Pill's events by binding handlers on document element:
document.addEventListener('pill:loading', (e) => {
e.detail.page; // Current page
})pill:error Event{
detail: {
url: URL
element: HTMLElement
error: Error
}
}Is emitted when the new page loading has been started. This event wouldn't be emitted if page is cached.
Could be replaced with PillOptions.onLoading() hook.
pill:loading Event{
detail: {
url: URL
element: HTMLElement
}
}Is emitted when the new page loading has been started. This event wouldn't be emitted if page is cached.
Could be replaced with PillOptions.onLoading() hook.
pill:mounting Event{
detail: {
page: Page
url: URL
element: HTMLElement
}
}Is emitted when new page content is about to be added into the DOM.
Could be replaced with PillOptions.onMounting() hook.
pill:ready Event{
detail: {
page: Page
url: URL
element: HTMLElement
}
}Is emitted when the requested page is mounted into DOM and no futher work would be done.
Could be replaced with PillOptions.onReady() hook.
pill:unmounting Event{
detail: {
page: Page
url: URL
element: HTMLElement
}
}Is emitted when new page content is about to be removed from the DOM.
Could be replaced with PillOptions.onMounting() hook.
PillOptions.onError()(error) -> voidHandle page loading exception. By default is console.error.
PillOptions.onLoading()(page:Page) -> voidHandle loading start.
Could be replaced with pill:loading Event listener.
PillOptions.onMounting()(page:Page, url:URL, element:HTMLElement) -> voidFires everytime new content is about to be loaded to the DOM.
Could be replaced with pill:mounting Event listener.
PillOptions.onReady()(page:Page) -> voidHandle loading finish.
Could be replaced with pill:ready Event listener.
PillOptions.onUnmounting()(page:Page, url:URL, element:HTMLElement) -> voidFires everytime content is about to be removed from the DOM.
Could be replaced with pill:unmounting Event listener.
PillOptions.fromError()(error:Error) -> {title, content}Use it to display notification when something went wrong.
If an error was thrown while handling request. You still able
to render content using method fromError
PillOptions.getKeyFromUrl()(url:URL) -> StringGet cache key from URL. It's useful when URL contains query params which are unknown to server and could not affect response. By default any new pathname and search string combination will cause new request.
PillOptions.shouldReload()(page:Page) -> BooleanDetermine wether previously loaded page should be loaded from server.
PillOptions.shouldServe()(url:URL, target:HTMLElement) -> BooleanDeveloper-defined logic to determine whether the URL could be served by Pill.
If you return false then the link will be served by browser.
MIT © Rumkin
暂无开放 Issues,或尚未同步最近议题。