@websolutespa/payload-plugin-seo 0.1.3 → 2.0.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.
Files changed (3) hide show
  1. package/CHANGELOG.md +67 -55
  2. package/dist/index.js +95 -21
  3. package/package.json +7 -7
package/CHANGELOG.md CHANGED
@@ -1,55 +1,67 @@
1
- # @websolutespa/payload-plugin-seo
2
-
3
- ## 0.1.3
4
-
5
- ### Patch Changes
6
-
7
- - - Add rules field localization
8
-
9
- ## 0.1.2
10
-
11
- ### Patch Changes
12
-
13
- - 0b4e928: Modified: translations
14
- - Updated dependencies [0b4e928]
15
- - Updated dependencies [e921c43]
16
- - Updated dependencies [e921c43]
17
- - @websolutespa/payload-utils@0.0.3
18
-
19
- ## 0.1.1
20
-
21
- ### Patch Changes
22
-
23
- - 15ad595: Modified: payload local api consolidated overrideAccess
24
-
25
- ## 0.1.0
26
-
27
- ### Minor Changes
28
-
29
- - 8475b68: Added: external dependency payload-utils
30
- - 946a81f: Updating: dependency payload 2.28.0
31
-
32
- ### Patch Changes
33
-
34
- - Updated dependencies [8475b68]
35
- - @websolutespa/payload-utils@0.0.1
36
-
37
- ## 0.0.3
38
-
39
- ### Patch Changes
40
-
41
- - Fixing: utility functions exports
42
-
43
- ## 0.0.2
44
-
45
- ### Patch Changes
46
-
47
- - 882a9be: add token autocomplete
48
- add readme
49
- - 8696866: add new tests
50
-
51
- ## 0.0.1
52
-
53
- ### Patch Changes
54
-
55
- - 06a3127: Initial release
1
+ # @websolutespa/payload-plugin-seo
2
+
3
+ ## 2.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - Release: Bom 2.0
8
+
9
+ ## 0.1.4
10
+
11
+ ### Patch Changes
12
+
13
+ - - Improved rules evaluation performance
14
+
15
+ ## 0.1.3
16
+
17
+ ### Patch Changes
18
+
19
+ - - Add rules field localization
20
+
21
+ ## 0.1.2
22
+
23
+ ### Patch Changes
24
+
25
+ - 0b4e928: Modified: translations
26
+ - Updated dependencies [0b4e928]
27
+ - Updated dependencies [e921c43]
28
+ - Updated dependencies [e921c43]
29
+ - @websolutespa/payload-utils@0.0.3
30
+
31
+ ## 0.1.1
32
+
33
+ ### Patch Changes
34
+
35
+ - 15ad595: Modified: payload local api consolidated overrideAccess
36
+
37
+ ## 0.1.0
38
+
39
+ ### Minor Changes
40
+
41
+ - 8475b68: Added: external dependency payload-utils
42
+ - 946a81f: Updating: dependency payload 2.28.0
43
+
44
+ ### Patch Changes
45
+
46
+ - Updated dependencies [8475b68]
47
+ - @websolutespa/payload-utils@0.0.1
48
+
49
+ ## 0.0.3
50
+
51
+ ### Patch Changes
52
+
53
+ - Fixing: utility functions exports
54
+
55
+ ## 0.0.2
56
+
57
+ ### Patch Changes
58
+
59
+ - 882a9be: add token autocomplete
60
+ add readme
61
+ - 8696866: add new tests
62
+
63
+ ## 0.0.1
64
+
65
+ ### Patch Changes
66
+
67
+ - 06a3127: Initial release
package/dist/index.js CHANGED
@@ -311,6 +311,33 @@ var MetatagsRule = {
311
311
  ]
312
312
  };
313
313
 
314
+ // src/utils/cache.ts
315
+ var CACHE_ = /* @__PURE__ */ new Map();
316
+ async function fromCache(callback, key, duration = 5 * 60) {
317
+ const now = (/* @__PURE__ */ new Date()).getTime() / 1e3;
318
+ const cachedItem = CACHE_.get(key);
319
+ const executeCallback = /* @__PURE__ */ __name(async () => {
320
+ const expire = now + duration;
321
+ const t = await callback();
322
+ CACHE_.set(key, {
323
+ data: t,
324
+ expire
325
+ });
326
+ return t;
327
+ }, "executeCallback");
328
+ if (cachedItem) {
329
+ if (now < cachedItem.expire) {
330
+ return cachedItem.data;
331
+ } else {
332
+ CACHE_.delete(key);
333
+ return await executeCallback();
334
+ }
335
+ } else {
336
+ return await executeCallback();
337
+ }
338
+ }
339
+ __name(fromCache, "fromCache");
340
+
314
341
  // src/seo.ts
315
342
  var replaceTokens = /* @__PURE__ */ __name(async (req, collection, doc, str, locale) => {
316
343
  const tokens = str.match(/\[(.*?)\]/g) || [];
@@ -362,7 +389,11 @@ var replaceTokens = /* @__PURE__ */ __name(async (req, collection, doc, str, loc
362
389
  str = str.replace(token, titleFieldValue);
363
390
  } else {
364
391
  const titleFieldValue = findVal(fieldValue, titleField.name);
365
- str = str.replace(token, locale && titleField?.localized ? titleFieldValue[locale.code] ?? titleFieldValue[defaultLocale2] : titleFieldValue);
392
+ if (!titleFieldValue || Object.keys(titleFieldValue).length === 0) {
393
+ str = str.replace(token, "");
394
+ } else {
395
+ str = str.replace(token, locale && titleField?.localized ? titleFieldValue[locale.code] ?? titleFieldValue[defaultLocale2] : titleFieldValue);
396
+ }
366
397
  }
367
398
  } else {
368
399
  str = str.replace(token, "");
@@ -379,28 +410,34 @@ var replaceTokens = /* @__PURE__ */ __name(async (req, collection, doc, str, loc
379
410
  }
380
411
  return str;
381
412
  }, "replaceTokens");
382
- var setCollectionMetatags = /* @__PURE__ */ __name(({ req, doc, query }) => async (collection) => {
413
+ var setDocMetatags = /* @__PURE__ */ __name(async ({
414
+ req,
415
+ doc,
416
+ collection
417
+ }) => {
383
418
  try {
384
- if (req.query[options.qsMetatagsRules] === void 0) {
385
- return doc;
386
- }
387
419
  const { payload: payload2 } = req;
388
420
  const defaultLocale2 = req.payload.config.localization ? req.payload.config.localization.defaultLocale : "en";
389
- const result = await payload2.find({
390
- collection: options.slug.metatagsRule,
391
- where: {
392
- or: [
393
- { collections: { equals: "" } },
394
- { collections: { equals: [] } },
395
- { collections: { equals: null } },
396
- { collections: { exists: false } },
397
- { collections: { contains: collection.slug } }
398
- ]
399
- },
400
- pagination: false,
401
- overrideAccess: true,
402
- locale: "all"
403
- });
421
+ const result = await fromCache(
422
+ async () => await payload2.find({
423
+ collection: options.slug.metatagsRule,
424
+ where: {
425
+ or: [
426
+ { collections: { equals: "" } },
427
+ { collections: { equals: [] } },
428
+ { collections: { equals: null } },
429
+ { collections: { exists: false } },
430
+ { collections: { contains: collection.slug } }
431
+ ]
432
+ },
433
+ pagination: false,
434
+ overrideAccess: true,
435
+ locale: "all"
436
+ }),
437
+ "metatags-rules-" + collection.slug,
438
+ 60
439
+ // TODO: add to plugin options
440
+ );
404
441
  let rules = result.docs;
405
442
  rules = rules.sort((a, b) => a.collections.length === 0 ? 1 : b.collections.length === 0 ? -1 : 0);
406
443
  const metaFields = ["title", "description", "keywords", ...options.additionalFields.map((field) => field.name)];
@@ -437,6 +474,40 @@ var setCollectionMetatags = /* @__PURE__ */ __name(({ req, doc, query }) => asyn
437
474
  console.error(`Error while setting seo metatags: ${error}`);
438
475
  }
439
476
  return doc;
477
+ }, "setDocMetatags");
478
+ var setCollectionMetatags = /* @__PURE__ */ __name(async ({
479
+ args,
480
+ operation,
481
+ result
482
+ }) => {
483
+ const { req, collection } = args;
484
+ if (req?.query && req.query[options.qsMetatagsRules] === void 0) {
485
+ return result;
486
+ }
487
+ if (operation !== "find") {
488
+ if (result && typeof result === "object" && !Array.isArray(result) && !("docs" in result)) {
489
+ const updatedDoc = await setDocMetatags({
490
+ req,
491
+ doc: result,
492
+ collection: collection.config
493
+ });
494
+ return updatedDoc;
495
+ }
496
+ } else if (operation === "find" && req?.path === "/store") {
497
+ if (result && typeof result === "object" && "docs" in result && Array.isArray(result.docs)) {
498
+ const documentPromises = result.docs.map(async (doc) => {
499
+ return await setDocMetatags({
500
+ req,
501
+ doc,
502
+ collection: collection.config
503
+ });
504
+ });
505
+ const updatedDocs = await Promise.all(documentPromises);
506
+ result.docs = updatedDocs;
507
+ return result;
508
+ }
509
+ }
510
+ return result;
440
511
  }, "setCollectionMetatags");
441
512
  var seo = /* @__PURE__ */ __name((sourceOptions) => (sourceConfig) => {
442
513
  options.collections = sourceOptions.collections ? Object.assign(options.collections, sourceOptions.collections) : Object.assign(options.collections, sourceConfig.collections?.map((x) => x.slug));
@@ -447,7 +518,10 @@ var seo = /* @__PURE__ */ __name((sourceOptions) => (sourceConfig) => {
447
518
  collections?.forEach((collection) => {
448
519
  collection.hooks = {
449
520
  ...collection.hooks,
450
- afterRead: [...collection.hooks?.afterRead || [], (args) => setCollectionMetatags(args)(collection)]
521
+ afterOperation: [
522
+ ...collection.hooks?.afterOperation || [],
523
+ setCollectionMetatags
524
+ ]
451
525
  };
452
526
  });
453
527
  const targetConfig = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@websolutespa/payload-plugin-seo",
3
- "version": "0.1.3",
3
+ "version": "2.0.0",
4
4
  "description": "SEO plugin for PayloadCms",
5
5
  "keywords": [
6
6
  "payload",
@@ -27,7 +27,7 @@
27
27
  },
28
28
  "peerDependencies": {
29
29
  "@payloadcms/db-mongodb": ">= 1.2.0",
30
- "@websolutespa/payload-utils": "*",
30
+ "@websolutespa/payload-utils": "^2.0.0",
31
31
  "payload": ">= 2.28.0 < 3",
32
32
  "react": ">= 18.2.0",
33
33
  "uuid": ">= 9.0.1"
@@ -35,13 +35,13 @@
35
35
  "devDependencies": {
36
36
  "@types/express": "^4.17.15",
37
37
  "@types/uuid": "^9.0.2",
38
- "@websolutespa/bom-cli": "*",
39
- "@websolutespa/bom-env": "*",
40
- "@websolutespa/test": "*",
41
- "@websolutespa/tsconfig": "*",
38
+ "@websolutespa/bom-cli": "^2.0.0",
39
+ "@websolutespa/bom-env": "^2.0.0",
40
+ "@websolutespa/test": "^2.0.0",
41
+ "@websolutespa/tsconfig": "^2.0.0",
42
42
  "esbuild-sass-plugin": "^2.10.0",
43
43
  "eslint": "^8.56.0",
44
- "eslint-config-websolute": "*",
44
+ "eslint-config-websolute": "^2.0.0",
45
45
  "mongodb-memory-server": "^8.13.0",
46
46
  "react": "^18.2.0",
47
47
  "ts-node": "^10.9.2",