From ca756adbe07b9c1fd3eb55c3fd3cc63fa4afc621 Mon Sep 17 00:00:00 2001 From: Anton Sylka Date: Thu, 16 Jan 2025 13:23:37 +0800 Subject: [PATCH 1/3] Refs #33747: Storyblok Extension - tweak cache invalidation to allow invalidate separate stories --- config/local.json.template | 2 +- scripts/storyblok.js | 5 ++- .../vsf-storyblok-extension/helpers.js | 7 ++-- .../vsf-storyblok-extension/index.js | 2 +- .../vsf-storyblok-extension/sync.js | 35 +++++++++++++++---- 5 files changed, 39 insertions(+), 12 deletions(-) diff --git a/config/local.json.template b/config/local.json.template index 61663993..7ce64cc0 100644 --- a/config/local.json.template +++ b/config/local.json.template @@ -172,7 +172,7 @@ "previewToken": "${STORYBLOK_TOKEN}", "managementToken": "${STORYBLOK_MANAGEMENT_TOKEN}", "hookSecret": "${STORYBLOK_HOOK_SECRET}", - "invalidate": "https://${VSF_DOMAIN}/invalidate?tag=storyblok&key=${CACHE_INVALIDATION_KEY}", + "invalidate": "https://${VSF_DOMAIN}/invalidate?tag={{tag}}&key=${CACHE_INVALIDATION_KEY}", "spaceId": "${STORYBLOK_SPACE_ID}", "settings": { "appendStoreCodeFromHeader": true diff --git a/scripts/storyblok.js b/scripts/storyblok.js index 07dfd671..a58717a2 100644 --- a/scripts/storyblok.js +++ b/scripts/storyblok.js @@ -3,7 +3,8 @@ const config = require('config') const { getClient } = require('../src/lib/elastic'); const { seedDatabase, seedStoryblokDatasources } = require('../src/api/extensions/vsf-storyblok-extension/sync'); -const {initStoryblokClients} = require('../src/api/extensions/vsf-storyblok-extension/storyblok'); +const { initStoryblokClients } = require('../src/api/extensions/vsf-storyblok-extension/storyblok'); +const { cacheInvalidate } = require('../src/api/extensions/vsf-storyblok-extension/helpers'); const db = getClient(config); initStoryblokClients(config); @@ -35,6 +36,8 @@ program await seedStoryblokDatasources(db, config); } + await cacheInvalidate(config.storyblok, 'storyblok'); + process.exit(0) }); diff --git a/src/api/extensions/vsf-storyblok-extension/helpers.js b/src/api/extensions/vsf-storyblok-extension/helpers.js index 9977d80f..210a650c 100644 --- a/src/api/extensions/vsf-storyblok-extension/helpers.js +++ b/src/api/extensions/vsf-storyblok-extension/helpers.js @@ -119,11 +119,12 @@ export const log = (string) => { console.log('📖 : ' + string) // eslint-disable-line no-console } -export const cacheInvalidate = async (config) => { +export const cacheInvalidate = async (config, tag) => { if (config.invalidate) { - log(`Invalidating cache... (${config.invalidate})`) + const uri = config.invalidate.replace('{{tag}}', tag) + log(`Invalidating cache... (${uri})`) await rp({ - uri: config.invalidate + uri }) log('Invalidated cache ✅') } diff --git a/src/api/extensions/vsf-storyblok-extension/index.js b/src/api/extensions/vsf-storyblok-extension/index.js index 8740806a..8d2a3152 100644 --- a/src/api/extensions/vsf-storyblok-extension/index.js +++ b/src/api/extensions/vsf-storyblok-extension/index.js @@ -55,7 +55,7 @@ module.exports = ({ config }) => { api.get('/full', protectRoute(config), async (req, res) => { await fullSync(db, config) - await cacheInvalidate(config.storyblok) + await cacheInvalidate(config.storyblok, 'storyblok') log('Stories synced!') apiStatus(res) }) diff --git a/src/api/extensions/vsf-storyblok-extension/sync.js b/src/api/extensions/vsf-storyblok-extension/sync.js index e1cf5c6f..f8d4bd19 100644 --- a/src/api/extensions/vsf-storyblok-extension/sync.js +++ b/src/api/extensions/vsf-storyblok-extension/sync.js @@ -9,6 +9,10 @@ function indexStories ({ db, config, stories = [] }) { }) } +function getCacheTagsByStoriesSlugs (slugs) { + return slugs.map((slug) => `S_${slug}`) +} + async function indexStory ({ db, config, story }) { const transformedStory = transformStory(config.storyblok.storiesIndex, story) @@ -87,7 +91,7 @@ const handleHook = async (db, config, params) => { if (action === 'branch_deployed') { await fullSync(db, config) - await cacheInvalidate(config.storyblok) + await cacheInvalidate(config.storyblok, 'storyblok') return } @@ -98,19 +102,28 @@ const handleHook = async (db, config, params) => { if (response.data.story && response.data.story.is_folder) { await fullSync(db, config) - await cacheInvalidate(config.storyblok) + await cacheInvalidate(config.storyblok, 'storyblok') return } } catch (e) {} - await handleActionForStory(db, config, id, action, cv) - await handleActionForRelatedStories(db, config, id, action, cv) - - await cacheInvalidate(config.storyblok) + const updatedStoriesSlugs = await handleActionForStory(db, config, id, action, cv) + const updatedRelatedStoriesSlugs = await handleActionForRelatedStories(db, config, id, action, cv) + + await cacheInvalidate( + config.storyblok, + getCacheTagsByStoriesSlugs( + [ + ...updatedStoriesSlugs, + ...updatedRelatedStoriesSlugs + ] + ) + ) } const handleActionForRelatedStories = async (db, config, id, action, cv) => { const size = 10 + const updatedStoriesSlugs = [] switch (action) { case 'deleted': @@ -133,6 +146,7 @@ const handleActionForRelatedStories = async (db, config, id, action, cv) => { for (const storyToReindex of response.data.stories) { log(`Try to reindex story ${storyToReindex.full_slug} with ${id} ID reference`) + updatedStoriesSlugs.push(storyToReindex.full_slug) await indexStory({ db, config, story: storyToReindex }) } @@ -146,9 +160,13 @@ const handleActionForRelatedStories = async (db, config, id, action, cv) => { break } } + + return updatedStoriesSlugs } const handleActionForStory = async (db, config, id, action, cv) => { + const updatedStoriesSlugs = [] + switch (action) { case 'published': { let storiesToPublish = [] @@ -178,12 +196,14 @@ const handleActionForStory = async (db, config, id, action, cv) => { for (const storyToDelete of storiesToDelete) { log(`Try to delete old copy of story ${storyToDelete.full_slug}`) + updatedStoriesSlugs.push(storyToDelete.full_slug) await deleteStory({ db, config, story: storyToDelete }) } for (const storyToPublish of storiesToPublish) { log(`Try to reindex story ${storyToPublish.full_slug}`) + updatedStoriesSlugs.push(storyToPublish.full_slug) await indexStory({ db, config, story: storyToPublish }) } @@ -196,6 +216,7 @@ const handleActionForStory = async (db, config, id, action, cv) => { for (const storyToDelete of stories) { log(`Try to delete story ${storyToDelete.full_slug}`) + updatedStoriesSlugs.push(storyToDelete.full_slug) await deleteStory({ db, config, story: storyToDelete }); } @@ -208,6 +229,8 @@ const handleActionForStory = async (db, config, id, action, cv) => { break } } + + return updatedStoriesSlugs } const seedDatabase = async (db, config) => { From 4e5953096dd82dcba4890802aff588ea104bebd7 Mon Sep 17 00:00:00 2001 From: Anton Sylka Date: Tue, 21 Jan 2025 13:07:24 +0800 Subject: [PATCH 2/3] Refs #33807: Storyblok Cache Invalidation - update cache tag --- src/api/extensions/vsf-storyblok-extension/sync.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/api/extensions/vsf-storyblok-extension/sync.js b/src/api/extensions/vsf-storyblok-extension/sync.js index f8d4bd19..3900c2b6 100644 --- a/src/api/extensions/vsf-storyblok-extension/sync.js +++ b/src/api/extensions/vsf-storyblok-extension/sync.js @@ -10,7 +10,7 @@ function indexStories ({ db, config, stories = [] }) { } function getCacheTagsByStoriesSlugs (slugs) { - return slugs.map((slug) => `S_${slug}`) + return slugs.map((slug) => `storyblok_${slug}`) } async function indexStory ({ db, config, story }) { @@ -105,7 +105,7 @@ const handleHook = async (db, config, params) => { await cacheInvalidate(config.storyblok, 'storyblok') return } - } catch (e) {} + } catch (e) { } const updatedStoriesSlugs = await handleActionForStory(db, config, id, action, cv) const updatedRelatedStoriesSlugs = await handleActionForRelatedStories(db, config, id, action, cv) @@ -268,7 +268,7 @@ const seedStoryblokDatasources = async (db, config) => { body: { 'size': 1000, 'sort': [ - {'name.keyword': 'asc'} + { 'name.keyword': 'asc' } ], 'query': { 'constant_score': { @@ -329,14 +329,14 @@ const seedStoryblokDatasources = async (db, config) => { body: { 'size': 1000, 'sort': [ - {'name.keyword': 'asc'} + { 'name.keyword': 'asc' } ], 'query': { 'constant_score': { 'filter': { 'bool': { 'must': [ - {'term': {'is_active': true}} + { 'term': { 'is_active': true } } ] } } @@ -351,7 +351,7 @@ const seedStoryblokDatasources = async (db, config) => { body: { 'size': 1000, 'sort': [ - {'name.keyword': 'asc'} + { 'name.keyword': 'asc' } ], 'query': {} } From 32961e412ba1cee6bd36e893152ae0642c15f2f9 Mon Sep 17 00:00:00 2001 From: Anton Sylka Date: Thu, 23 Jan 2025 19:01:17 +0800 Subject: [PATCH 3/3] Refs #33757: Product Config - include `visibility` field --- config/local.json.template | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/config/local.json.template b/config/local.json.template index 7ce64cc0..94208455 100644 --- a/config/local.json.template +++ b/config/local.json.template @@ -273,7 +273,7 @@ "loadByAttributeMetadata": false }, "product": { - "excludeFields": [ "updated_at", "created_at", "attribute_set_id", "status", "visibility", "tier_prices", "options_container", "msrp_display_actual_price_type", "has_options", "stock.manage_stock", "stock.use_config_min_qty", "stock.use_config_notify_stock_qty", "stock.stock_id", "stock.use_config_backorders", "stock.use_config_enable_qty_inc", "stock.enable_qty_increments", "stock.use_config_manage_stock", "stock.use_config_min_sale_qty", "stock.notify_stock_qty", "stock.use_config_max_sale_qty", "stock.use_config_max_sale_qty", "stock.qty_increments", "small_image", "sgn", "*.sgn"], + "excludeFields": [ "updated_at", "created_at", "attribute_set_id", "status", "tier_prices", "options_container", "msrp_display_actual_price_type", "has_options", "stock.manage_stock", "stock.use_config_min_qty", "stock.use_config_notify_stock_qty", "stock.stock_id", "stock.use_config_backorders", "stock.use_config_enable_qty_inc", "stock.enable_qty_increments", "stock.use_config_manage_stock", "stock.use_config_min_sale_qty", "stock.notify_stock_qty", "stock.use_config_max_sale_qty", "stock.use_config_max_sale_qty", "stock.qty_increments", "small_image", "sgn", "*.sgn"], "includeFields": [ "category_ids", "category", @@ -321,7 +321,8 @@ "thumbnail", "slug", "disabled_upgrades", - "customizations" + "customizations", + "visibility" ], "filterFieldMapping": { "category.name": "category.name.keyword"