{"id":13536184,"url":"https://github.com/richardanaya/voir","last_synced_at":"2026-03-05T20:20:33.884Z","repository":{"id":57393724,"uuid":"73018381","full_name":"richardanaya/voir","owner":"richardanaya","description":"A simple router for lit-html","archived":false,"fork":false,"pushed_at":"2020-01-29T05:08:01.000Z","size":44,"stargazers_count":34,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-09T17:14:53.833Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/richardanaya.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-11-06T20:55:53.000Z","updated_at":"2022-02-12T07:59:32.000Z","dependencies_parsed_at":"2022-09-26T16:51:14.841Z","dependency_job_id":null,"html_url":"https://github.com/richardanaya/voir","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/richardanaya/voir","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/richardanaya%2Fvoir","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/richardanaya%2Fvoir/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/richardanaya%2Fvoir/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/richardanaya%2Fvoir/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/richardanaya","download_url":"https://codeload.github.com/richardanaya/voir/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/richardanaya%2Fvoir/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267988309,"owners_count":24176990,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-31T02:00:08.723Z","response_time":66,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T09:00:35.580Z","updated_at":"2026-03-05T20:20:28.859Z","avatar_url":"https://github.com/richardanaya.png","language":"JavaScript","funding_links":[],"categories":["实用库"],"sub_categories":[],"readme":"\n\u003cp align=\"center\"\u003e\u003cimg width=150 src=\"https://richardanaya.github.io/voir/voir.png\"\u003e\u003c/p\u003e\n\n# Voir\n\nVoir is a minimalistic routing/rendering system for single page applications. I have observed there are three basic operations that occur during the page lifecycle.\n\n* initial loading on first visit of a route\n* setting of page state on navigation to a route\n* rendering and rerendering of a current route on interations\n\nThis library makes it easy to do this.\n\n```javascript\nclass MyPageRoute extends PageRoute {\n  constructor() {\n    super(\"/blog/(?\u003cpostId\u003e.*)\")\n  }\n  \n  async function onInit(){\n    // perform some operation on first load\n\n    // get parameters from route regex match\n    const pageId = this.match.groups.postId;\n  }\n  \n  async function onLoad(){\n    // perform some operation when navigated to\n  }\n  \n  async function onRender(){\n    // render from session state and view state\n  }\n }\n```\n\nNotice that the route paths are simply regex strings. You can take advantage of ES 2018 regex named groups for more expressive route matches.\n\n## Usage\n\nWe're going to create a simple counter application.  \n\nFirst let's import [`lit-html`](https://lit-html.polymer-project.org/) and voir as ES modules\n\n```javascript\nimport {html, render} from 'https://unpkg.com/lit-html?module';\nimport {PageRoute} from 'https://cdn.jsdelivr.net/gh/richardanaya/voir@latest/voir.js';\n```\n\nLet's start by creating the session state for our app.\n\n```javascript\nvar session = { counter: 0 };\n```\n\nNow lets think about its lifecycle a bit\n\n```javascript\nclass CounterPageRoute extends PageRoute {\n  constructor() {\n    // all pages route to counter\n    super(\"/*\")\n  }\n\n  async onRender() {\n    // use lit to render to content holder\n    render(\n      html`\n        \u003cdiv\u003e\n          ${session.counter}\u003cbutton @click=${this.onAdd.bind(this)}\u003e+\u003c/button\u003e\n        \u003c/div\u003e\n      `,\n      document.body\n    );\n  }\n  \n  function onAdd() {\n    // modify state\n    session.counter += 1;\n    // rerender current page\n    this.renderCurrentPage();\n  }\n}\n```\n\nFinally we register the page routes in the order we'd like them evaluated\n\n```javascript\nregister([\n  CounterPageRoute\n  // other routes would go here\n])\n```\n\nSee this demo at: https://richardanaya.github.io/voir/demo.html\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frichardanaya%2Fvoir","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frichardanaya%2Fvoir","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frichardanaya%2Fvoir/lists"}