@klnap/payload-redirect 0.4.0 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +261 -312
- package/dist/core/redirects/auto/create-redirect.js +2 -4
- package/dist/core/redirects/auto/create-redirect.js.map +1 -1
- package/dist/core/redirects/auto/persist-auto-rule.js +3 -0
- package/dist/core/redirects/auto/persist-auto-rule.js.map +1 -1
- package/dist/core/redirects/auto/publication/get-publication-transition.d.ts +5 -0
- package/dist/core/redirects/auto/publication/get-publication-transition.js +20 -8
- package/dist/core/redirects/auto/publication/get-publication-transition.js.map +1 -1
- package/dist/core/redirects/auto/publication/global-transition.js +9 -7
- package/dist/core/redirects/auto/publication/global-transition.js.map +1 -1
- package/dist/core/redirects/auto/publication/locales.d.ts +0 -1
- package/dist/core/redirects/auto/publication/locales.js +0 -28
- package/dist/core/redirects/auto/publication/locales.js.map +1 -1
- package/dist/core/redirects/auto/publication/versions/version-rows.d.ts +2 -0
- package/dist/core/redirects/auto/publication/versions/version-rows.js +2 -0
- package/dist/core/redirects/auto/publication/versions/version-rows.js.map +1 -1
- package/dist/core/redirects/collection/collection.js +5 -0
- package/dist/core/redirects/collection/collection.js.map +1 -1
- package/dist/core/redirects/collection/duplicate-guard.js +0 -4
- package/dist/core/redirects/collection/duplicate-guard.js.map +1 -1
- package/dist/core/redirects/collection/graph-lock.d.ts +6 -1
- package/dist/core/redirects/collection/graph-lock.js +11 -0
- package/dist/core/redirects/collection/graph-lock.js.map +1 -1
- package/docs/FAILURE_SEMANTICS.md +5 -4
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -1,40 +1,51 @@
|
|
|
1
1
|
# `@klnap/payload-redirect`
|
|
2
2
|
|
|
3
|
-
Redirect management for Payload CMS.
|
|
3
|
+
Redirect management for Payload CMS.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Quick start
|
|
6
|
+
|
|
7
|
+
Register the plugin once in `payload.config.ts`:
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { redirects } from "@klnap/payload-redirect";
|
|
11
|
+
import { buildConfig } from "payload";
|
|
12
|
+
|
|
13
|
+
export default buildConfig({
|
|
14
|
+
// ...your database, editor, and collections
|
|
15
|
+
plugins: [
|
|
16
|
+
redirects({
|
|
17
|
+
collections: {
|
|
18
|
+
articles: { path: "/articles/[slug]" },
|
|
19
|
+
},
|
|
20
|
+
}),
|
|
21
|
+
],
|
|
22
|
+
});
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Publish an article, then change its slug and publish again:
|
|
6
26
|
|
|
7
27
|
```text
|
|
8
|
-
/articles/
|
|
9
|
-
→ /articles/
|
|
28
|
+
/articles/hello-world
|
|
29
|
+
→ /articles/introduction-to-payload (301)
|
|
10
30
|
```
|
|
11
31
|
|
|
32
|
+
The plugin stores the rule. Your application looks it up and sends the response; see [Application integration](#application-integration). Draft edits alone create no rule.
|
|
33
|
+
|
|
34
|
+
## Why?
|
|
35
|
+
|
|
36
|
+
Published URLs change when editors update content. A changed slug breaks links from search results, social posts, and other sites. Moved pages and campaign links need redirects too, and maintaining each one by hand is easy to forget.
|
|
37
|
+
|
|
38
|
+
This plugin records the change automatically when a published path moves. It also gives editors a place in the Payload Admin to manage redirects for moved pages and campaign links.
|
|
39
|
+
|
|
12
40
|
## Features
|
|
13
41
|
|
|
14
|
-
-
|
|
15
|
-
-
|
|
16
|
-
-
|
|
17
|
-
-
|
|
18
|
-
-
|
|
19
|
-
-
|
|
20
|
-
-
|
|
21
|
-
- **No runtime dependencies** beyond the Payload peer.
|
|
22
|
-
|
|
23
|
-
## Contents
|
|
24
|
-
|
|
25
|
-
- [Installation](#installation)
|
|
26
|
-
- [Complete example](#complete-example)
|
|
27
|
-
- [How it works](#how-it-works)
|
|
28
|
-
- [Configuration](#configuration)
|
|
29
|
-
- [Localization](#localization)
|
|
30
|
-
- [Globals](#globals)
|
|
31
|
-
- [Manual redirects](#manual-redirects)
|
|
32
|
-
- [Serving redirects](#serving-redirects)
|
|
33
|
-
- [Overrides and access](#overrides-and-access)
|
|
34
|
-
- [Reference](#reference)
|
|
35
|
-
- [Compatibility](#compatibility)
|
|
36
|
-
- [Limitations](#limitations)
|
|
37
|
-
- [Documentation](#documentation)
|
|
42
|
+
- Automatic `301` rules when a published path changes, including changes to other fields in the path, such as a category.
|
|
43
|
+
- Configuration next to the schema with `custom.redirect`, or centrally in `redirects()`.
|
|
44
|
+
- Collections and Globals with the same rules.
|
|
45
|
+
- Native Payload localization with one template per locale, or one shared template with locale prefixes.
|
|
46
|
+
- Manual `301` and `302` rules, enabled or disabled, with query strings and fragments.
|
|
47
|
+
- Duplicate sources and loops are rejected. Manual rules are never overwritten by automatic ones.
|
|
48
|
+
- No runtime dependencies beyond the Payload peer.
|
|
38
49
|
|
|
39
50
|
## Installation
|
|
40
51
|
|
|
@@ -46,34 +57,118 @@ bun add @klnap/payload-redirect
|
|
|
46
57
|
npm install @klnap/payload-redirect
|
|
47
58
|
```
|
|
48
59
|
|
|
49
|
-
`payload` is a required peer. `@payloadcms/db-postgres` is an optional peer
|
|
60
|
+
`payload` is a required peer dependency. `@payloadcms/db-postgres` is an optional peer dependency; install it when you use PostgreSQL. Node.js versions are listed in [Compatibility](#compatibility-and-documentation).
|
|
50
61
|
|
|
51
|
-
##
|
|
62
|
+
## Usage
|
|
52
63
|
|
|
53
|
-
|
|
64
|
+
### Automatic redirects
|
|
54
65
|
|
|
55
|
-
|
|
66
|
+
Add `custom.redirect` to any Collection with drafts enabled:
|
|
56
67
|
|
|
57
68
|
```ts
|
|
58
|
-
|
|
59
|
-
import {
|
|
60
|
-
import {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
69
|
+
// src/collections/Articles.ts
|
|
70
|
+
import type { CollectionConfig } from "payload";
|
|
71
|
+
import type { RedirectCollectionOptions } from "@klnap/payload-redirect";
|
|
72
|
+
|
|
73
|
+
export const Articles: CollectionConfig = {
|
|
74
|
+
slug: "articles",
|
|
75
|
+
versions: { drafts: true },
|
|
76
|
+
admin: { useAsTitle: "title" },
|
|
77
|
+
fields: [
|
|
78
|
+
{ name: "title", type: "text", required: true },
|
|
79
|
+
{ name: "slug", type: "text", required: true },
|
|
80
|
+
],
|
|
81
|
+
custom: {
|
|
82
|
+
redirect: {
|
|
83
|
+
path: "/articles/[slug]",
|
|
84
|
+
} satisfies RedirectCollectionOptions<"articles">,
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Register the plugin without options:
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
plugins: [redirects()],
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Central configuration does the same job. Use either pattern, not both. A central entry replaces the entity's `custom.redirect` for that entity.
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
redirects({
|
|
99
|
+
collections: {
|
|
100
|
+
articles: { path: "/articles/[slug]" },
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
A route is one of three strategies:
|
|
64
106
|
|
|
107
|
+
| Option | Use when |
|
|
108
|
+
| --------- | ------------------------------------------------------------------------------------- |
|
|
109
|
+
| `path` | The route is a template over top-level fields, such as `/articles/[slug]`. |
|
|
110
|
+
| `paths` | Each locale has its own template. See [Localization](#localization). |
|
|
111
|
+
| `getPath` | The route depends on logic a template cannot express. Return `null` to skip a locale. |
|
|
112
|
+
|
|
113
|
+
Prefer `path` and `paths`. Use `getPath` only when you need it:
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
custom: {
|
|
117
|
+
redirect: {
|
|
118
|
+
getPath: ({ doc }) => (doc.category === "interviews" ? `/interviews/${doc.slug}` : null),
|
|
119
|
+
} satisfies RedirectCollectionOptions<"articles">,
|
|
120
|
+
},
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Values in `getPath` are not encoded for you. Encode dynamic segments with `encodeURIComponent` when they can contain spaces or reserved characters.
|
|
124
|
+
|
|
125
|
+
A rule is created only when a publication changes the published path. Draft saves, unchanged republications, and unpublishing create nothing. Existing rules stay when a document is unpublished or deleted. A first publication creates nothing either, because there is no previous path.
|
|
126
|
+
|
|
127
|
+
Full-path tracking uses several fields. A category change then creates a redirect even when the slug stays the same:
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
path: "/articles/[category]/[slug]",
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Globals
|
|
134
|
+
|
|
135
|
+
Globals work the same way. Give the Global drafts and a `custom.redirect` entry. This About page is a familiar example:
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
// src/globals/About.ts
|
|
139
|
+
import type { GlobalConfig } from "payload";
|
|
140
|
+
import type { RedirectGlobalOptions } from "@klnap/payload-redirect";
|
|
141
|
+
|
|
142
|
+
export const About: GlobalConfig = {
|
|
143
|
+
slug: "about",
|
|
144
|
+
versions: { drafts: true },
|
|
145
|
+
fields: [{ name: "slug", type: "text", required: true }],
|
|
146
|
+
custom: {
|
|
147
|
+
redirect: {
|
|
148
|
+
path: "/[slug]",
|
|
149
|
+
} satisfies RedirectGlobalOptions<"about">,
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Publishing a new slug `about-us` creates this redirect:
|
|
155
|
+
|
|
156
|
+
```text
|
|
157
|
+
/about → /about-us (301)
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### Localization
|
|
161
|
+
|
|
162
|
+
The plugin uses Payload's native content localization. Configure locales in `buildConfig`, make the slug field localized, and give each locale its path in `paths`:
|
|
163
|
+
|
|
164
|
+
```ts
|
|
165
|
+
// payload.config.ts
|
|
65
166
|
export default buildConfig({
|
|
66
|
-
secret: process.env.PAYLOAD_SECRET ?? "",
|
|
67
|
-
editor: lexicalEditor(),
|
|
68
|
-
db: postgresAdapter({
|
|
69
|
-
pool: { connectionString: process.env.DATABASE_URI },
|
|
70
|
-
}),
|
|
71
167
|
localization: {
|
|
72
168
|
locales: ["en", "fr", "de"],
|
|
73
169
|
defaultLocale: "en",
|
|
74
170
|
},
|
|
75
|
-
|
|
76
|
-
globals: [About],
|
|
171
|
+
// ...
|
|
77
172
|
plugins: [
|
|
78
173
|
redirects({
|
|
79
174
|
localePrefixes: { en: "", fr: "/fr", de: "/de" },
|
|
@@ -82,28 +177,20 @@ export default buildConfig({
|
|
|
82
177
|
});
|
|
83
178
|
```
|
|
84
179
|
|
|
85
|
-
`redirects()` is called once. It adds the `redirects` collection, the publication hooks, and the locale prefixes.
|
|
86
|
-
|
|
87
|
-
### 2. `src/collections/Articles.ts`
|
|
88
|
-
|
|
89
180
|
```ts
|
|
90
|
-
|
|
91
|
-
import type { RedirectCollectionOptions } from "@klnap/payload-redirect";
|
|
92
|
-
|
|
181
|
+
// src/collections/Articles.ts
|
|
93
182
|
export const Articles: CollectionConfig = {
|
|
94
183
|
slug: "articles",
|
|
95
184
|
versions: { drafts: true },
|
|
96
|
-
admin: { useAsTitle: "title" },
|
|
97
185
|
fields: [
|
|
98
186
|
{ name: "title", type: "text", required: true, localized: true },
|
|
99
187
|
{ name: "slug", type: "text", required: true, localized: true },
|
|
100
|
-
{ name: "category", type: "text", required: true },
|
|
101
188
|
],
|
|
102
189
|
custom: {
|
|
103
190
|
redirect: {
|
|
104
191
|
paths: {
|
|
105
192
|
en: "/articles/[slug]",
|
|
106
|
-
fr: "/
|
|
193
|
+
fr: "/articles/[slug]",
|
|
107
194
|
de: "/artikel/[slug]",
|
|
108
195
|
},
|
|
109
196
|
} satisfies RedirectCollectionOptions<"articles">,
|
|
@@ -111,33 +198,73 @@ export const Articles: CollectionConfig = {
|
|
|
111
198
|
};
|
|
112
199
|
```
|
|
113
200
|
|
|
114
|
-
|
|
201
|
+
With these settings the same article has one path per locale:
|
|
115
202
|
|
|
116
|
-
|
|
203
|
+
| Locale | Slug | Public path |
|
|
204
|
+
| ------ | ----------------- | ---------------------------- |
|
|
205
|
+
| `en` | `getting-started` | `/articles/getting-started` |
|
|
206
|
+
| `fr` | `premiers-pas` | `/fr/articles/premiers-pas` |
|
|
207
|
+
| `de` | `erste-schritte` | `/de/artikel/erste-schritte` |
|
|
117
208
|
|
|
118
|
-
|
|
119
|
-
import type { GlobalConfig } from "payload";
|
|
120
|
-
import type { RedirectGlobalOptions } from "@klnap/payload-redirect";
|
|
209
|
+
Changing the French slug to `bien-demarrer` and publishing creates only the French redirect:
|
|
121
210
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
fields: [{ name: "slug", type: "text", required: true, localized: true }],
|
|
126
|
-
custom: {
|
|
127
|
-
redirect: {
|
|
128
|
-
getPath: ({ doc, locale }) => `${locale === "fr" ? "/fr" : ""}/${doc.slug}`,
|
|
129
|
-
} satisfies RedirectGlobalOptions<"about">,
|
|
130
|
-
},
|
|
131
|
-
};
|
|
211
|
+
```text
|
|
212
|
+
/fr/articles/premiers-pas
|
|
213
|
+
→ /fr/articles/bien-demarrer (301)
|
|
132
214
|
```
|
|
133
215
|
|
|
134
|
-
|
|
216
|
+
Rules for localized paths:
|
|
217
|
+
|
|
218
|
+
- `path` is one template for every locale. `paths` gives one template per locale and must list every locale in `localization.locales`.
|
|
219
|
+
- `localePrefixes` adds a prefix to `path` and `paths`. It must list every locale. Use `""` for the default locale.
|
|
220
|
+
- A locale without a slug is skipped. Generation never uses another locale's slug, even when Payload's fallback is enabled for reads.
|
|
221
|
+
- Redirect rules have no locale. Different locales need different source paths.
|
|
222
|
+
- Payload's experimental localized publication status (`localizeStatus`) is supported at the level described in [Compatibility](#compatibility-and-documentation).
|
|
223
|
+
|
|
224
|
+
For Globals, the same `localePrefixes` apply to the template. `/about` becomes `/fr/about` in French; use a localized slug field for the translated page name, such as `/fr/a-propos` and `/de/uber-uns`.
|
|
225
|
+
|
|
226
|
+
### Manual redirects
|
|
227
|
+
|
|
228
|
+
`redirects()` registers a Payload collection named `redirects`. Editors create and edit rules in the Payload Admin. Manual rules need no automatic configuration and no tracked Collection or Global.
|
|
229
|
+
|
|
230
|
+
| From | To | Status |
|
|
231
|
+
| -------------- | -------------------------------------- | ------ |
|
|
232
|
+
| `/old-page` | `/new-page` | `301` |
|
|
233
|
+
| `/old-guide` | `/guides/getting-started#installation` | `301` |
|
|
234
|
+
| `/summer-sale` | `/offers` | `302` |
|
|
235
|
+
|
|
236
|
+
Use `301` for a permanent move. Use `302` for a temporary destination, such as a seasonal offer. Destinations may include a fragment. Sources may not.
|
|
237
|
+
|
|
238
|
+
### Campaign links
|
|
135
239
|
|
|
136
|
-
|
|
240
|
+
Share short, readable URLs in SMS campaigns, newsletters, or promotional materials. A manual rule sends visitors to the landing page with campaign parameters:
|
|
137
241
|
|
|
138
|
-
|
|
242
|
+
| Field | Value |
|
|
243
|
+
| ------------ | --------------------------------------------------------------------- |
|
|
244
|
+
| `from` | `/summer-2027` |
|
|
245
|
+
| `to` | `/summer-2027?utm_source=sms&utm_medium=sms&utm_campaign=summer_2027` |
|
|
246
|
+
| `statusCode` | `302` |
|
|
247
|
+
| `enabled` | `true` |
|
|
248
|
+
|
|
249
|
+
The public pathname stays the same. Only the destination carries the parameters.
|
|
250
|
+
|
|
251
|
+
Query strings follow these rules:
|
|
252
|
+
|
|
253
|
+
- Sources and destinations may contain a query string. Matching is exact: `/campaign?ref=sms` is different from `/campaign` and from `/campaign?ref=email`.
|
|
254
|
+
- A source and destination can share a pathname when their query strings differ. The application must include the query string in its lookup, shown in [Application integration](#application-integration). Otherwise the landing request matches the campaign rule again.
|
|
255
|
+
- Parameters are not merged, reordered, or forwarded. The destination is the stored `to`.
|
|
256
|
+
- Automatic routes have no query strings. Campaign rules are ordinary manual rules.
|
|
257
|
+
|
|
258
|
+
Editing or disabling an automatic rule in the Payload Admin turns it into a manual rule, and later publications no longer change it. A manual rule that already owns a source path is never overwritten by an automatic one.
|
|
259
|
+
|
|
260
|
+
The plugin stores these rules only. It does not collect attribution data or connect to analytics or advertising tools.
|
|
261
|
+
|
|
262
|
+
## Application integration
|
|
263
|
+
|
|
264
|
+
The plugin stores rules and never sends a response. The example below is illustrative: the plugin does not run it in a Next.js app, and how your application serves redirects is up to you. Your application looks each request up and redirects. The example below is for Next.js 16 (`src/proxy.ts`); the same logic works in any framework that can call the Payload Local API.
|
|
139
265
|
|
|
140
266
|
```ts
|
|
267
|
+
// src/proxy.ts
|
|
141
268
|
import { NextResponse, type NextRequest } from "next/server";
|
|
142
269
|
import { getPayload } from "payload";
|
|
143
270
|
import payloadConfig from "@payload-config";
|
|
@@ -147,23 +274,15 @@ function normalizeRequestPath(pathname: string): string {
|
|
|
147
274
|
return collapsed.length > 1 && collapsed.endsWith("/") ? collapsed.slice(0, -1) : collapsed;
|
|
148
275
|
}
|
|
149
276
|
|
|
150
|
-
function decodeSafely(value: string): string {
|
|
151
|
-
try {
|
|
152
|
-
return decodeURI(value);
|
|
153
|
-
} catch {
|
|
154
|
-
return value;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
277
|
export async function proxy(request: NextRequest) {
|
|
159
|
-
const
|
|
160
|
-
const
|
|
278
|
+
const { search } = request.nextUrl;
|
|
279
|
+
const from = normalizeRequestPath(request.nextUrl.pathname) + search;
|
|
161
280
|
|
|
162
281
|
const payload = await getPayload({ config: payloadConfig });
|
|
163
282
|
const { docs } = await payload.find({
|
|
164
283
|
collection: "redirects",
|
|
165
284
|
where: {
|
|
166
|
-
and: [{ from: {
|
|
285
|
+
and: [{ from: { equals: from } }, { enabled: { equals: true } }],
|
|
167
286
|
},
|
|
168
287
|
limit: 1,
|
|
169
288
|
pagination: false,
|
|
@@ -174,7 +293,11 @@ export async function proxy(request: NextRequest) {
|
|
|
174
293
|
const rule = docs[0];
|
|
175
294
|
if (!rule) return NextResponse.next();
|
|
176
295
|
|
|
177
|
-
|
|
296
|
+
// Never redirect a request to the URL it already has.
|
|
297
|
+
const target = new URL(rule.to, request.url);
|
|
298
|
+
if (target.pathname + target.search === from) return NextResponse.next();
|
|
299
|
+
|
|
300
|
+
return NextResponse.redirect(target, Number(rule.statusCode) as 301 | 302);
|
|
178
301
|
}
|
|
179
302
|
|
|
180
303
|
export const config = {
|
|
@@ -182,273 +305,99 @@ export const config = {
|
|
|
182
305
|
};
|
|
183
306
|
```
|
|
184
307
|
|
|
185
|
-
`@payload-config` is the alias that the Payload Next.js template sets up. It points to your `payload.config.ts`.
|
|
186
|
-
|
|
187
|
-
## How it works
|
|
188
|
-
|
|
189
|
-
The plugin compares the complete public path of the newly published version with the path of the previously published version. It does not compare slugs alone.
|
|
190
|
-
|
|
191
|
-
| Event | Result |
|
|
192
|
-
| ------------------------------------------------------------------ | --------------------------------------------------------------------------- |
|
|
193
|
-
| First publication | No rule. |
|
|
194
|
-
| Draft save | No rule. |
|
|
195
|
-
| Republication, path unchanged | An identical enabled automatic rule is left as is. |
|
|
196
|
-
| Republication, path changed | A `301` from the old path to the new one is created or updated and enabled. |
|
|
197
|
-
| Previous published version has no resolvable path in a locale | That locale is skipped. |
|
|
198
|
-
| A manual rule already owns the source path | The automatic write is skipped and the manual rule stays unchanged. |
|
|
199
|
-
| Editor edits or manually disables an automatic rule | Its origin becomes `manual`, and later publications no longer manage it. |
|
|
200
|
-
| Unpublish or delete | No new rule. Existing rules stay. |
|
|
201
|
-
| Return to an earlier path that would close a loop in enabled rules | The write is rejected and the publication fails. |
|
|
308
|
+
`@payload-config` is the alias that the Payload Next.js template sets up. It points to your `payload.config.ts`. If you renamed the redirects collection with `overrides.slug`, use that slug here.
|
|
202
309
|
|
|
203
|
-
|
|
310
|
+
How the lookup matches:
|
|
204
311
|
|
|
205
|
-
|
|
312
|
+
- The lookup is exact. It joins the normalized pathname and the query string (`request.nextUrl.search`, which includes the leading `?`). `/campaign?ref=sms` matches only that query string.
|
|
313
|
+
- The path is not decoded. A browser always sends encoded characters, so store the encoded form of a path: a rule for `/café` must be stored as `/caf%C3%A9`.
|
|
314
|
+
- Trailing slashes and repeated slashes are normalized the same way the plugin stores paths, so `/articles/hello-world/` matches `/articles/hello-world`.
|
|
315
|
+
- Next.js may already redirect some URLs before `proxy.ts` runs, for example to add or remove a trailing slash.
|
|
206
316
|
|
|
207
|
-
|
|
317
|
+
A pathname-only rule does not match a request with extra query parameters. If you want generic pathname rules to apply to any query string, add a second lookup in your application after the exact one, and make sure the redirect cannot repeat. Keep the exact match first. The plugin does not provide this fallback.
|
|
208
318
|
|
|
209
|
-
|
|
319
|
+
## Configuration and access control
|
|
210
320
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
| Option | Type | Description |
|
|
214
|
-
| --------- | ------------------------------------- | --------------------------------------------------------------------------------------------------------------- |
|
|
215
|
-
| `path` | `string` | One site-relative template for all locales. Starts with `/`; no query string or fragment. |
|
|
216
|
-
| `paths` | `Record<Locale, string>` | One template per Payload locale. Requires `localization`. Must cover every locale exactly. |
|
|
217
|
-
| `getPath` | `({ doc, locale }) => string \| null` | Synchronous callback for conditional routes. Returns a complete site-relative path, or `null` to skip a locale. |
|
|
218
|
-
| `enabled` | `boolean` | Defaults to `true` when a strategy is set. `false` alone keeps manual redirects and stops generation. |
|
|
219
|
-
|
|
220
|
-
Use exactly one of `path`, `paths`, or `getPath`. `enabled: false` cannot be combined with a strategy.
|
|
221
|
-
|
|
222
|
-
Type the option with `RedirectCollectionOptions<"articles">` for a Collection or `RedirectGlobalOptions<"about">` for a Global. Both are generic over the slug, so `doc` in `getPath` has that entity's fields.
|
|
223
|
-
|
|
224
|
-
**`path`** uses top-level field names in brackets. Each value must be a non-empty string or finite number. The plugin encodes it as one path segment:
|
|
225
|
-
|
|
226
|
-
```ts
|
|
227
|
-
// src/collections/Articles.ts (custom only)
|
|
228
|
-
custom: {
|
|
229
|
-
redirect: {
|
|
230
|
-
path: "/articles/[category]/[slug]",
|
|
231
|
-
} satisfies RedirectCollectionOptions<"articles">,
|
|
232
|
-
},
|
|
233
|
-
```
|
|
234
|
-
|
|
235
|
-
A missing or unsafe value skips path resolution for that locale.
|
|
236
|
-
|
|
237
|
-
**`getPath`** handles conditional routes. Here is a complete collection with it:
|
|
238
|
-
|
|
239
|
-
```ts
|
|
240
|
-
// src/collections/Articles.ts
|
|
241
|
-
import type { CollectionConfig } from "payload";
|
|
242
|
-
import type { RedirectCollectionOptions } from "@klnap/payload-redirect";
|
|
243
|
-
|
|
244
|
-
export const Articles: CollectionConfig = {
|
|
245
|
-
slug: "articles",
|
|
246
|
-
versions: { drafts: true },
|
|
247
|
-
admin: { useAsTitle: "title" },
|
|
248
|
-
fields: [
|
|
249
|
-
{ name: "title", type: "text", required: true },
|
|
250
|
-
{ name: "slug", type: "text", required: true },
|
|
251
|
-
{ name: "category", type: "text", required: true },
|
|
252
|
-
],
|
|
253
|
-
custom: {
|
|
254
|
-
redirect: {
|
|
255
|
-
getPath: ({ doc }) => {
|
|
256
|
-
if (!doc.category || !doc.slug) return null;
|
|
257
|
-
|
|
258
|
-
const category = encodeURIComponent(doc.category);
|
|
259
|
-
const slug = encodeURIComponent(doc.slug);
|
|
260
|
-
|
|
261
|
-
return doc.category === "interviews"
|
|
262
|
-
? `/interviews/${slug}`
|
|
263
|
-
: `/articles/${category}/${slug}`;
|
|
264
|
-
},
|
|
265
|
-
} satisfies RedirectCollectionOptions<"articles">,
|
|
266
|
-
},
|
|
267
|
-
};
|
|
268
|
-
```
|
|
269
|
-
|
|
270
|
-
The callback is synchronous. The plugin does not add locale prefixes to its result, so add them yourself when you need them. Return `null` to skip a locale. An exception thrown by the callback is not converted to a skip.
|
|
271
|
-
|
|
272
|
-
### Central configuration: `redirects()`
|
|
273
|
-
|
|
274
|
-
You can also configure entities in the plugin call. This is useful when you cannot edit the Collection or Global, or when the route is a site-wide decision:
|
|
275
|
-
|
|
276
|
-
```ts
|
|
277
|
-
redirects({
|
|
278
|
-
localePrefixes: { en: "", fr: "/fr", de: "/de" },
|
|
279
|
-
collections: {
|
|
280
|
-
articles: { path: "/articles/[category]/[slug]" },
|
|
281
|
-
},
|
|
282
|
-
globals: {
|
|
283
|
-
about: { path: "/[slug]" },
|
|
284
|
-
},
|
|
285
|
-
});
|
|
286
|
-
```
|
|
287
|
-
|
|
288
|
-
**Precedence.** A central entry replaces that entity's `custom.redirect`. The two are never merged. Central `collections` and `globals` maps are independent, even when their slugs match.
|
|
289
|
-
|
|
290
|
-
## Localization
|
|
291
|
-
|
|
292
|
-
The plugin uses Payload's native content localization. The complete setup is in [Complete example](#complete-example): `localization` in `buildConfig`, localized fields in the collection, and a `paths` map in `custom.redirect`.
|
|
293
|
-
|
|
294
|
-
With those templates, the same article resolves to:
|
|
295
|
-
|
|
296
|
-
| Locale | Published slug | Public path |
|
|
297
|
-
| ------ | ------------------------------ | ------------------------------------------ |
|
|
298
|
-
| `en` | `getting-started-with-payload` | `/articles/getting-started-with-payload` |
|
|
299
|
-
| `fr` | `premiers-pas-avec-payload` | `/fr/actualites/premiers-pas-avec-payload` |
|
|
300
|
-
| `de` | `erste-schritte-mit-payload` | `/de/artikel/erste-schritte-mit-payload` |
|
|
301
|
-
|
|
302
|
-
Publishing a new French slug `introduction-a-payload` creates only this redirect:
|
|
303
|
-
|
|
304
|
-
```text
|
|
305
|
-
/fr/actualites/premiers-pas-avec-payload
|
|
306
|
-
→ /fr/actualites/introduction-a-payload (301)
|
|
307
|
-
```
|
|
308
|
-
|
|
309
|
-
Rules:
|
|
321
|
+
### Plugin options
|
|
310
322
|
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
- Payload content localization translates article data. Payload `i18n` translates the Admin UI. Plugin labels and validation messages are English.
|
|
323
|
+
| Option | Default | Description |
|
|
324
|
+
| ---------------- | ----------------------------------- | ----------------------------------------------------------------------------------------- |
|
|
325
|
+
| `collections` | None | Central configuration for Collections. Replaces the entity's `custom.redirect`. |
|
|
326
|
+
| `globals` | None | Central configuration for Globals. Replaces the entity's `custom.redirect`. |
|
|
327
|
+
| `localePrefixes` | No prefixes | One prefix per locale, applied to `path` and `paths`. Must list every locale. |
|
|
328
|
+
| `overrides` | Plugin-owned `redirects` collection | Native Payload collection options, plus `fields` to add fields. Required fields are kept. |
|
|
318
329
|
|
|
319
|
-
|
|
330
|
+
### Overrides
|
|
320
331
|
|
|
321
|
-
|
|
332
|
+
`overrides` passes options to the plugin-owned collection. This example renames it and adds a note field:
|
|
322
333
|
|
|
323
334
|
```ts
|
|
324
335
|
redirects({
|
|
325
|
-
|
|
326
|
-
|
|
336
|
+
overrides: {
|
|
337
|
+
slug: "url-redirects",
|
|
338
|
+
admin: { group: "Settings" },
|
|
339
|
+
fields: ({ defaultFields }) => [...defaultFields, { name: "editorNote", type: "textarea" }],
|
|
327
340
|
},
|
|
328
341
|
});
|
|
329
342
|
```
|
|
330
343
|
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
`redirects()` registers the plugin-owned `redirects` collection. Create, edit, enable, or disable manual rules in the Payload Admin. Manual rules need no automatic configuration.
|
|
334
|
-
|
|
335
|
-
- `from` is a normalized site-relative path. Duplicates are rejected.
|
|
336
|
-
- `to` is a normalized site-relative path that differs from `from`. Absolute URLs are not accepted here.
|
|
337
|
-
- `statusCode` is `301` (default) or `302`.
|
|
338
|
-
- `enabled` defaults to `true`. `origin` is `manual` for rules you create and `auto` for generated ones.
|
|
339
|
-
|
|
340
|
-
Register `redirects()` once. A second registration throws. The collection slug defaults to `redirects`. Change it with `overrides.slug` if that slug is taken.
|
|
344
|
+
Then change `collection: "redirects"` to `collection: "url-redirects"` in your application lookup.
|
|
341
345
|
|
|
342
|
-
|
|
346
|
+
### Access control
|
|
343
347
|
|
|
344
|
-
The
|
|
348
|
+
The redirects collection uses Payload's native access defaults. Anyone who is not logged in cannot read or write rules.
|
|
345
349
|
|
|
346
|
-
|
|
350
|
+
Automatic generation runs inside the publication hook with trusted Local API calls (`overrideAccess: true`). Editors who publish content therefore create redirects without holding redirect permissions. REST, GraphQL, and user-scoped Local API calls follow your access rules.
|
|
347
351
|
|
|
348
|
-
|
|
349
|
-
| ------------------------ | ------------------------------------ |
|
|
350
|
-
| `/articles/old/` | `/articles/old` |
|
|
351
|
-
| `/articles//old` | `/articles/old` |
|
|
352
|
-
| `/a/../b` | `/b` |
|
|
353
|
-
| `/café` | `/café` (distinct from `/caf%C3%A9`) |
|
|
354
|
-
| `/articles/old?ref=mail` | `/articles/old?ref=mail` |
|
|
355
|
-
|
|
356
|
-
Normalization trims whitespace, adds a leading slash, collapses repeated slashes, resolves `.` and `..` segments (including percent-encoded forms), and removes a trailing slash except at `/`. It preserves Unicode, percent encoding, and query strings. Fragments are rejected.
|
|
357
|
-
|
|
358
|
-
The example looks up the pathname only, so rules with a query string never match. If you need them, add the query string to the lookup yourself. Lookup compares the stored value exactly, so case and encoding matter.
|
|
359
|
-
|
|
360
|
-
## Overrides and access
|
|
361
|
-
|
|
362
|
-
`overrides` passes native Payload collection options to the plugin-owned collection. This example renames it, moves it into a group, adds a field, and makes it readable:
|
|
352
|
+
Public read access is opt-in:
|
|
363
353
|
|
|
364
354
|
```ts
|
|
365
|
-
// payload.config.ts
|
|
366
355
|
redirects({
|
|
367
|
-
localePrefixes: { en: "", fr: "/fr", de: "/de" },
|
|
368
356
|
overrides: {
|
|
369
|
-
slug: "url-redirects",
|
|
370
|
-
admin: { group: "Settings" },
|
|
371
357
|
access: {
|
|
372
358
|
read: () => true,
|
|
373
359
|
},
|
|
374
|
-
fields: ({ defaultFields }) => [...defaultFields, { name: "editorNote", type: "textarea" }],
|
|
375
360
|
},
|
|
376
361
|
});
|
|
377
362
|
```
|
|
378
363
|
|
|
379
|
-
|
|
364
|
+
Public reads return every field the rule allows, including disabled rules and editor notes. Use field-level `access` to hide fields you do not want public.
|
|
380
365
|
|
|
381
|
-
|
|
366
|
+
Your application's lookup in [Application integration](#application-integration) uses trusted server-side access and does not need public read access.
|
|
382
367
|
|
|
383
|
-
|
|
368
|
+
## Compatibility and documentation
|
|
384
369
|
|
|
385
|
-
|
|
370
|
+
| Requirement | Support |
|
|
371
|
+
| ---------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
372
|
+
| Node.js | `^22.23.5`, `^24.22.3`, or `>=26.13.1`, as declared in `engines`. |
|
|
373
|
+
| Payload | Declared peer range `>=3.94.1 <4`. The test suite runs against Payload `3.94.1`, the version in the development dependencies. Other 3.x versions are not tested. |
|
|
374
|
+
| PostgreSQL | `@payloadcms/db-postgres` peer `>=3.94.1 <4`. The integration tests run against PostgreSQL 16. Redirect graph writes are serialized with a transaction-scoped advisory lock. |
|
|
375
|
+
| Other database adapters | Not tested. No concurrency guarantee is claimed for them. |
|
|
376
|
+
| Localized publication status | Payload's experimental `localizeStatus`. The plugin follows the per-locale publication status Payload stores; see [Limitations](#limitations). |
|
|
377
|
+
| Application framework | The rule store is framework-independent. The lookup example targets Next.js 16. |
|
|
386
378
|
|
|
387
|
-
###
|
|
379
|
+
### Limitations
|
|
388
380
|
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
| `from` | `string` | Required | Unique normalized site-relative source path. No fragment. |
|
|
401
|
-
| `to` | `string` | Required | Normalized site-relative destination. Must differ from `from`. |
|
|
402
|
-
| `statusCode` | `"301" \| "302"` | `"301"` | Redirect status. |
|
|
403
|
-
| `enabled` | `boolean` | `true` | Disabled rules are not used for lookup or loop checks. |
|
|
404
|
-
| `origin` | `"manual" \| "auto"` | `manual` | Whether the plugin manages the rule. |
|
|
405
|
-
|
|
406
|
-
Enabled rules are checked for loops and for chains longer than 1,000 hops at write time. The check does not see loops created by host routing, external redirects, or browser URL equivalence.
|
|
407
|
-
|
|
408
|
-
### Exports
|
|
409
|
-
|
|
410
|
-
| Export | Kind | Purpose |
|
|
411
|
-
| --------------------------- | ----- | ----------------------------------------------------------- |
|
|
412
|
-
| `redirects` | Value | Returns the Payload plugin. |
|
|
413
|
-
| `RedirectsOptions` | Type | Options for `redirects()`. |
|
|
414
|
-
| `RedirectCollectionOptions` | Type | Options for a Collection, generic over the collection slug. |
|
|
415
|
-
| `RedirectGlobalOptions` | Type | Options for a Global, generic over the global slug. |
|
|
416
|
-
| `RedirectOrigin` | Type | `"manual" \| "auto"`. |
|
|
417
|
-
| `RedirectStatusCode` | Type | `301 \| 302`. |
|
|
418
|
-
|
|
419
|
-
## Compatibility
|
|
420
|
-
|
|
421
|
-
| Requirement | Support |
|
|
422
|
-
| ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
423
|
-
| Node.js | `^22.23.5`, `^24.22.3`, or `>=26.13.1`. |
|
|
424
|
-
| Payload | Tested with Payload `3.94.1` and `3.93.0`: unit and integration tests pass on both. Declared peer range is `>=3.94.1 <4`. Older 3.x versions should work too, but this repository has not tested them. |
|
|
425
|
-
| PostgreSQL | Optional `@payloadcms/db-postgres` peer, `>=3.94.1 <4`. Integration tests run against PostgreSQL 16. Rule-graph writes run in a Payload transaction at `READ COMMITTED`. |
|
|
426
|
-
| Other database adapters | Not verified. No equivalent concurrency guarantee is claimed. |
|
|
427
|
-
| Automatic redirects | Require `versions.drafts` on each configured Collection or Global. |
|
|
428
|
-
| Content localization | Requires `localization.locales` and `localization.defaultLocale`. Each locale resolves without fallback. |
|
|
429
|
-
| Localized publication status | Opt-in with `experimental.localizeStatus: true` and `versions.drafts.localizeStatus: true`. Payload marks this feature experimental; this package's per-locale coverage is basic. |
|
|
430
|
-
| Frontend framework | The rule store is framework-independent. The serving example targets Next.js 16. |
|
|
431
|
-
|
|
432
|
-
## Limitations
|
|
433
|
-
|
|
434
|
-
- Route changes do not migrate historical URLs. Add manual rules for older paths.
|
|
435
|
-
- Rules have no locale, domain, or host scope.
|
|
436
|
-
- Cycle detection covers enabled stored rules only.
|
|
437
|
-
- Request matching, caching, and HTTP responses are the application's responsibility. The lookup in `proxy.ts` runs on every matched request.
|
|
438
|
-
|
|
439
|
-
## Documentation
|
|
381
|
+
- Route changes do not migrate old URLs. Add manual rules for paths you changed before the plugin was installed.
|
|
382
|
+
- Automatic rules follow the published history that Payload keeps. If retention removes the version that held the previous path, no rule is created for that change.
|
|
383
|
+
- Restoring an older version publishes it and creates a rule. The restore is rejected when that rule would close a loop with enabled rules.
|
|
384
|
+
- Changes to the route configuration are not compared with old content paths. Edit manual rules for those.
|
|
385
|
+
- Source paths are unique across all Collections and Globals, and rules have no locale, domain, or host scope.
|
|
386
|
+
- Loop detection covers enabled stored rules. It cannot see loops created by host routing, external redirects, or browser URL equivalence.
|
|
387
|
+
- Request matching, caching, and HTTP responses belong to your application. Lookups run on every matched request, so keep the matcher narrow.
|
|
388
|
+
- Localized publication status depends on Payload's experimental feature. This package's coverage of it is limited to the cases described in its tests.
|
|
389
|
+
- Fragments are not allowed on source paths. Destinations may include them.
|
|
390
|
+
|
|
391
|
+
### Documentation
|
|
440
392
|
|
|
441
393
|
- [Failure semantics](./docs/FAILURE_SEMANTICS.md): exact behavior for each configuration and write error.
|
|
442
|
-
- [Releasing](
|
|
394
|
+
- [Releasing](https://github.com/klnap/payload-redirect/blob/main/docs/RELEASING.md): release process for maintainers.
|
|
395
|
+
- [Contributing](https://github.com/klnap/payload-redirect/blob/main/CONTRIBUTING.md): development setup and checks.
|
|
396
|
+
- [Security policy](https://github.com/klnap/payload-redirect/blob/main/SECURITY.md): how to report vulnerabilities.
|
|
443
397
|
- [Payload content localization](https://payloadcms.com/docs/configuration/localization)
|
|
444
|
-
- [Payload localized publication status](https://payloadcms.com/docs/configuration/localization#status-localization)
|
|
445
398
|
- [Payload drafts and versions](https://payloadcms.com/docs/versions/drafts)
|
|
446
399
|
- [Payload Local API](https://payloadcms.com/docs/local-api/overview)
|
|
447
400
|
|
|
448
|
-
## Contributing
|
|
449
|
-
|
|
450
|
-
See [CONTRIBUTING.md](./CONTRIBUTING.md). Report security issues through [SECURITY.md](./SECURITY.md).
|
|
451
|
-
|
|
452
401
|
## License
|
|
453
402
|
|
|
454
|
-
[
|
|
403
|
+
MIT. See [LICENSE](./LICENSE).
|