@aglyn/plugins-redirects
The Redirects plugin for Aglyn: URL redirect rules for a site, the console page that manages them, and the server hook that answers a request with a redirect before the site resolves a route. Install it if you run the Aglyn console and tenant runtime and want redirect rules; it is a first-party plugin, not a standalone library.
Published to npm from the Aglyn monorepo. Versions follow semantic versioning.
Install
npm install @aglyn/plugins-redirects
Peer dependencies: react, @mui/material, firebase, and firebase-admin for the server entry.
What's in it
On a published site. Nothing is drawn. Redirects are enforced on the server: registerRedirectsApi (the tenantApi registrar, exported from @aglyn/plugins-redirects/server) registers a site redirect resolver with @aglyn/aglyn/server. For each request the resolver is given the host and path and returns a destination and status code, or nothing.
Console (registerRedirectsConsole, the console registrar): a Redirects nav item at /redirects, gated by the redirects entitlement. The page is code-split and loads when opened.
Console server declarations (registerRedirectsConsoleServerDeclarations, the consoleServerDeclarations registrar): the server half of the redirects import and export, and the redirect writer on the resource-drafts seam with the redirect build operation an Aglyn AI build uses to create, update or remove a site's redirect rule. The writer makes the redirects page's checks (publishing role, the redirects feature, redirectsPerHost on a create, lockdown, validation, duplicates and loops), keeps a published page's path unless the item says it was asked for, and stamps an off-site destination with the member. A rule has no draft state, so it is live once written. The console's redirect record route opens the Redirects page.
The rule model, exported from .:
HostRedirect: a rule'ssource,destination,statusCode(REDIRECT_STATUS_CODES),kind(REDIRECT_KINDS:exact,prefix,regex),priorityandenabled.matchRedirect(rules, path): evaluates rules in priority order and returns aRedirectMatchornull. Disabled, self-targeting and invalid rules never fire, and an external destination is served only when the rule records the publisher who approved it.validateRedirectRule,compileRedirectRegex,normalizeRedirectSource,normalizeRedirectDestination,isExternalRedirectDestination,isSelfRedirect,REDIRECT_DEFAULT_PRIORITY.BUNDLE_ID('redirects').
Usage
The plugin is loaded through Aglyn's plugin manager: the generated loader manifests import the package and call the registrars named in plugins.config.json. An app that wires plugins by hand calls them once at startup:
// console, client side
import { registerRedirectsConsole } from '@aglyn/plugins-redirects'
registerRedirectsConsole()
// tenant runtime, server side
import { registerRedirectsApi } from '@aglyn/plugins-redirects/server'
registerRedirectsApi()
The matcher is pure and can be used directly:
import { matchRedirect, type HostRedirect } from '@aglyn/plugins-redirects'
const rules: HostRedirect[] = [
{ source: '/old', destination: '/new', statusCode: 301 },
]
const match = matchRedirect(rules, '/old')
// match?.destination === '/new'
How it fits
A plugin package (scope:plugin). It depends on the core (@aglyn/aglyn), the tenant packages (@aglyn/tenant-data-admin, @aglyn/tenant-feature-instance) and generic @aglyn/shared-* packages. It imports no other plugin, and the core never imports it: the tenant runtime asks whichever resolver is registered, and the console reaches the page through the loader manifest.
License
Apache-2.0. Source: https://github.com/aglyn/aglyn/tree/main/libs/plugins/redirects