百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
P

pill

> 编程语言
开源

仅使用 1 KiB 的 JS 将动态内容加载到静态站点中

380 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

仅使用 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.

  • 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 by Andrey Sitnik @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
  • Usage
  • Corner cases
  • API
  • License

Install

  • Include script from unpkg.com:

    xml
    
    

    ⚠️ Remember about security! Add subresource integrity (SRI) checksum from checksum.txt.

  • Install via npm:

    bash
    npm i pill

Usage

  1. Inject pill's `
…

html


## API

### `pill()`
```text
(selector:string, options:PillOptions) -> void

Initialize pill. Start listening for navigation attempts and history state changes. Puts loaded content into selector element.

Events

You can handle Pill's events by binding handlers on document element:

javascript
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.

Hooks

PillOptions.onError()

(error) -> void

Handle page loading exception. By default is console.error.

PillOptions.onLoading()

(page:Page) -> void

Handle loading start.

Could be replaced with pill:loading Event listener.

PillOptions.onMounting()

(page:Page, url:URL, element:HTMLElement) -> void

Fires everytime new content is about to be loaded to the DOM.

Could be replaced with pill:mounting Event listener.

PillOptions.onReady()

(page:Page) -> void

Handle loading finish.

Could be replaced with pill:ready Event listener.

PillOptions.onUnmounting()

(page:Page, url:URL, element:HTMLElement) -> void

Fires everytime content is about to be removed from the DOM.

Could be replaced with pill:unmounting Event listener.

Other options

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) -> String

Get 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) -> Boolean

Determine wether previously loaded page should be loaded from server.

PillOptions.shouldServe()

(url:URL, target:HTMLElement) -> Boolean

Developer-defined logic to determine whether the URL could be served by Pill. If you return false then the link will be served by browser.

License

MIT © Rumkin

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScriptdynamic-loadingstatic-sites

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言