> For the complete documentation index, see [llms.txt](https://router5.js.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://router5.js.org/advanced/plugins.md).

# Plugins

router5 is extensible with the use of plugins. Plugins can decorate a route instance and do things on specific router and transition events.

## Plugin requirements

A plugin is a function taking a router instance and returning an object with a name and at least one of the following methods:

* `onStart()`: invoked when `router.start()` is called
* `onStop()`: invoked when `router.stop()` is called
* `onTransitionStart(toState, fromState)`
* `onTransitionCancel(toState, fromState)`
* `onTransitionError(toState, fromState, err)`
* `onTransitionSuccess(toState, fromState, opts)` (options contains `replace` and `reload` boolean flags)
* `teardown()`: a function called when removing the plugin

## Registering a plugin

```javascript
function myPlugin(router, dependencies) {
    return {
        onTransitionSuccess: (toState, fromState) => {
            console.log(
                'Yippee, navigation to ' + toState.name + ' was successful!'
            )
        }
    }
}

const router = createRouter()

router.usePlugin(myPlugin)
```

## Plugin examples

* [Browser plugin](https://github.com/router5/router5/blob/master/packages/router5-plugin-browser/modules/index.ts)
* [Persistent params plugin](https://github.com/router5/router5/blob/master/packages/router5-plugin-persistent-params/modules/index.ts)
* [Logger](https://github.com/router5/router5/blob/master/packages/router5-plugin-logger/modules/index.ts)

Router5 includes a logging plugin that you can use to help development

```javascript
import createRouter, { loggerPlugin } from 'router5'

const router = createRouter()

const teardownPlgin = router.usePlugin(loggerPlugin)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://router5.js.org/advanced/plugins.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
