@klnap/payload-redirect 0.3.0 → 0.4.0
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 +355 -172
- 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 +11 -7
- package/dist/core/redirects/auto/create-redirect.js +24 -14
- package/dist/core/redirects/auto/create-redirect.js.map +1 -0
- package/dist/core/redirects/auto/match-existing-rule.d.ts +1 -1
- package/dist/core/redirects/auto/match-existing-rule.js +3 -2
- package/dist/core/redirects/auto/match-existing-rule.js.map +1 -0
- package/dist/core/redirects/auto/persist-auto-rule.d.ts +3 -3
- package/dist/core/redirects/auto/persist-auto-rule.js +17 -16
- package/dist/core/redirects/auto/persist-auto-rule.js.map +1 -0
- package/dist/core/redirects/auto/publication/get-publication-transition.d.ts +2 -2
- package/dist/core/redirects/auto/publication/get-publication-transition.js +7 -6
- package/dist/core/redirects/auto/publication/get-publication-transition.js.map +1 -0
- 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 +2 -1
- package/dist/core/redirects/auto/publication/locales.js +18 -13
- package/dist/core/redirects/auto/publication/locales.js.map +1 -0
- package/dist/core/redirects/auto/publication/publish-intent.d.ts +1 -1
- package/dist/core/redirects/auto/publication/publish-intent.js +5 -4
- package/dist/core/redirects/auto/publication/publish-intent.js.map +1 -0
- package/dist/core/redirects/auto/publication/versions/document-locale.d.ts +1 -1
- package/dist/core/redirects/auto/publication/versions/document-locale.js +1 -0
- package/dist/core/redirects/auto/publication/versions/document-locale.js.map +1 -0
- package/dist/core/redirects/auto/publication/versions/version-pages.d.ts +2 -2
- package/dist/core/redirects/auto/publication/versions/version-pages.js +3 -2
- package/dist/core/redirects/auto/publication/versions/version-pages.js.map +1 -0
- package/dist/core/redirects/auto/publication/versions/version-rows.js +5 -4
- package/dist/core/redirects/auto/publication/versions/version-rows.js.map +1 -0
- package/dist/core/redirects/auto/types.d.ts +3 -3
- package/dist/core/redirects/auto/types.js +1 -0
- package/dist/core/redirects/auto/types.js.map +1 -0
- package/dist/core/redirects/collection/collection.d.ts +2 -2
- package/dist/core/redirects/collection/collection.js +9 -11
- package/dist/core/redirects/collection/collection.js.map +1 -0
- package/dist/core/redirects/collection/duplicate-guard.d.ts +1 -1
- package/dist/core/redirects/collection/duplicate-guard.js +34 -33
- package/dist/core/redirects/collection/duplicate-guard.js.map +1 -0
- package/dist/core/redirects/collection/fields.d.ts +1 -1
- package/dist/core/redirects/collection/fields.js +51 -56
- package/dist/core/redirects/collection/fields.js.map +1 -0
- package/dist/core/redirects/collection/graph-lock.d.ts +1 -1
- package/dist/core/redirects/collection/graph-lock.js +8 -7
- package/dist/core/redirects/collection/graph-lock.js.map +1 -0
- package/dist/core/redirects/collection/origin-hook.d.ts +1 -1
- package/dist/core/redirects/collection/origin-hook.js +8 -6
- package/dist/core/redirects/collection/origin-hook.js.map +1 -0
- package/dist/core/redirects/collection/types.d.ts +2 -2
- package/dist/core/redirects/collection/types.js +1 -0
- package/dist/core/redirects/collection/types.js.map +1 -0
- package/dist/core/redirects/context-keys.d.ts +2 -2
- package/dist/core/redirects/context-keys.js +3 -2
- package/dist/core/redirects/context-keys.js.map +1 -0
- package/dist/core/redirects/model/errors.js +2 -1
- package/dist/core/redirects/model/errors.js.map +1 -0
- package/dist/core/redirects/model/origin.d.ts +1 -1
- package/dist/core/redirects/model/origin.js +4 -3
- package/dist/core/redirects/model/origin.js.map +1 -0
- package/dist/core/redirects/model/status-code.js +5 -4
- package/dist/core/redirects/model/status-code.js.map +1 -0
- package/dist/core/redirects/normalize-path.js +23 -22
- package/dist/core/redirects/normalize-path.js.map +1 -0
- package/dist/core/redirects/queries.d.ts +2 -2
- package/dist/core/redirects/queries.js +1 -0
- package/dist/core/redirects/queries.js.map +1 -0
- package/dist/core/url/published-path.js +20 -19
- package/dist/core/url/published-path.js.map +1 -0
- package/dist/core/url/route-params.js +9 -8
- package/dist/core/url/route-params.js.map +1 -0
- package/dist/exports/index.d.ts +4 -4
- package/dist/exports/index.js +2 -1
- package/dist/exports/index.js.map +1 -0
- package/dist/plugin/payload-plugin.d.ts +2 -2
- package/dist/plugin/payload-plugin.js +74 -38
- package/dist/plugin/payload-plugin.js.map +1 -0
- package/dist/plugin/types.d.ts +10 -5
- package/dist/plugin/types.js +1 -0
- package/dist/plugin/types.js.map +1 -0
- package/docs/FAILURE_SEMANTICS.md +50 -31
- package/package.json +21 -13
- package/dist/core/redirects/collection/access.d.ts +0 -8
- package/dist/core/redirects/collection/access.js +0 -9
package/README.md
CHANGED
|
@@ -1,271 +1,454 @@
|
|
|
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
|
-
## Manual redirects
|
|
22
|
-
|
|
23
|
-
Register the plugin with no options:
|
|
24
|
-
|
|
25
|
-
```ts
|
|
26
|
-
import { buildConfig } from 'payload'
|
|
27
|
-
import { postgresAdapter } from '@payloadcms/db-postgres'
|
|
28
|
-
import { redirects } from '@klnap/payload-redirect'
|
|
29
|
-
|
|
30
|
-
export default buildConfig({
|
|
31
|
-
secret: process.env.PAYLOAD_SECRET!,
|
|
32
|
-
db: postgresAdapter({ pool: { connectionString: process.env.DATABASE_URI! } }),
|
|
33
|
-
plugins: [redirects()],
|
|
34
|
-
})
|
|
45
|
+
```bash
|
|
46
|
+
npm install @klnap/payload-redirect
|
|
35
47
|
```
|
|
36
48
|
|
|
37
|
-
|
|
49
|
+
`payload` is a required peer. `@payloadcms/db-postgres` is an optional peer, needed only with PostgreSQL.
|
|
50
|
+
|
|
51
|
+
## Complete example
|
|
38
52
|
|
|
39
|
-
|
|
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.
|
|
40
54
|
|
|
41
|
-
|
|
55
|
+
### 1. `src/payload.config.ts`
|
|
42
56
|
|
|
43
57
|
```ts
|
|
44
|
-
import {
|
|
45
|
-
import {
|
|
46
|
-
import {
|
|
58
|
+
import { postgresAdapter } from "@payloadcms/db-postgres";
|
|
59
|
+
import { lexicalEditor } from "@payloadcms/richtext-lexical";
|
|
60
|
+
import { buildConfig } from "payload";
|
|
61
|
+
import { redirects } from "@klnap/payload-redirect";
|
|
62
|
+
import { Articles } from "./collections/Articles";
|
|
63
|
+
import { About } from "./globals/About";
|
|
47
64
|
|
|
48
65
|
export default buildConfig({
|
|
49
|
-
secret: process.env.PAYLOAD_SECRET
|
|
50
|
-
|
|
66
|
+
secret: process.env.PAYLOAD_SECRET ?? "",
|
|
67
|
+
editor: lexicalEditor(),
|
|
68
|
+
db: postgresAdapter({
|
|
69
|
+
pool: { connectionString: process.env.DATABASE_URI },
|
|
70
|
+
}),
|
|
51
71
|
localization: {
|
|
52
|
-
locales: [
|
|
53
|
-
defaultLocale:
|
|
72
|
+
locales: ["en", "fr", "de"],
|
|
73
|
+
defaultLocale: "en",
|
|
54
74
|
},
|
|
55
|
-
collections: [
|
|
56
|
-
|
|
57
|
-
slug: 'articles',
|
|
58
|
-
versions: { drafts: true },
|
|
59
|
-
fields: [
|
|
60
|
-
{ name: 'title', type: 'text', required: true },
|
|
61
|
-
{ name: 'slug', type: 'text', required: true, localized: true },
|
|
62
|
-
],
|
|
63
|
-
},
|
|
64
|
-
{
|
|
65
|
-
slug: 'projects',
|
|
66
|
-
versions: { drafts: true },
|
|
67
|
-
fields: [{ name: 'slug', type: 'text', required: true, localized: true }],
|
|
68
|
-
},
|
|
69
|
-
],
|
|
75
|
+
collections: [Articles],
|
|
76
|
+
globals: [About],
|
|
70
77
|
plugins: [
|
|
71
78
|
redirects({
|
|
72
|
-
localePrefixes: {
|
|
73
|
-
pl: '',
|
|
74
|
-
en: '/en',
|
|
75
|
-
de: '/de',
|
|
76
|
-
},
|
|
77
|
-
collections: {
|
|
78
|
-
articles: { path: '/[slug]' },
|
|
79
|
-
projects: {
|
|
80
|
-
paths: {
|
|
81
|
-
pl: '/realizacje/[slug]',
|
|
82
|
-
en: '/work/[slug]',
|
|
83
|
-
de: '/projekte/[slug]',
|
|
84
|
-
},
|
|
85
|
-
},
|
|
86
|
-
},
|
|
79
|
+
localePrefixes: { en: "", fr: "/fr", de: "/de" },
|
|
87
80
|
}),
|
|
88
81
|
],
|
|
89
|
-
})
|
|
82
|
+
});
|
|
90
83
|
```
|
|
91
84
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
> When the published path of a document in this collection changes, redirect the previous path to the new one.
|
|
85
|
+
`redirects()` is called once. It adds the `redirects` collection, the publication hooks, and the locale prefixes.
|
|
95
86
|
|
|
96
|
-
|
|
97
|
-
- Projects use a separate public path template for each Payload locale.
|
|
98
|
-
- Collections without redirect configuration remain unaffected.
|
|
87
|
+
### 2. `src/collections/Articles.ts`
|
|
99
88
|
|
|
100
|
-
|
|
89
|
+
```ts
|
|
90
|
+
import type { CollectionConfig } from "payload";
|
|
91
|
+
import type { RedirectCollectionOptions } from "@klnap/payload-redirect";
|
|
101
92
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
+
};
|
|
106
112
|
```
|
|
107
113
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
### Collection-level configuration
|
|
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.
|
|
111
115
|
|
|
112
|
-
|
|
116
|
+
### 3. `src/globals/About.ts`
|
|
113
117
|
|
|
114
118
|
```ts
|
|
115
|
-
import type {
|
|
116
|
-
import type {
|
|
117
|
-
|
|
118
|
-
const projectRedirect = {
|
|
119
|
-
paths: {
|
|
120
|
-
pl: '/realizacje/[slug]',
|
|
121
|
-
en: '/work/[slug]',
|
|
122
|
-
de: '/projekte/[slug]',
|
|
123
|
-
},
|
|
124
|
-
} satisfies RedirectCollectionOptions<'projects'>
|
|
119
|
+
import type { GlobalConfig } from "payload";
|
|
120
|
+
import type { RedirectGlobalOptions } from "@klnap/payload-redirect";
|
|
125
121
|
|
|
126
|
-
export const
|
|
127
|
-
slug:
|
|
122
|
+
export const About: GlobalConfig = {
|
|
123
|
+
slug: "about",
|
|
128
124
|
versions: { drafts: true },
|
|
125
|
+
fields: [{ name: "slug", type: "text", required: true, localized: true }],
|
|
129
126
|
custom: {
|
|
130
|
-
redirect:
|
|
127
|
+
redirect: {
|
|
128
|
+
getPath: ({ doc, locale }) => `${locale === "fr" ? "/fr" : ""}/${doc.slug}`,
|
|
129
|
+
} satisfies RedirectGlobalOptions<"about">,
|
|
131
130
|
},
|
|
132
|
-
|
|
131
|
+
};
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
`getPath` returns the complete path. The plugin does not add prefixes to it, so the French prefix is added here.
|
|
135
|
+
|
|
136
|
+
### 4. `src/proxy.ts`
|
|
137
|
+
|
|
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.
|
|
139
|
+
|
|
140
|
+
```ts
|
|
141
|
+
import { NextResponse, type NextRequest } from "next/server";
|
|
142
|
+
import { getPayload } from "payload";
|
|
143
|
+
import payloadConfig from "@payload-config";
|
|
144
|
+
|
|
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;
|
|
133
148
|
}
|
|
149
|
+
|
|
150
|
+
function decodeSafely(value: string): string {
|
|
151
|
+
try {
|
|
152
|
+
return decodeURI(value);
|
|
153
|
+
} catch {
|
|
154
|
+
return value;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export async function proxy(request: NextRequest) {
|
|
159
|
+
const pathname = normalizeRequestPath(request.nextUrl.pathname);
|
|
160
|
+
const candidates = [...new Set([pathname, decodeSafely(pathname)])];
|
|
161
|
+
|
|
162
|
+
const payload = await getPayload({ config: payloadConfig });
|
|
163
|
+
const { docs } = await payload.find({
|
|
164
|
+
collection: "redirects",
|
|
165
|
+
where: {
|
|
166
|
+
and: [{ from: { in: candidates } }, { enabled: { equals: true } }],
|
|
167
|
+
},
|
|
168
|
+
limit: 1,
|
|
169
|
+
pagination: false,
|
|
170
|
+
depth: 0,
|
|
171
|
+
overrideAccess: true,
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
const rule = docs[0];
|
|
175
|
+
if (!rule) return NextResponse.next();
|
|
176
|
+
|
|
177
|
+
return NextResponse.redirect(new URL(rule.to, request.url), Number(rule.statusCode) as 301 | 302);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export const config = {
|
|
181
|
+
matcher: ["/((?!api|admin|_next/static|_next/image|favicon.ico).*)"],
|
|
182
|
+
};
|
|
134
183
|
```
|
|
135
184
|
|
|
136
|
-
|
|
185
|
+
`@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.
|
|
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.
|
|
137
190
|
|
|
138
|
-
|
|
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. |
|
|
139
202
|
|
|
140
|
-
|
|
203
|
+
Route changes do not migrate historical URLs. Add manual rules for older paths if you need them.
|
|
141
204
|
|
|
142
|
-
|
|
205
|
+
## Configuration
|
|
143
206
|
|
|
144
|
-
|
|
207
|
+
You can configure automatic redirects in two places. Use `custom.redirect` by default.
|
|
145
208
|
|
|
146
|
-
|
|
209
|
+
### `custom.redirect` on the entity
|
|
210
|
+
|
|
211
|
+
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.
|
|
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:
|
|
147
225
|
|
|
148
226
|
```ts
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
227
|
+
// src/collections/Articles.ts (custom only)
|
|
228
|
+
custom: {
|
|
229
|
+
redirect: {
|
|
230
|
+
path: "/articles/[category]/[slug]",
|
|
231
|
+
} satisfies RedirectCollectionOptions<"articles">,
|
|
232
|
+
},
|
|
152
233
|
```
|
|
153
234
|
|
|
154
|
-
|
|
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:
|
|
155
238
|
|
|
156
239
|
```ts
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
}
|
|
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
|
+
};
|
|
160
268
|
```
|
|
161
269
|
|
|
162
|
-
|
|
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.
|
|
163
271
|
|
|
164
|
-
###
|
|
272
|
+
### Central configuration: `redirects()`
|
|
165
273
|
|
|
166
|
-
|
|
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:
|
|
167
275
|
|
|
168
276
|
```ts
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
de: '/projekte/[slug]',
|
|
277
|
+
redirects({
|
|
278
|
+
localePrefixes: { en: "", fr: "/fr", de: "/de" },
|
|
279
|
+
collections: {
|
|
280
|
+
articles: { path: "/articles/[category]/[slug]" },
|
|
174
281
|
},
|
|
175
|
-
|
|
282
|
+
globals: {
|
|
283
|
+
about: { path: "/[slug]" },
|
|
284
|
+
},
|
|
285
|
+
});
|
|
176
286
|
```
|
|
177
287
|
|
|
178
|
-
|
|
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
|
|
179
291
|
|
|
180
|
-
|
|
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`.
|
|
181
293
|
|
|
182
|
-
|
|
294
|
+
With those templates, the same article resolves to:
|
|
183
295
|
|
|
184
|
-
|
|
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` |
|
|
185
301
|
|
|
186
|
-
|
|
302
|
+
Publishing a new French slug `introduction-a-payload` creates only this redirect:
|
|
187
303
|
|
|
188
|
-
|
|
304
|
+
```text
|
|
305
|
+
/fr/actualites/premiers-pas-avec-payload
|
|
306
|
+
→ /fr/actualites/introduction-a-payload (301)
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
Rules:
|
|
310
|
+
|
|
311
|
+
- `path` shares one template across locales. `paths` gives one per locale.
|
|
312
|
+
- `localePrefixes` prefixes either template strategy. It must list every Payload locale.
|
|
313
|
+
- `paths` and `localePrefixes` must match `localization.locales` exactly.
|
|
314
|
+
- Without localization, use `path` or `getPath` and omit `localePrefixes`.
|
|
315
|
+
- 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.
|
|
316
|
+
- Redirect rules have no locale or domain scope. Localized routes need distinct source paths when they need different destinations.
|
|
317
|
+
- Payload content localization translates article data. Payload `i18n` translates the Admin UI. Plugin labels and validation messages are English.
|
|
318
|
+
|
|
319
|
+
## Globals
|
|
320
|
+
|
|
321
|
+
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:
|
|
189
322
|
|
|
190
323
|
```ts
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
}
|
|
324
|
+
redirects({
|
|
325
|
+
globals: {
|
|
326
|
+
about: { path: "/[slug]" },
|
|
327
|
+
},
|
|
328
|
+
});
|
|
197
329
|
```
|
|
198
330
|
|
|
199
|
-
|
|
331
|
+
## Manual redirects
|
|
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.
|
|
200
341
|
|
|
201
|
-
##
|
|
342
|
+
## Serving redirects
|
|
202
343
|
|
|
203
|
-
The plugin
|
|
344
|
+
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.
|
|
204
345
|
|
|
205
|
-
|
|
206
|
-
- `to`: site-relative destination path.
|
|
207
|
-
- `statusCode`: `301` or `302`.
|
|
208
|
-
- `enabled`: whether the rule is active.
|
|
209
|
-
- `origin`: `manual` or `auto`.
|
|
346
|
+
Stored rules use the plugin's normalization, and `URL.pathname` alone does not match it. The lookup in `proxy.ts` applies the same rules to the request path:
|
|
210
347
|
|
|
211
|
-
|
|
348
|
+
| Input | Stored source |
|
|
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` |
|
|
212
355
|
|
|
213
|
-
|
|
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.
|
|
214
357
|
|
|
215
|
-
|
|
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:
|
|
216
363
|
|
|
217
364
|
```ts
|
|
365
|
+
// payload.config.ts
|
|
218
366
|
redirects({
|
|
367
|
+
localePrefixes: { en: "", fr: "/fr", de: "/de" },
|
|
219
368
|
overrides: {
|
|
220
|
-
slug:
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
369
|
+
slug: "url-redirects",
|
|
370
|
+
admin: { group: "Settings" },
|
|
371
|
+
access: {
|
|
372
|
+
read: () => true,
|
|
373
|
+
},
|
|
374
|
+
fields: ({ defaultFields }) => [...defaultFields, { name: "editorNote", type: "textarea" }],
|
|
224
375
|
},
|
|
225
|
-
})
|
|
376
|
+
});
|
|
226
377
|
```
|
|
227
378
|
|
|
228
|
-
|
|
379
|
+
If you rename the collection, change the collection name in `proxy.ts` to match: `collection: "url-redirects"`.
|
|
229
380
|
|
|
230
|
-
|
|
381
|
+
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`.
|
|
231
382
|
|
|
232
|
-
-
|
|
233
|
-
- The plugin compares the current published path with the latest earlier published path.
|
|
234
|
-
- A missing or unsafe path on either side skips that locale's redirect.
|
|
235
|
-
- A manual rule that owns the previous path is never overwritten.
|
|
236
|
-
- Automatic writes run through Payload Local API with the active request and transaction.
|
|
237
|
-
- Deleting a document does not create a redirect; configure removal behavior in your application if needed.
|
|
383
|
+
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.
|
|
238
384
|
|
|
239
|
-
|
|
385
|
+
## Reference
|
|
240
386
|
|
|
241
|
-
|
|
387
|
+
### Plugin options
|
|
242
388
|
|
|
243
|
-
|
|
389
|
+
| Option | Default | Description |
|
|
390
|
+
| ---------------- | ----------------------------------- | ------------------------------------------------------------------------------------ |
|
|
391
|
+
| `collections` | None | Central opt-in for Collections. Replaces each entity's `custom.redirect`. |
|
|
392
|
+
| `globals` | None | Central opt-in for Globals. Replaces each entity's `custom.redirect`. |
|
|
393
|
+
| `localePrefixes` | No prefixes | One prefix per Payload locale, applied to `path` and `paths`. |
|
|
394
|
+
| `overrides` | Plugin-owned `redirects` collection | Native collection options, plus `fields` for extra fields. Required fields are kept. |
|
|
244
395
|
|
|
245
|
-
|
|
246
|
-
const { docs } = await payload.find({
|
|
247
|
-
collection: 'redirects',
|
|
248
|
-
where: { enabled: { equals: true } },
|
|
249
|
-
depth: 0,
|
|
250
|
-
})
|
|
396
|
+
### Rule fields
|
|
251
397
|
|
|
252
|
-
|
|
253
|
-
|
|
398
|
+
| Field | Type | Default | Description |
|
|
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. |
|
|
254
431
|
|
|
255
|
-
|
|
432
|
+
## Limitations
|
|
256
433
|
|
|
257
|
-
|
|
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.
|
|
258
438
|
|
|
259
|
-
|
|
439
|
+
## Documentation
|
|
260
440
|
|
|
261
|
-
|
|
441
|
+
- [Failure semantics](./docs/FAILURE_SEMANTICS.md): exact behavior for each configuration and write error.
|
|
442
|
+
- [Releasing](./docs/RELEASING.md): release process for maintainers.
|
|
443
|
+
- [Payload content localization](https://payloadcms.com/docs/configuration/localization)
|
|
444
|
+
- [Payload localized publication status](https://payloadcms.com/docs/configuration/localization#status-localization)
|
|
445
|
+
- [Payload drafts and versions](https://payloadcms.com/docs/versions/drafts)
|
|
446
|
+
- [Payload Local API](https://payloadcms.com/docs/local-api/overview)
|
|
262
447
|
|
|
263
|
-
##
|
|
448
|
+
## Contributing
|
|
264
449
|
|
|
265
|
-
|
|
266
|
-
- PostgreSQL: supported adapter for transaction-scoped graph locking.
|
|
267
|
-
- Other adapters: no equivalent concurrency guarantee is claimed.
|
|
450
|
+
See [CONTRIBUTING.md](./CONTRIBUTING.md). Report security issues through [SECURITY.md](./SECURITY.md).
|
|
268
451
|
|
|
269
452
|
## License
|
|
270
453
|
|
|
271
|
-
MIT
|
|
454
|
+
[MIT](./LICENSE)
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { DataFromGlobalSlug, GlobalAfterChangeHook, GlobalSlug } from "payload";
|
|
2
|
+
import type { LocalePrefixes } from "../../url/published-path.js";
|
|
3
|
+
import type { RedirectPathOptions } from "./types.js";
|
|
4
|
+
type CreateGlobalRedirectOptions<TSlug extends GlobalSlug = GlobalSlug> = {
|
|
5
|
+
globalSlug: TSlug;
|
|
6
|
+
redirectCollectionSlug: string;
|
|
7
|
+
pathOptions: RedirectPathOptions<DataFromGlobalSlug<TSlug>>;
|
|
8
|
+
localePrefixes?: LocalePrefixes;
|
|
9
|
+
};
|
|
10
|
+
export declare const createGlobalRedirect: <TSlug extends GlobalSlug>({ globalSlug, redirectCollectionSlug, pathOptions, localePrefixes, }: CreateGlobalRedirectOptions<TSlug>) => GlobalAfterChangeHook;
|
|
11
|
+
export {};
|