Plugins
Plugins add features to ctx and hook into each request. Turn them on in +config.ts:
// pb_pages/+config.ts
module.exports = {
plugins: ['auth', 'realtime', 'datastar'],
}
Each entry is one of:
-
A name in quotes. It's loaded with
require, which looks in this order:- a
_private/folder inpb_pages('datastar'finds_private/datastar.js) pb_pages/node_modules, for a package from yourpackage.json- the built-in plugins,
'auth'and'realtime'
A path such as
'./plugins/my-plugin.ts'works too. - a
-
A plugin factory, usually
require('./_private/my-plugin.ts'). Writing therequireyourself with a path skips the lookup above and loads exactly that file, which is how you pick one copy over another:'datastar'would find_private/datastar.jsfirst, whilerequire('./node_modules/datastar')always loads the package.
Datastar isn't built in. It's an add-on file you copy into pb_pages/_private/ and list by name.
ETS templates and Markdown are always on; they aren't plugins.
Built-in plugins
| Name | Adds |
|---|---|
auth |
Cookie sessions on PocketBase auth: sets ctx.auth from the pb_auth cookie and adds ctx.session.* sign-in methods. |
realtime |
ctx.realtime.send() to PocketBase realtime subscribers. |
Custom plugins
A custom plugin is a module that exports a factory returning hooks. List it by name, or require() it yourself, which you
need for options ({ factory, ...options }):
module.exports = {
plugins: ['auth', 'my-plugin', { factory: require('./_private/other-plugin.ts'), color: 'blue' }],
}
See Plugin Authoring.
| Add-on | Adds |
|---|---|
| Datastar | ctx.datastar.* helpers for Datastar. Copy addons/datastar.js into pb_pages/_private/; the datastar starter kit already has it. |
Order
Plugins' hooks run in the order they're listed, and onRequest hooks run before any middleware.
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.