Skip to content

Commit 5dfcffa

Browse files
authored
feat(plugin-redirects): added new option for redirect type in the redirects collection (#7625)
You can now add a redirect type to your redirects if needed: ```ts // Supported types redirectTypes: ['301', '302'], // Override the select field redirectTypeFieldOverride: { label: 'Redirect Type (Overridden)', }, ```
1 parent fa3d250 commit 5dfcffa

File tree

6 files changed

+60
-3
lines changed

6 files changed

+60
-3
lines changed

packages/plugin-redirects/src/index.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1-
import type { CollectionConfig, Config, Field } from 'payload'
1+
import type { CollectionConfig, Config, Field, SelectField } from 'payload'
22

33
import type { RedirectsPluginConfig } from './types.js'
44

5+
import { redirectOptions } from './redirectTypes.js'
6+
7+
export { redirectOptions, redirectTypes } from './redirectTypes.js'
58
export const redirectsPlugin =
69
(pluginConfig: RedirectsPluginConfig) =>
710
(incomingConfig: Config): Config => {
11+
const redirectSelectField: SelectField = {
12+
name: 'type',
13+
type: 'select',
14+
label: 'Redirect Type',
15+
options: redirectOptions.filter((option) =>
16+
pluginConfig?.redirectTypes?.includes(option.value),
17+
),
18+
required: true,
19+
...(pluginConfig?.redirectTypeFieldOverride || {}),
20+
}
21+
822
const defaultFields: Field[] = [
923
{
1024
name: 'from',
@@ -58,6 +72,7 @@ export const redirectsPlugin =
5872
],
5973
label: false,
6074
},
75+
...(pluginConfig?.redirectTypes ? [redirectSelectField] : []),
6176
]
6277

6378
const redirectsCollection: CollectionConfig = {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export const redirectTypes = ['301', '302', '303', '307', '308'] as const
2+
3+
export const redirectOptions: { label: string; value: (typeof redirectTypes)[number] }[] = [
4+
{
5+
label: '301 - Permanent',
6+
value: '301',
7+
},
8+
{
9+
label: '302 - Temporary',
10+
value: '302',
11+
},
12+
{
13+
label: '303 - See Other',
14+
value: '303',
15+
},
16+
{
17+
label: '307 - Temporary Redirect',
18+
value: '307',
19+
},
20+
{
21+
label: '308 - Permanent Redirect',
22+
value: '308',
23+
},
24+
]
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import type { CollectionConfig, Field } from 'payload'
1+
import type { CollectionConfig, Field, SelectField } from 'payload'
22

3+
import type { redirectTypes } from './redirectTypes.js'
34
export type FieldsOverride = (args: { defaultFields: Field[] }) => Field[]
45

56
export type RedirectsPluginConfig = {
67
collections?: string[]
78
overrides?: { fields?: FieldsOverride } & Partial<Omit<CollectionConfig, 'fields'>>
9+
redirectTypeFieldOverride?: Partial<SelectField>
10+
redirectTypes?: (typeof redirectTypes)[number][]
811
}

test/plugin-redirects/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ export default buildConfigWithDefaults({
4242
]
4343
},
4444
},
45+
redirectTypes: ['301', '302'],
46+
redirectTypeFieldOverride: {
47+
label: 'Redirect Type (Overridden)',
48+
},
4549
}),
4650
],
4751
typescript: {

test/plugin-redirects/int.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ describe('@payloadcms/plugin-redirects', () => {
4949
value: page.id,
5050
},
5151
},
52+
type: '301',
5253
},
5354
})
5455

@@ -66,6 +67,7 @@ describe('@payloadcms/plugin-redirects', () => {
6667
type: 'custom',
6768
url: '/test',
6869
},
70+
type: '301',
6971
},
7072
})
7173

test/plugin-redirects/payload-types.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ export interface Config {
1717
'payload-preferences': PayloadPreference;
1818
'payload-migrations': PayloadMigration;
1919
};
20+
db: {
21+
defaultIDType: string;
22+
};
2023
globals: {};
2124
locale: 'en' | 'es' | 'de';
2225
user: User & {
@@ -26,15 +29,20 @@ export interface Config {
2629
export interface UserAuthOperations {
2730
forgotPassword: {
2831
email: string;
32+
password: string;
2933
};
3034
login: {
31-
password: string;
3235
email: string;
36+
password: string;
3337
};
3438
registerFirstUser: {
3539
email: string;
3640
password: string;
3741
};
42+
unlock: {
43+
email: string;
44+
password: string;
45+
};
3846
}
3947
/**
4048
* This interface was referenced by `Config`'s JSON-Schema
@@ -80,6 +88,7 @@ export interface Redirect {
8088
} | null;
8189
url?: string | null;
8290
};
91+
type: '301' | '302';
8392
customField?: string | null;
8493
updatedAt: string;
8594
createdAt: string;

0 commit comments

Comments
 (0)