Skip to content

Commit cab029f

Browse files
fix: ghost unpublish delete record operations (#518)
* fix: add optional chaining for ghost unpublish operations that should delete records on Algolia * fix: siteLang logic when unpublishing posts
1 parent a03a567 commit cab029f

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

lib/utils/helpers.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ export const getBaseSiteURL = (url) => {
108108
let siteLang;
109109
if (host.startsWith('chinese')) {
110110
siteLang = 'chinese';
111-
} else if (pathParts.length === 3) {
111+
} else if (pathParts.length >= 3) {
112112
// Webhooks will only be triggered by posts, so the path will
113-
// always have 3 parts for our localized instances:
114-
// (/<lang>/news/<slug>/).
113+
// always have 3-4 parts for our localized instances:
114+
// (/<lang>/news/<slug>/) or (/<lang>/news/<author>/<slug>/).
115115
// Or if it's coming from a Dockerized test instance / localhost,
116116
// it will only have 1 part: (/<slug>/).
117117
siteLang = pathParts[0];

packages/ghost/delete-record/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ export const deleteRecord = async (req) => {
1818

1919
// Whether a published post is unpublished or deleted, the
2020
// status will be 'published' in the previous state
21-
if (prevState.status === 'published') {
21+
if (prevState?.status === 'published') {
2222
// Deleted posts don't include a url or a primary author object.
2323
// But since every post returns an author array, we can use
2424
// the first author object to determine the search index name.
2525
// This helps since we're handling data from localhost,
2626
// chinese.freecodecamp.org, and www.freecodecamp.org.
27-
const primaryAuthorURL = prevState.authors
28-
? prevState.authors[0].url
29-
: currState.authors[0].url;
27+
const primaryAuthorURL = prevState?.authors
28+
? prevState?.authors[0]?.url
29+
: currState?.authors[0]?.url;
3030
const siteURL = getBaseSiteURL(primaryAuthorURL);
3131
const indexName = getSearchIndexName(siteURL);
3232

0 commit comments

Comments
 (0)