@klnap/payload-redirect 0.3.1 → 0.4.1
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 +19 -0
- package/README.md +368 -168
- package/dist/core/redirects/auto/create-global-redirect.d.ts +11 -0
- package/dist/core/redirects/auto/create-global-redirect.js +18 -0
- package/dist/core/redirects/auto/create-global-redirect.js.map +1 -0
- package/dist/core/redirects/auto/create-redirect.d.ts +10 -6
- package/dist/core/redirects/auto/create-redirect.js +18 -9
- package/dist/core/redirects/auto/create-redirect.js.map +1 -1
- package/dist/core/redirects/auto/persist-auto-rule.d.ts +1 -1
- package/dist/core/redirects/auto/persist-auto-rule.js +8 -5
- package/dist/core/redirects/auto/persist-auto-rule.js.map +1 -1
- package/dist/core/redirects/auto/publication/global-transition.d.ts +6 -0
- package/dist/core/redirects/auto/publication/global-transition.js +63 -0
- package/dist/core/redirects/auto/publication/global-transition.js.map +1 -0
- package/dist/core/redirects/auto/publication/locales.d.ts +1 -0
- package/dist/core/redirects/auto/publication/locales.js +4 -0
- package/dist/core/redirects/auto/publication/locales.js.map +1 -1
- package/dist/core/redirects/auto/types.d.ts +3 -3
- package/dist/core/redirects/auto/types.js.map +1 -1
- package/dist/core/redirects/collection/collection.js +5 -3
- 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/fields.js +0 -6
- package/dist/core/redirects/collection/fields.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/dist/core/redirects/collection/origin-hook.js +3 -2
- package/dist/core/redirects/collection/origin-hook.js.map +1 -1
- package/dist/core/redirects/context-keys.d.ts +1 -1
- package/dist/core/redirects/context-keys.js +1 -1
- package/dist/core/redirects/context-keys.js.map +1 -1
- package/dist/exports/index.d.ts +1 -1
- package/dist/exports/index.js.map +1 -1
- package/dist/plugin/payload-plugin.js +54 -19
- package/dist/plugin/payload-plugin.js.map +1 -1
- package/dist/plugin/types.d.ts +7 -2
- package/dist/plugin/types.js.map +1 -1
- package/docs/FAILURE_SEMANTICS.md +25 -6
- package/package.json +1 -1
- package/dist/core/redirects/collection/access.d.ts +0 -8
- package/dist/core/redirects/collection/access.js +0 -10
- package/dist/core/redirects/collection/access.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,272 +1,472 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@klnap/payload-redirect`
|
|
2
2
|
|
|
3
|
-
Redirect management for Payload CMS.
|
|
3
|
+
Redirect management for Payload CMS. When a published Collection or Global path changes, the plugin stores a permanent redirect from the old path to the new one. Editors can also create manual `301` and `302` rules in the Payload Admin.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
The plugin stores rules. Your application matches incoming requests and sends the response. A complete Next.js example is in [Serving redirects](#serving-redirects).
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
```text
|
|
8
|
+
/articles/guides/getting-started-with-payload
|
|
9
|
+
→ /articles/tutorials/getting-started-with-payload (301)
|
|
10
|
+
```
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
- **Automatic `301`s** when a published path changes, including changes that do not touch the slug, such as a category.
|
|
15
|
+
- **Configuration beside the schema** with `custom.redirect` on each Collection or Global.
|
|
16
|
+
- **Complete-path tracking** through a path template or a typed `getPath` callback.
|
|
17
|
+
- **Native localization** with per-locale templates, explicit prefixes, and no locale fallback.
|
|
18
|
+
- **Globals** with the same publication and redirect rules as Collections.
|
|
19
|
+
- **Manual rules** in the Payload Admin, enabled or disabled, with no automatic configuration.
|
|
20
|
+
- **Safe writes**: duplicate and cyclic rules are rejected, and manual rules are never overwritten.
|
|
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)
|
|
12
38
|
|
|
13
39
|
## Installation
|
|
14
40
|
|
|
15
|
-
```
|
|
41
|
+
```bash
|
|
16
42
|
bun add @klnap/payload-redirect
|
|
17
43
|
```
|
|
18
44
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
## Manual redirects
|
|
23
|
-
|
|
24
|
-
Register the plugin with no options:
|
|
25
|
-
|
|
26
|
-
```ts
|
|
27
|
-
import { buildConfig } from "payload";
|
|
28
|
-
import { postgresAdapter } from "@payloadcms/db-postgres";
|
|
29
|
-
import { redirects } from "@klnap/payload-redirect";
|
|
30
|
-
|
|
31
|
-
export default buildConfig({
|
|
32
|
-
secret: process.env.PAYLOAD_SECRET!,
|
|
33
|
-
db: postgresAdapter({ pool: { connectionString: process.env.DATABASE_URI! } }),
|
|
34
|
-
plugins: [redirects()],
|
|
35
|
-
});
|
|
45
|
+
```bash
|
|
46
|
+
npm install @klnap/payload-redirect
|
|
36
47
|
```
|
|
37
48
|
|
|
38
|
-
|
|
49
|
+
`payload` is a required peer. `@payloadcms/db-postgres` is an optional peer, needed only with PostgreSQL.
|
|
50
|
+
|
|
51
|
+
## Complete example
|
|
39
52
|
|
|
40
|
-
|
|
53
|
+
These four files form a working setup for a Payload 3 app with Next.js 16 (App Router) and PostgreSQL. Each one is complete. The `custom` key is the extension point: Payload stores it as-is on collections, globals, and fields. This plugin reads only `custom.redirect`. You can use other `custom` keys for your own metadata.
|
|
41
54
|
|
|
42
|
-
|
|
55
|
+
### 1. `src/payload.config.ts`
|
|
43
56
|
|
|
44
57
|
```ts
|
|
45
|
-
import { buildConfig } from "payload";
|
|
46
58
|
import { postgresAdapter } from "@payloadcms/db-postgres";
|
|
59
|
+
import { lexicalEditor } from "@payloadcms/richtext-lexical";
|
|
60
|
+
import { buildConfig } from "payload";
|
|
47
61
|
import { redirects } from "@klnap/payload-redirect";
|
|
62
|
+
import { Articles } from "./collections/Articles";
|
|
63
|
+
import { About } from "./globals/About";
|
|
48
64
|
|
|
49
65
|
export default buildConfig({
|
|
50
|
-
secret: process.env.PAYLOAD_SECRET
|
|
51
|
-
|
|
66
|
+
secret: process.env.PAYLOAD_SECRET ?? "",
|
|
67
|
+
editor: lexicalEditor(),
|
|
68
|
+
db: postgresAdapter({
|
|
69
|
+
pool: { connectionString: process.env.DATABASE_URI },
|
|
70
|
+
}),
|
|
52
71
|
localization: {
|
|
53
|
-
locales: ["
|
|
54
|
-
defaultLocale: "
|
|
72
|
+
locales: ["en", "fr", "de"],
|
|
73
|
+
defaultLocale: "en",
|
|
55
74
|
},
|
|
56
|
-
collections: [
|
|
57
|
-
|
|
58
|
-
slug: "articles",
|
|
59
|
-
versions: { drafts: true },
|
|
60
|
-
fields: [
|
|
61
|
-
{ name: "title", type: "text", required: true },
|
|
62
|
-
{ name: "slug", type: "text", required: true, localized: true },
|
|
63
|
-
],
|
|
64
|
-
},
|
|
65
|
-
{
|
|
66
|
-
slug: "projects",
|
|
67
|
-
versions: { drafts: true },
|
|
68
|
-
fields: [{ name: "slug", type: "text", required: true, localized: true }],
|
|
69
|
-
},
|
|
70
|
-
],
|
|
75
|
+
collections: [Articles],
|
|
76
|
+
globals: [About],
|
|
71
77
|
plugins: [
|
|
72
78
|
redirects({
|
|
73
|
-
localePrefixes: {
|
|
74
|
-
pl: "",
|
|
75
|
-
en: "/en",
|
|
76
|
-
de: "/de",
|
|
77
|
-
},
|
|
78
|
-
collections: {
|
|
79
|
-
articles: { path: "/[slug]" },
|
|
80
|
-
projects: {
|
|
81
|
-
paths: {
|
|
82
|
-
pl: "/realizacje/[slug]",
|
|
83
|
-
en: "/work/[slug]",
|
|
84
|
-
de: "/projekte/[slug]",
|
|
85
|
-
},
|
|
86
|
-
},
|
|
87
|
-
},
|
|
79
|
+
localePrefixes: { en: "", fr: "/fr", de: "/de" },
|
|
88
80
|
}),
|
|
89
81
|
],
|
|
90
82
|
});
|
|
91
83
|
```
|
|
92
84
|
|
|
93
|
-
|
|
85
|
+
`redirects()` is called once. It adds the `redirects` collection, the publication hooks, and the locale prefixes.
|
|
94
86
|
|
|
95
|
-
|
|
87
|
+
### 2. `src/collections/Articles.ts`
|
|
96
88
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
For a Polish project, a slug change creates this rule:
|
|
89
|
+
```ts
|
|
90
|
+
import type { CollectionConfig } from "payload";
|
|
91
|
+
import type { RedirectCollectionOptions } from "@klnap/payload-redirect";
|
|
102
92
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
93
|
+
export const Articles: CollectionConfig = {
|
|
94
|
+
slug: "articles",
|
|
95
|
+
versions: { drafts: true },
|
|
96
|
+
admin: { useAsTitle: "title" },
|
|
97
|
+
fields: [
|
|
98
|
+
{ name: "title", type: "text", required: true, localized: true },
|
|
99
|
+
{ name: "slug", type: "text", required: true, localized: true },
|
|
100
|
+
{ name: "category", type: "text", required: true },
|
|
101
|
+
],
|
|
102
|
+
custom: {
|
|
103
|
+
redirect: {
|
|
104
|
+
paths: {
|
|
105
|
+
en: "/articles/[slug]",
|
|
106
|
+
fr: "/actualites/[slug]",
|
|
107
|
+
de: "/artikel/[slug]",
|
|
108
|
+
},
|
|
109
|
+
} satisfies RedirectCollectionOptions<"articles">,
|
|
110
|
+
},
|
|
111
|
+
};
|
|
107
112
|
```
|
|
108
113
|
|
|
109
|
-
|
|
114
|
+
Tracked collections need `versions: { drafts: true }`. The `satisfies` clause types the route against this collection. Use `paths` because the collection is localized. Locales in `paths` must match `localization.locales` exactly.
|
|
110
115
|
|
|
111
|
-
###
|
|
112
|
-
|
|
113
|
-
Use the same options directly on a collection when central configuration is not convenient:
|
|
116
|
+
### 3. `src/globals/About.ts`
|
|
114
117
|
|
|
115
118
|
```ts
|
|
116
|
-
import type {
|
|
117
|
-
import type {
|
|
119
|
+
import type { GlobalConfig } from "payload";
|
|
120
|
+
import type { RedirectGlobalOptions } from "@klnap/payload-redirect";
|
|
118
121
|
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
pl: "/realizacje/[slug]",
|
|
122
|
-
en: "/work/[slug]",
|
|
123
|
-
de: "/projekte/[slug]",
|
|
124
|
-
},
|
|
125
|
-
} satisfies RedirectCollectionOptions<"projects">;
|
|
126
|
-
|
|
127
|
-
export const Projects: CollectionConfig = {
|
|
128
|
-
slug: "projects",
|
|
122
|
+
export const About: GlobalConfig = {
|
|
123
|
+
slug: "about",
|
|
129
124
|
versions: { drafts: true },
|
|
125
|
+
fields: [{ name: "slug", type: "text", required: true, localized: true }],
|
|
130
126
|
custom: {
|
|
131
|
-
redirect:
|
|
127
|
+
redirect: {
|
|
128
|
+
getPath: ({ doc, locale }) => `${locale === "fr" ? "/fr" : ""}/${doc.slug}`,
|
|
129
|
+
} satisfies RedirectGlobalOptions<"about">,
|
|
132
130
|
},
|
|
133
|
-
fields: [{ name: "slug", type: "text", required: true, localized: true }],
|
|
134
131
|
};
|
|
135
132
|
```
|
|
136
133
|
|
|
137
|
-
|
|
134
|
+
`getPath` returns the complete path. The plugin does not add prefixes to it, so the French prefix is added here.
|
|
138
135
|
|
|
139
|
-
|
|
136
|
+
### 4. `src/proxy.ts`
|
|
140
137
|
|
|
141
|
-
|
|
138
|
+
Next.js 16 runs `proxy.ts` on the Node.js runtime by default, so it can use Payload's Local API. Redirects are sent from here with the status stored on the rule.
|
|
142
139
|
|
|
143
|
-
|
|
140
|
+
```ts
|
|
141
|
+
import { NextResponse, type NextRequest } from "next/server";
|
|
142
|
+
import { getPayload } from "payload";
|
|
143
|
+
import payloadConfig from "@payload-config";
|
|
144
144
|
|
|
145
|
-
|
|
145
|
+
function normalizeRequestPath(pathname: string): string {
|
|
146
|
+
const collapsed = pathname.replace(/\/{2,}/g, "/");
|
|
147
|
+
return collapsed.length > 1 && collapsed.endsWith("/") ? collapsed.slice(0, -1) : collapsed;
|
|
148
|
+
}
|
|
146
149
|
|
|
147
|
-
|
|
150
|
+
export async function proxy(request: NextRequest) {
|
|
151
|
+
const { search } = request.nextUrl;
|
|
152
|
+
const from = normalizeRequestPath(request.nextUrl.pathname) + search;
|
|
148
153
|
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
154
|
+
const payload = await getPayload({ config: payloadConfig });
|
|
155
|
+
const { docs } = await payload.find({
|
|
156
|
+
collection: "redirects",
|
|
157
|
+
where: {
|
|
158
|
+
and: [{ from: { equals: from } }, { enabled: { equals: true } }],
|
|
159
|
+
},
|
|
160
|
+
limit: 1,
|
|
161
|
+
pagination: false,
|
|
162
|
+
depth: 0,
|
|
163
|
+
overrideAccess: true,
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
const rule = docs[0];
|
|
167
|
+
if (!rule) return NextResponse.next();
|
|
168
|
+
|
|
169
|
+
const target = new URL(rule.to, request.url);
|
|
170
|
+
// A rule that points at the current path and query would redirect forever. Fragments are not sent to the server.
|
|
171
|
+
if (target.pathname + target.search === from) return NextResponse.next();
|
|
172
|
+
|
|
173
|
+
return NextResponse.redirect(target, Number(rule.statusCode) as 301 | 302);
|
|
152
174
|
}
|
|
175
|
+
|
|
176
|
+
export const config = {
|
|
177
|
+
matcher: ["/((?!api|admin|_next/static|_next/image|favicon.ico).*)"],
|
|
178
|
+
};
|
|
153
179
|
```
|
|
154
180
|
|
|
155
|
-
|
|
181
|
+
`@payload-config` is the alias that the Payload Next.js template sets up. It points to your `payload.config.ts`. The lookup runs on every matched request, so keep the matcher narrow for your public routes. See [Serving redirects](#serving-redirects) for the path rules.
|
|
182
|
+
|
|
183
|
+
## How it works
|
|
184
|
+
|
|
185
|
+
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.
|
|
186
|
+
|
|
187
|
+
| Event | Result |
|
|
188
|
+
| ------------------------------------------------------------------ | --------------------------------------------------------------------------- |
|
|
189
|
+
| First publication | No rule. |
|
|
190
|
+
| Draft save | No rule. |
|
|
191
|
+
| Republication, path unchanged | An identical enabled automatic rule is left as is. |
|
|
192
|
+
| Republication, path changed | A `301` from the old path to the new one is created or updated and enabled. |
|
|
193
|
+
| Previous published version has no resolvable path in a locale | That locale is skipped. |
|
|
194
|
+
| A manual rule already owns the source path | The automatic write is skipped and the manual rule stays unchanged. |
|
|
195
|
+
| Editor edits or manually disables an automatic rule | Its origin becomes `manual`, and later publications no longer manage it. |
|
|
196
|
+
| Unpublish or delete | No new rule. Existing rules stay. |
|
|
197
|
+
| Return to an earlier path that would close a loop in enabled rules | The write is rejected and the publication fails. |
|
|
198
|
+
|
|
199
|
+
Route changes do not migrate historical URLs. Add manual rules for older paths if you need them.
|
|
200
|
+
|
|
201
|
+
## Configuration
|
|
202
|
+
|
|
203
|
+
You can configure automatic redirects in two places. Use `custom.redirect` by default.
|
|
204
|
+
|
|
205
|
+
### `custom.redirect` on the entity
|
|
206
|
+
|
|
207
|
+
Each Collection or Global gets its own `custom.redirect` object. The plugin reads it when Payload builds the config, so the route sits next to the fields it uses.
|
|
208
|
+
|
|
209
|
+
| Option | Type | Description |
|
|
210
|
+
| --------- | ------------------------------------- | --------------------------------------------------------------------------------------------------------------- |
|
|
211
|
+
| `path` | `string` | One site-relative template for all locales. Starts with `/`; no query string or fragment. |
|
|
212
|
+
| `paths` | `Record<Locale, string>` | One template per Payload locale. Requires `localization`. Must cover every locale exactly. |
|
|
213
|
+
| `getPath` | `({ doc, locale }) => string \| null` | Synchronous callback for conditional routes. Returns a complete site-relative path, or `null` to skip a locale. |
|
|
214
|
+
| `enabled` | `boolean` | Defaults to `true` when a strategy is set. `false` alone keeps manual redirects and stops generation. |
|
|
215
|
+
|
|
216
|
+
Use exactly one of `path`, `paths`, or `getPath`. `enabled: false` cannot be combined with a strategy.
|
|
217
|
+
|
|
218
|
+
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.
|
|
219
|
+
|
|
220
|
+
**`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:
|
|
156
221
|
|
|
157
222
|
```ts
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
223
|
+
// src/collections/Articles.ts (custom only)
|
|
224
|
+
custom: {
|
|
225
|
+
redirect: {
|
|
226
|
+
path: "/articles/[category]/[slug]",
|
|
227
|
+
} satisfies RedirectCollectionOptions<"articles">,
|
|
228
|
+
},
|
|
161
229
|
```
|
|
162
230
|
|
|
163
|
-
|
|
231
|
+
A missing or unsafe value skips path resolution for that locale.
|
|
164
232
|
|
|
165
|
-
|
|
233
|
+
**`getPath`** handles conditional routes. Here is a complete collection with it:
|
|
166
234
|
|
|
167
|
-
|
|
235
|
+
```ts
|
|
236
|
+
// src/collections/Articles.ts
|
|
237
|
+
import type { CollectionConfig } from "payload";
|
|
238
|
+
import type { RedirectCollectionOptions } from "@klnap/payload-redirect";
|
|
239
|
+
|
|
240
|
+
export const Articles: CollectionConfig = {
|
|
241
|
+
slug: "articles",
|
|
242
|
+
versions: { drafts: true },
|
|
243
|
+
admin: { useAsTitle: "title" },
|
|
244
|
+
fields: [
|
|
245
|
+
{ name: "title", type: "text", required: true },
|
|
246
|
+
{ name: "slug", type: "text", required: true },
|
|
247
|
+
{ name: "category", type: "text", required: true },
|
|
248
|
+
],
|
|
249
|
+
custom: {
|
|
250
|
+
redirect: {
|
|
251
|
+
getPath: ({ doc }) => {
|
|
252
|
+
if (!doc.category || !doc.slug) return null;
|
|
253
|
+
|
|
254
|
+
const category = encodeURIComponent(doc.category);
|
|
255
|
+
const slug = encodeURIComponent(doc.slug);
|
|
256
|
+
|
|
257
|
+
return doc.category === "interviews"
|
|
258
|
+
? `/interviews/${slug}`
|
|
259
|
+
: `/articles/${category}/${slug}`;
|
|
260
|
+
},
|
|
261
|
+
} satisfies RedirectCollectionOptions<"articles">,
|
|
262
|
+
},
|
|
263
|
+
};
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
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.
|
|
267
|
+
|
|
268
|
+
### Central configuration: `redirects()`
|
|
269
|
+
|
|
270
|
+
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:
|
|
168
271
|
|
|
169
272
|
```ts
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
de: '/projekte/[slug]',
|
|
273
|
+
redirects({
|
|
274
|
+
localePrefixes: { en: "", fr: "/fr", de: "/de" },
|
|
275
|
+
collections: {
|
|
276
|
+
articles: { path: "/articles/[category]/[slug]" },
|
|
175
277
|
},
|
|
176
|
-
|
|
278
|
+
globals: {
|
|
279
|
+
about: { path: "/[slug]" },
|
|
280
|
+
},
|
|
281
|
+
});
|
|
177
282
|
```
|
|
178
283
|
|
|
179
|
-
|
|
284
|
+
**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.
|
|
285
|
+
|
|
286
|
+
## Localization
|
|
287
|
+
|
|
288
|
+
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`.
|
|
180
289
|
|
|
181
|
-
|
|
290
|
+
With those templates, the same article resolves to:
|
|
182
291
|
|
|
183
|
-
|
|
292
|
+
| Locale | Published slug | Public path |
|
|
293
|
+
| ------ | ------------------------------ | ------------------------------------------ |
|
|
294
|
+
| `en` | `getting-started-with-payload` | `/articles/getting-started-with-payload` |
|
|
295
|
+
| `fr` | `premiers-pas-avec-payload` | `/fr/actualites/premiers-pas-avec-payload` |
|
|
296
|
+
| `de` | `erste-schritte-mit-payload` | `/de/artikel/erste-schritte-mit-payload` |
|
|
184
297
|
|
|
185
|
-
|
|
298
|
+
Publishing a new French slug `introduction-a-payload` creates only this redirect:
|
|
299
|
+
|
|
300
|
+
```text
|
|
301
|
+
/fr/actualites/premiers-pas-avec-payload
|
|
302
|
+
→ /fr/actualites/introduction-a-payload (301)
|
|
303
|
+
```
|
|
186
304
|
|
|
187
|
-
|
|
305
|
+
Rules:
|
|
188
306
|
|
|
189
|
-
|
|
307
|
+
- `path` shares one template across locales. `paths` gives one per locale.
|
|
308
|
+
- `localePrefixes` prefixes either template strategy. It must list every Payload locale.
|
|
309
|
+
- `paths` and `localePrefixes` must match `localization.locales` exactly.
|
|
310
|
+
- Without localization, use `path` or `getPath` and omit `localePrefixes`.
|
|
311
|
+
- Generation reads current and historical published documents with fallback disabled. A locale without a slug is skipped. The plugin never borrows another locale's slug.
|
|
312
|
+
- Redirect rules have no locale or domain scope. Localized routes need distinct source paths when they need different destinations.
|
|
313
|
+
- Payload content localization translates article data. Payload `i18n` translates the Admin UI. Plugin labels and validation messages are English.
|
|
314
|
+
|
|
315
|
+
## Globals
|
|
316
|
+
|
|
317
|
+
Globals use the same publication, localization, path validation, and storage as Collections. The complete Global setup is in [Complete example](#complete-example). Give the Global `versions: { drafts: true }` and a `custom.redirect` entry, or configure it centrally:
|
|
190
318
|
|
|
191
319
|
```ts
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
}
|
|
320
|
+
redirects({
|
|
321
|
+
globals: {
|
|
322
|
+
about: { path: "/[slug]" },
|
|
323
|
+
},
|
|
324
|
+
});
|
|
198
325
|
```
|
|
199
326
|
|
|
200
|
-
|
|
327
|
+
## Manual redirects
|
|
328
|
+
|
|
329
|
+
`redirects()` registers the plugin-owned `redirects` collection. Create, edit, enable, or disable manual rules in the Payload Admin. Manual rules need no automatic configuration.
|
|
330
|
+
|
|
331
|
+
- `from` is a normalized site-relative path. Duplicates are rejected.
|
|
332
|
+
- `to` is a normalized site-relative path that differs from `from`. Absolute URLs are not accepted here.
|
|
333
|
+
- `statusCode` is `301` (default) or `302`.
|
|
334
|
+
- `enabled` defaults to `true`. `origin` is `manual` for rules you create and `auto` for generated ones.
|
|
335
|
+
|
|
336
|
+
### Campaign links
|
|
337
|
+
|
|
338
|
+
Share short, readable URLs in SMS campaigns, newsletters, or promotional materials. A manual rule redirects visitors to the landing page with campaign parameters:
|
|
339
|
+
|
|
340
|
+
| Field | Value |
|
|
341
|
+
| ------------ | --------------------------------------------------------------------- |
|
|
342
|
+
| `from` | `/summer-2027` |
|
|
343
|
+
| `to` | `/summer-2027?utm_source=sms&utm_medium=sms&utm_campaign=summer_2027` |
|
|
344
|
+
| `statusCode` | `302` |
|
|
345
|
+
| `enabled` | `true` |
|
|
346
|
+
|
|
347
|
+
Rules for query strings:
|
|
348
|
+
|
|
349
|
+
- Sources and destinations may contain a query string. Source matching is exact, so `/campaign?ref=sms` is distinct from `/campaign` and from `/campaign?ref=email`.
|
|
350
|
+
- A source and destination may share a pathname when their query strings differ. The lookup in [Serving redirects](#serving-redirects) includes `search`, so the landing request does not match the campaign rule again.
|
|
351
|
+
- The plugin does not forward or merge incoming query parameters. The destination is the stored `to`.
|
|
352
|
+
- Automatic content paths have no query strings. Campaign rules are ordinary manual rules.
|
|
201
353
|
|
|
202
|
-
|
|
354
|
+
The plugin stores these rules only. It does not collect attribution data or integrate with analytics or advertising tools.
|
|
203
355
|
|
|
204
|
-
The
|
|
356
|
+
Register `redirects()` once. A second registration throws. The collection slug defaults to `redirects`. Change it with `overrides.slug` if that slug is taken.
|
|
205
357
|
|
|
206
|
-
|
|
207
|
-
- `to`: site-relative destination path.
|
|
208
|
-
- `statusCode`: `301` or `302`.
|
|
209
|
-
- `enabled`: whether the rule is active.
|
|
210
|
-
- `origin`: `manual` or `auto`.
|
|
358
|
+
## Serving redirects
|
|
211
359
|
|
|
212
|
-
The
|
|
360
|
+
The plugin stores rules and does not send responses. The [complete example](#complete-example) shows `src/proxy.ts`, which looks up the rule and returns the redirect with its stored status.
|
|
213
361
|
|
|
214
|
-
|
|
362
|
+
Stored rules use the plugin's normalization. The lookup in `proxy.ts` applies the same path rules to `request.nextUrl.pathname` and appends `request.nextUrl.search`, so a query-specific rule matches only the same query string:
|
|
215
363
|
|
|
216
|
-
|
|
364
|
+
| Input | Stored source |
|
|
365
|
+
| ------------------------ | ------------------------------------ |
|
|
366
|
+
| `/articles/old/` | `/articles/old` |
|
|
367
|
+
| `/articles//old` | `/articles/old` |
|
|
368
|
+
| `/a/../b` | `/b` |
|
|
369
|
+
| `/café` | `/café` (distinct from `/caf%C3%A9`) |
|
|
370
|
+
| `/articles/old?ref=mail` | `/articles/old?ref=mail` |
|
|
371
|
+
|
|
372
|
+
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.
|
|
373
|
+
|
|
374
|
+
Lookup is exact. The request path is not decoded, so a browser request for `/caf%C3%A9` matches only a rule stored as `/caf%C3%A9`, and a rule stored as `/café` never matches a browser request. Store the percent-encoded form for paths that browsers request. Query parameters are not reordered, merged, or forwarded: the destination is the stored `to`.
|
|
375
|
+
|
|
376
|
+
A pathname-only rule does not match a request that has extra query parameters. Add a second rule with the exact query string if needed. Automatic rules use paths without query strings.
|
|
377
|
+
|
|
378
|
+
## Overrides and access
|
|
379
|
+
|
|
380
|
+
`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:
|
|
217
381
|
|
|
218
382
|
```ts
|
|
383
|
+
// payload.config.ts
|
|
219
384
|
redirects({
|
|
385
|
+
localePrefixes: { en: "", fr: "/fr", de: "/de" },
|
|
220
386
|
overrides: {
|
|
221
|
-
slug: "url-
|
|
222
|
-
labels: { singular: "URL rule", plural: "URL rules" },
|
|
387
|
+
slug: "url-redirects",
|
|
223
388
|
admin: { group: "Settings" },
|
|
224
|
-
|
|
389
|
+
access: {
|
|
390
|
+
read: () => true,
|
|
391
|
+
},
|
|
392
|
+
fields: ({ defaultFields }) => [...defaultFields, { name: "editorNote", type: "textarea" }],
|
|
225
393
|
},
|
|
226
394
|
});
|
|
227
395
|
```
|
|
228
396
|
|
|
229
|
-
|
|
397
|
+
If you rename the collection, change the collection name in `proxy.ts` to match: `collection: "url-redirects"`.
|
|
230
398
|
|
|
231
|
-
|
|
399
|
+
The plugin keeps its required fields, their types, built-in validators, `from` uniqueness, and the `enabled` index. It appends its own hooks. Access follows Payload's native defaults unless you set `overrides.access`.
|
|
232
400
|
|
|
233
|
-
-
|
|
234
|
-
- The plugin compares the current published path with the latest earlier published path.
|
|
235
|
-
- A missing or unsafe path on either side skips that locale's redirect.
|
|
236
|
-
- A manual rule that owns the previous path is never overwritten.
|
|
237
|
-
- Automatic writes run through Payload Local API with the active request and transaction.
|
|
238
|
-
- Deleting a document does not create a redirect; configure removal behavior in your application if needed.
|
|
401
|
+
Automatic generation runs trusted server-side Local API operations with `overrideAccess: true`. REST, GraphQL, and user-scoped Local API requests remain subject to your access rules. The example above makes every rule readable by anyone, including through the API. Keep the default unless you want that.
|
|
239
402
|
|
|
240
|
-
|
|
403
|
+
## Reference
|
|
241
404
|
|
|
242
|
-
|
|
405
|
+
### Plugin options
|
|
243
406
|
|
|
244
|
-
|
|
407
|
+
| Option | Default | Description |
|
|
408
|
+
| ---------------- | ----------------------------------- | ------------------------------------------------------------------------------------ |
|
|
409
|
+
| `collections` | None | Central opt-in for Collections. Replaces each entity's `custom.redirect`. |
|
|
410
|
+
| `globals` | None | Central opt-in for Globals. Replaces each entity's `custom.redirect`. |
|
|
411
|
+
| `localePrefixes` | No prefixes | One prefix per Payload locale, applied to `path` and `paths`. |
|
|
412
|
+
| `overrides` | Plugin-owned `redirects` collection | Native collection options, plus `fields` for extra fields. Required fields are kept. |
|
|
245
413
|
|
|
246
|
-
|
|
247
|
-
const { docs } = await payload.find({
|
|
248
|
-
collection: "redirects",
|
|
249
|
-
where: { enabled: { equals: true } },
|
|
250
|
-
depth: 0,
|
|
251
|
-
});
|
|
414
|
+
### Rule fields
|
|
252
415
|
|
|
253
|
-
|
|
254
|
-
|
|
416
|
+
| Field | Type | Default | Description |
|
|
417
|
+
| ------------ | -------------------- | -------- | -------------------------------------------------------------- |
|
|
418
|
+
| `from` | `string` | Required | Unique normalized site-relative source path. No fragment. |
|
|
419
|
+
| `to` | `string` | Required | Normalized site-relative destination. Must differ from `from`. |
|
|
420
|
+
| `statusCode` | `"301" \| "302"` | `"301"` | Redirect status. |
|
|
421
|
+
| `enabled` | `boolean` | `true` | Disabled rules are not used for lookup or loop checks. |
|
|
422
|
+
| `origin` | `"manual" \| "auto"` | `manual` | Whether the plugin manages the rule. |
|
|
423
|
+
|
|
424
|
+
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.
|
|
425
|
+
|
|
426
|
+
### Exports
|
|
427
|
+
|
|
428
|
+
| Export | Kind | Purpose |
|
|
429
|
+
| --------------------------- | ----- | ----------------------------------------------------------- |
|
|
430
|
+
| `redirects` | Value | Returns the Payload plugin. |
|
|
431
|
+
| `RedirectsOptions` | Type | Options for `redirects()`. |
|
|
432
|
+
| `RedirectCollectionOptions` | Type | Options for a Collection, generic over the collection slug. |
|
|
433
|
+
| `RedirectGlobalOptions` | Type | Options for a Global, generic over the global slug. |
|
|
434
|
+
| `RedirectOrigin` | Type | `"manual" \| "auto"`. |
|
|
435
|
+
| `RedirectStatusCode` | Type | `301 \| 302`. |
|
|
436
|
+
|
|
437
|
+
## Compatibility
|
|
438
|
+
|
|
439
|
+
| Requirement | Support |
|
|
440
|
+
| ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
441
|
+
| Node.js | `^22.23.5`, `^24.22.3`, or `>=26.13.1`. |
|
|
442
|
+
| 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. |
|
|
443
|
+
| 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`. |
|
|
444
|
+
| Other database adapters | Not verified. No equivalent concurrency guarantee is claimed. |
|
|
445
|
+
| Automatic redirects | Require `versions.drafts` on each configured Collection or Global. |
|
|
446
|
+
| Content localization | Requires `localization.locales` and `localization.defaultLocale`. Each locale resolves without fallback. |
|
|
447
|
+
| 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. |
|
|
448
|
+
| Frontend framework | The rule store is framework-independent. The serving example targets Next.js 16. |
|
|
255
449
|
|
|
256
|
-
|
|
450
|
+
## Limitations
|
|
257
451
|
|
|
258
|
-
|
|
452
|
+
- Route changes do not migrate historical URLs. Add manual rules for older paths.
|
|
453
|
+
- Rules have no locale, domain, or host scope.
|
|
454
|
+
- Cycle detection covers enabled stored rules only.
|
|
455
|
+
- Request matching, caching, and HTTP responses are the application's responsibility. The lookup in `proxy.ts` runs on every matched request.
|
|
259
456
|
|
|
260
|
-
|
|
457
|
+
## Documentation
|
|
261
458
|
|
|
262
|
-
|
|
459
|
+
- [Failure semantics](./docs/FAILURE_SEMANTICS.md): exact behavior for each configuration and write error.
|
|
460
|
+
- [Releasing](./docs/RELEASING.md): release process for maintainers.
|
|
461
|
+
- [Payload content localization](https://payloadcms.com/docs/configuration/localization)
|
|
462
|
+
- [Payload localized publication status](https://payloadcms.com/docs/configuration/localization#status-localization)
|
|
463
|
+
- [Payload drafts and versions](https://payloadcms.com/docs/versions/drafts)
|
|
464
|
+
- [Payload Local API](https://payloadcms.com/docs/local-api/overview)
|
|
263
465
|
|
|
264
|
-
##
|
|
466
|
+
## Contributing
|
|
265
467
|
|
|
266
|
-
|
|
267
|
-
- PostgreSQL: supported adapter for transaction-scoped graph locking.
|
|
268
|
-
- Other adapters: no equivalent concurrency guarantee is claimed.
|
|
468
|
+
See [CONTRIBUTING.md](./CONTRIBUTING.md). Report security issues through [SECURITY.md](./SECURITY.md).
|
|
269
469
|
|
|
270
470
|
## License
|
|
271
471
|
|
|
272
|
-
MIT
|
|
472
|
+
[MIT](./LICENSE)
|