{"id":15097968,"url":"https://github.com/ionic-team/stencil-router-v2","last_synced_at":"2025-10-08T03:31:01.257Z","repository":{"id":40363488,"uuid":"262126192","full_name":"ionic-team/stencil-router-v2","owner":"ionic-team","description":null,"archived":true,"fork":false,"pushed_at":"2023-04-18T12:56:11.000Z","size":705,"stargazers_count":44,"open_issues_count":25,"forks_count":13,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-01-18T21:58:42.605Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ionic-team.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-07T18:18:03.000Z","updated_at":"2024-05-20T11:10:47.000Z","dependencies_parsed_at":"2024-06-18T15:39:06.374Z","dependency_job_id":null,"html_url":"https://github.com/ionic-team/stencil-router-v2","commit_stats":{"total_commits":27,"total_committers":6,"mean_commits":4.5,"dds":"0.33333333333333337","last_synced_commit":"b7a2da232c0884b627b7353a51646ced3f372401"},"previous_names":["manucorporat/stencil-router-v2"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ionic-team%2Fstencil-router-v2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ionic-team%2Fstencil-router-v2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ionic-team%2Fstencil-router-v2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ionic-team%2Fstencil-router-v2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ionic-team","download_url":"https://codeload.github.com/ionic-team/stencil-router-v2/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235678929,"owners_count":19028301,"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","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-09-25T16:41:40.664Z","updated_at":"2025-10-08T03:30:55.973Z","avatar_url":"https://github.com/ionic-team.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# stencil-router-v2\n\n⚠️ This project has been archived. ⚠️\n\nStencil Router V2 was an experimental router that did not reach v1.0 status, and should be considered unsupported.\n\nIndividuals and teams looking for a Stencil-based router solution should see the\n[Stencil Community Router project](https://github.com/stencil-community/stencil-router).\n\nThe project can continue to be downloaded in its current state from the NPM registry, and may\nbe forked by individuals wishing to build directly off of it.\n\nThe documentation below is kept for historical purposes only.\n\n---\nStencil Router V2 is an experimental new router for stencil that focus in:\n\n- **Lightweight** (600bytes)\n- **Treeshakable** (not used features are not included in the final build)\n- **Simple**, provide the bare mininum but it make it extendable with hooks.\n- **No DOM**: Router is not render any extra DOM element, to keep styling simple.\n- **Fast**: As fast and lightweight as writing your own router with if statements.\n\n## How does it work?\n\nThis router backs up the `document.location` in a `@stencil/store`, this way we can respond to changes in document.location is a much simpler, way, not more subscribes, no more event listeners events to connect and disconnect.\n\nFunctional Components are the used to collect the list of routes, finally the `Switch` renders only the selected route.\n\n\n## Install\n\n```bash\nnpm install stencil-router-v2 --save-dev\n```\n\n## Examples\n\n```tsx\nimport { createRouter, Route } from 'stencil-router-v2';\n\nconst Router = createRouter();\n\n@Component({\n  tag: 'app-root',\n})\nexport class AppRoot {\n\n  render() {\n    return (\n      \u003cHost\u003e\n        \u003cRouter.Switch\u003e\n\n          \u003cRoute path=\"/\"\u003e\n            \u003ch1\u003eWelcome\u003ch1\u003e\n            \u003cp\u003eWelcome to the new stencil-router demo\u003c/p\u003e\n          \u003c/Route\u003e\n\n          \u003cRoute path={/^\\/account/}\u003e\n            \u003capp-account\u003e\u003c/app-account\u003e\n          \u003c/Route\u003e\n\n        \u003c/Router.Switch\u003e\n      \u003c/Host\u003e\n    );\n  }\n}\n```\n\n### Redirects\n```tsx\n\u003cHost\u003e\n  \u003cRouter.Switch\u003e\n\n    \u003cRoute path=\"/\" to=\"/main\"/\u003e\n    \u003cRoute path={/^account/} to=\"/error\"/\u003e\n\n  \u003c/Router.Switch\u003e\n\u003c/Host\u003e\n```\n\n### Params\n\nRoute can take an optional `render` property that will pass down the params. This method should be used instead of JSX children.\n\nRegex or functional matches have the chance to generate an object of params when the URL matches.\n\n\n```tsx\nimport { createRouter, Route, match } from 'stencil-router-v2';\n\nconst Router = createRouter();\n\n\u003cHost\u003e\n  \u003cRouter.Switch\u003e\n\n    \u003cRoute\n      path={/^acc(ou)nt/}\n      render={(params) =\u003e (\n        \u003cp\u003e{params[1]}\u003c/p\u003e\n      )}\n    /\u003e\n\n    \u003cRoute\n      path={match('/blog/:page')}\n      render={({page}) =\u003e \u003cblog-post page={page}\u003e}\n    /\u003e\n\n    \u003cRoute\n      path={(url) =\u003e {\n        if (url.includes('hello')) {\n          return {user: 'hello'}\n        }\n        return undefined;\n      }}\n      render={({user}) =\u003e (\n        \u003ch1\u003eUser: {user}\u003c/h1\u003e\n      )}\n    /\u003e\n\n  \u003c/Router.Switch\u003e\n\u003c/Host\u003e\n```\n\n### Links\n\nThe `href()` function will inject all the handles to an native `anchor`, without extra DOM.\n\n```tsx\nimport { createRouter, Route, href } from 'stencil-router-v2';\n\nconst Router = createRouter();\n\n\u003cHost\u003e\n  \u003cRouter.Switch\u003e\n\n    \u003cRoute path=\"/main\"\u003e\n      \u003ca {...href('/main')} class=\"my-link\"\u003eGo to blog\u003c/a\u003e\n    \u003c/Route\u003e\n\n    \u003cRoute path=\"/blog\"\u003e\n      \u003ca {...href('/main')}\u003eGo to main\u003c/a\u003e\n    \u003c/Route\u003e\n\n  \u003c/Router.Switch\u003e\n\u003c/Host\u003e\n```\n\n\n### Dynamic routes (guards)\n\n```tsx\n@Component({\n  tag: 'app-root',\n})\nexport class AppRoot {\n\n  @State() logged = false;\n  render() {\n    return (\n      \u003cHost\u003e\n        \u003cRouter.Switch\u003e\n\n          {this.logged \u0026\u0026 (\n            \u003cRoute path=\"/account\"\u003e\n              \u003capp-account\u003e\u003c/app-account\u003e\n            \u003c/Route\u003e\n          )}\n\n          {!this.logged \u0026\u0026 (\n            \u003cRoute path=\"/account\" to=\"/error\"/\u003e\n          )\n\n        \u003c/Router.Switch\u003e\n      \u003c/Host\u003e\n    );\n  }\n}\n```\n\n### Subscriptions to route changes\n\nBecause the router uses `@stencil/store` its trivial to subscribe to changes in the locations, activeRoute, or even the list of routes.\n\n```tsx\nimport { createRouter, Route } from 'stencil-router-v2';\n\nconst Router = createRouter();\n\n@Component({\n  tag: 'app-root',\n})\nexport class AppRoot {\n  componentWillLoad() {\n    Router.onChange('url', (newValue: InternalRouterState['url'], _oldValue: InternalRouterState['url']) =\u003e {\n      // Access fields such as pathname, search, etc. from newValue\n\n      // This would be a good place to send a Google Analytics event, for example\n    });\n  }\n\n  render() {\n    const activePath = Router.state.activeRoute?.path;\n\n    return (\n      \u003cHost\u003e\n        \u003caside\u003e\n          \u003ca class={{'active': activePath === '/main'}}\u003eMain\u003c/a\u003e\n          \u003ca class={{'active': activePath === '/account'}}\u003eAccount\u003c/a\u003e\n        \u003c/aside\u003e\n\n        \u003cRouter.Switch\u003e\n\n          \u003cRoute path=\"/main\"\u003e\n            \u003ch1\u003eWelcome\u003ch1\u003e\n            \u003cp\u003eWelcome to the new stencil-router demo\u003c/p\u003e\n          \u003c/Route\u003e\n\n          \u003cRoute path='/account'\u003e\n            \u003capp-account\u003e\u003c/app-account\u003e\n          \u003c/Route\u003e\n\n        \u003c/Router.Switch\u003e\n      \u003c/Host\u003e\n    );\n  }\n}\n```\n\nThe routes state includes:\n\n```tsx\n  url: URL;\n  activeRoute?: RouteEntry;\n  urlParams: { [key: string]: string };\n  routes: RouteEntry[];\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fionic-team%2Fstencil-router-v2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fionic-team%2Fstencil-router-v2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fionic-team%2Fstencil-router-v2/lists"}