@easy-cms/plugin-redirects 0.48.0 → 0.49.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/dist/index.d.ts +11 -7
- package/dist/index.js +21 -14
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -26,13 +26,15 @@ interface RedirectsPluginOptions {
|
|
|
26
26
|
*/
|
|
27
27
|
readonly autoRedirect?: boolean | readonly string[];
|
|
28
28
|
/** Slug of the redirects collection. Default `redirects`. */
|
|
29
|
-
readonly
|
|
29
|
+
readonly slugs?: {
|
|
30
|
+
readonly redirects?: string;
|
|
31
|
+
};
|
|
30
32
|
/**
|
|
31
|
-
* How long a server keeps the redirects in memory, in
|
|
32
|
-
* server that made them; other servers (serverless instances) catch up within this time.
|
|
33
|
-
* Default
|
|
33
|
+
* How long a server keeps the redirects in memory, in seconds. Changes clear it at once on
|
|
34
|
+
* the server that made them; other servers (serverless instances) catch up within this time.
|
|
35
|
+
* Default 60.
|
|
34
36
|
*/
|
|
35
|
-
readonly
|
|
37
|
+
readonly cacheMaxAge?: number;
|
|
36
38
|
}
|
|
37
39
|
/**
|
|
38
40
|
* A `redirects` collection under Settings in the admin, `resolveRedirect()` for your app's
|
|
@@ -81,7 +83,9 @@ type RedirectsPluginTypes<R extends string, S extends string> = {
|
|
|
81
83
|
};
|
|
82
84
|
declare function redirectsPlugin<const S extends string = never, const R extends string = 'redirects'>(options?: RedirectsPluginOptions & {
|
|
83
85
|
readonly collections?: readonly S[];
|
|
84
|
-
readonly
|
|
86
|
+
readonly slugs?: {
|
|
87
|
+
readonly redirects?: R;
|
|
88
|
+
};
|
|
85
89
|
}): TypedPlugin<RedirectsPluginTypes<R, S>>;
|
|
86
90
|
|
|
87
91
|
type Doc = Record<string, unknown>;
|
|
@@ -122,7 +126,7 @@ interface ResolveRedirectOptions {
|
|
|
122
126
|
declare function clearRedirects(cms: object): void;
|
|
123
127
|
/**
|
|
124
128
|
* The redirect for a request's path, or `null`. Pass the request's URL to keep its query
|
|
125
|
-
* string. Redirects are read once and kept in memory (see `
|
|
129
|
+
* string. Redirects are read once and kept in memory (see `cacheMaxAge`), so calling this on every
|
|
126
130
|
* request is cheap.
|
|
127
131
|
*
|
|
128
132
|
* ```ts
|
package/dist/index.js
CHANGED
|
@@ -2,11 +2,14 @@
|
|
|
2
2
|
import {
|
|
3
3
|
definePlugin
|
|
4
4
|
} from "@easy-cms/core";
|
|
5
|
+
import { checkRenamedOptions } from "@easy-cms/core/internal";
|
|
5
6
|
|
|
6
7
|
// src/info.ts
|
|
7
8
|
var INFO = {
|
|
8
9
|
name: "@easy-cms/plugin-redirects",
|
|
9
|
-
|
|
10
|
+
// The plugin API it is written for (`PLUGIN_API_VERSION` of @easy-cms/core).
|
|
11
|
+
apiVersion: 1,
|
|
12
|
+
...true ? { version: "0.49.0" } : {}
|
|
10
13
|
};
|
|
11
14
|
|
|
12
15
|
// src/shared.ts
|
|
@@ -63,7 +66,7 @@ async function redirects(cms, options) {
|
|
|
63
66
|
holder[CACHE] ??= /* @__PURE__ */ new Map();
|
|
64
67
|
const caches = holder[CACHE];
|
|
65
68
|
const cached = caches.get(key);
|
|
66
|
-
if (cached && Date.now() - cached.at < source.
|
|
69
|
+
if (cached && Date.now() - cached.at < source.cacheMs) return cached.map;
|
|
67
70
|
const map = load(cms, source, scope);
|
|
68
71
|
caches.set(key, { at: Date.now(), map });
|
|
69
72
|
map.catch(() => {
|
|
@@ -117,8 +120,12 @@ async function target(cms, source, doc) {
|
|
|
117
120
|
|
|
118
121
|
// src/plugin.ts
|
|
119
122
|
function redirectsPlugin(options = {}) {
|
|
123
|
+
checkRenamedOptions("redirectsPlugin", options, {
|
|
124
|
+
slug: "slugs.redirects",
|
|
125
|
+
cacheTTL: "cacheMaxAge (in seconds)"
|
|
126
|
+
});
|
|
120
127
|
return definePlugin((config) => {
|
|
121
|
-
const slug = options.
|
|
128
|
+
const slug = options.slugs?.redirects ?? "redirects";
|
|
122
129
|
const collections = options.collections ?? [];
|
|
123
130
|
const missing = collections.filter((s) => !config.collections?.some((c) => c.slug === s));
|
|
124
131
|
if (missing.length > 0)
|
|
@@ -130,8 +137,10 @@ function redirectsPlugin(options = {}) {
|
|
|
130
137
|
"redirectsPlugin: set `url` to give the address of documents in `collections`"
|
|
131
138
|
);
|
|
132
139
|
if (config.collections?.some((c) => c.slug === slug))
|
|
133
|
-
throw new Error(
|
|
134
|
-
|
|
140
|
+
throw new Error(
|
|
141
|
+
`redirectsPlugin: there is already a collection "${slug}"; set \`slugs.redirects\``
|
|
142
|
+
);
|
|
143
|
+
const locales = (config.localization || void 0)?.locales ?? [];
|
|
135
144
|
const auto = options.autoRedirect === false ? [] : options.autoRedirect === true || options.autoRedirect === void 0 ? collections : options.autoRedirect.filter((s) => collections.includes(s));
|
|
136
145
|
const urlOf = async (cms, collection, id, locale) => {
|
|
137
146
|
const doc = await cms.findById(collection, id, {
|
|
@@ -147,7 +156,7 @@ function redirectsPlugin(options = {}) {
|
|
|
147
156
|
slug,
|
|
148
157
|
collections,
|
|
149
158
|
url: options.url,
|
|
150
|
-
|
|
159
|
+
cacheMs: (options.cacheMaxAge ?? 60) * 1e3
|
|
151
160
|
};
|
|
152
161
|
const targets = collections.map((collection) => {
|
|
153
162
|
const label = config.collections?.find((c) => c.slug === collection)?.labels?.singular;
|
|
@@ -199,7 +208,7 @@ function redirectsPlugin(options = {}) {
|
|
|
199
208
|
defaultValue: "301",
|
|
200
209
|
required: true,
|
|
201
210
|
label: { en: "Type", th: "\u0E0A\u0E19\u0E34\u0E14" },
|
|
202
|
-
position: "sidebar"
|
|
211
|
+
admin: { position: "sidebar" }
|
|
203
212
|
}
|
|
204
213
|
];
|
|
205
214
|
const clear = ({ cms }) => clearRedirects(cms);
|
|
@@ -209,10 +218,8 @@ function redirectsPlugin(options = {}) {
|
|
|
209
218
|
singular: { en: "Redirect", th: "\u0E01\u0E32\u0E23\u0E40\u0E1B\u0E25\u0E35\u0E48\u0E22\u0E19\u0E40\u0E2A\u0E49\u0E19\u0E17\u0E32\u0E07" },
|
|
210
219
|
plural: { en: "Redirects", th: "\u0E01\u0E32\u0E23\u0E40\u0E1B\u0E25\u0E35\u0E48\u0E22\u0E19\u0E40\u0E2A\u0E49\u0E19\u0E17\u0E32\u0E07" }
|
|
211
220
|
},
|
|
212
|
-
icon: "link",
|
|
213
221
|
useAsTitle: "from",
|
|
214
|
-
editIn: "drawer",
|
|
215
|
-
admin: { group: "settings" },
|
|
222
|
+
admin: { editIn: "drawer", icon: "link", group: "settings" },
|
|
216
223
|
fields: redirectFields,
|
|
217
224
|
hooks: {
|
|
218
225
|
beforeChange: [
|
|
@@ -223,11 +230,11 @@ function redirectsPlugin(options = {}) {
|
|
|
223
230
|
}
|
|
224
231
|
};
|
|
225
232
|
const before = /* @__PURE__ */ new Map();
|
|
226
|
-
const remember = async ({ data, operation,
|
|
227
|
-
if (operation === "update" &&
|
|
233
|
+
const remember = async ({ data, operation, previousDoc, cms, slug: s }) => {
|
|
234
|
+
if (operation === "update" && previousDoc?.id !== void 0)
|
|
228
235
|
before.set(
|
|
229
|
-
`${s}:${
|
|
230
|
-
await Promise.all(localeList.map((l) => urlOf(cms, s,
|
|
236
|
+
`${s}:${previousDoc.id}`,
|
|
237
|
+
await Promise.all(localeList.map((l) => urlOf(cms, s, previousDoc.id, l)))
|
|
231
238
|
);
|
|
232
239
|
return data;
|
|
233
240
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@easy-cms/plugin-redirects",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.49.0",
|
|
4
4
|
"description": "Redirects for Easy CMS: 301s managed in the admin, automatic redirects when a page's address changes",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -16,11 +16,11 @@
|
|
|
16
16
|
"dist"
|
|
17
17
|
],
|
|
18
18
|
"peerDependencies": {
|
|
19
|
-
"@easy-cms/core": "^0.
|
|
19
|
+
"@easy-cms/core": "^0.49.0"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
|
-
"@easy-cms/core": "^0.
|
|
23
|
-
"@easy-cms/db-sqlite": "^0.
|
|
22
|
+
"@easy-cms/core": "^0.49.0",
|
|
23
|
+
"@easy-cms/db-sqlite": "^0.49.0"
|
|
24
24
|
},
|
|
25
25
|
"engines": {
|
|
26
26
|
"node": ">=22.12"
|