Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#33656: Storyblok - tweak stories invalidation #115

Merged
merged 2 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/local.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion scripts/storyblok.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -35,6 +36,8 @@ program
await seedStoryblokDatasources(db, config);
}

await cacheInvalidate(config.storyblok, 'storyblok');

process.exit(0)
});

Expand Down
7 changes: 4 additions & 3 deletions src/api/extensions/vsf-storyblok-extension/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

function getHitsAsStories (hits) {
if (hits.total === 0) {
log('Missing stories')

Check warning on line 28 in src/api/extensions/vsf-storyblok-extension/helpers.js

View workflow job for this annotation

GitHub Actions / build (10.x)

'log' was used before it was defined

Check warning on line 28 in src/api/extensions/vsf-storyblok-extension/helpers.js

View workflow job for this annotation

GitHub Actions / build (12.x)

'log' was used before it was defined

Check warning on line 28 in src/api/extensions/vsf-storyblok-extension/helpers.js

View workflow job for this annotation

GitHub Actions / build (10.x)

'log' was used before it was defined

Check warning on line 28 in src/api/extensions/vsf-storyblok-extension/helpers.js

View workflow job for this annotation

GitHub Actions / build (12.x)

'log' was used before it was defined
return []
}
const stories = hits.hits.map((hit) => hit._source)
Expand All @@ -41,7 +41,7 @@

export const transformStory = (index, story, forIndexing = true) => {
if (story.content.parent && forIndexing) {
story.content.parent = resolveParentData(story.content.parent)

Check warning on line 44 in src/api/extensions/vsf-storyblok-extension/helpers.js

View workflow job for this annotation

GitHub Actions / build (10.x)

'resolveParentData' was used before it was defined

Check warning on line 44 in src/api/extensions/vsf-storyblok-extension/helpers.js

View workflow job for this annotation

GitHub Actions / build (12.x)

'resolveParentData' was used before it was defined

Check warning on line 44 in src/api/extensions/vsf-storyblok-extension/helpers.js

View workflow job for this annotation

GitHub Actions / build (10.x)

'resolveParentData' was used before it was defined

Check warning on line 44 in src/api/extensions/vsf-storyblok-extension/helpers.js

View workflow job for this annotation

GitHub Actions / build (12.x)

'resolveParentData' was used before it was defined
}

story.content = JSON.stringify(story.content)
Expand Down Expand Up @@ -119,11 +119,12 @@
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 ✅')
}
Expand Down
2 changes: 1 addition & 1 deletion src/api/extensions/vsf-storyblok-extension/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
47 changes: 35 additions & 12 deletions src/api/extensions/vsf-storyblok-extension/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
})
}

function getCacheTagsByStoriesSlugs (slugs) {
return slugs.map((slug) => `storyblok_${slug}`)
}

async function indexStory ({ db, config, story }) {
const transformedStory = transformStory(config.storyblok.storiesIndex, story)

Expand Down Expand Up @@ -87,7 +91,7 @@

if (action === 'branch_deployed') {
await fullSync(db, config)
await cacheInvalidate(config.storyblok)
await cacheInvalidate(config.storyblok, 'storyblok')
return
}

Expand All @@ -98,19 +102,28 @@

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)
} catch (e) { }

const updatedStoriesSlugs = await handleActionForStory(db, config, id, action, cv)

Check warning on line 110 in src/api/extensions/vsf-storyblok-extension/sync.js

View workflow job for this annotation

GitHub Actions / build (10.x)

'handleActionForStory' was used before it was defined

Check warning on line 110 in src/api/extensions/vsf-storyblok-extension/sync.js

View workflow job for this annotation

GitHub Actions / build (12.x)

'handleActionForStory' was used before it was defined

Check warning on line 110 in src/api/extensions/vsf-storyblok-extension/sync.js

View workflow job for this annotation

GitHub Actions / build (10.x)

'handleActionForStory' was used before it was defined

Check warning on line 110 in src/api/extensions/vsf-storyblok-extension/sync.js

View workflow job for this annotation

GitHub Actions / build (12.x)

'handleActionForStory' was used before it was defined
const updatedRelatedStoriesSlugs = await handleActionForRelatedStories(db, config, id, action, cv)

Check warning on line 111 in src/api/extensions/vsf-storyblok-extension/sync.js

View workflow job for this annotation

GitHub Actions / build (10.x)

'handleActionForRelatedStories' was used before it was defined

Check warning on line 111 in src/api/extensions/vsf-storyblok-extension/sync.js

View workflow job for this annotation

GitHub Actions / build (12.x)

'handleActionForRelatedStories' was used before it was defined

Check warning on line 111 in src/api/extensions/vsf-storyblok-extension/sync.js

View workflow job for this annotation

GitHub Actions / build (10.x)

'handleActionForRelatedStories' was used before it was defined

Check warning on line 111 in src/api/extensions/vsf-storyblok-extension/sync.js

View workflow job for this annotation

GitHub Actions / build (12.x)

'handleActionForRelatedStories' was used before it was defined

await cacheInvalidate(
config.storyblok,
getCacheTagsByStoriesSlugs(
[
...updatedStoriesSlugs,
...updatedRelatedStoriesSlugs
]
)
)
}

const handleActionForRelatedStories = async (db, config, id, action, cv) => {
const size = 10
const updatedStoriesSlugs = []

switch (action) {
case 'deleted':
Expand All @@ -133,6 +146,7 @@

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 })
}
Expand All @@ -146,9 +160,13 @@
break
}
}

return updatedStoriesSlugs
}

const handleActionForStory = async (db, config, id, action, cv) => {
const updatedStoriesSlugs = []

switch (action) {
case 'published': {
let storiesToPublish = []
Expand Down Expand Up @@ -178,12 +196,14 @@

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 })
}
Expand All @@ -196,6 +216,7 @@

for (const storyToDelete of stories) {
log(`Try to delete story ${storyToDelete.full_slug}`)
updatedStoriesSlugs.push(storyToDelete.full_slug)

await deleteStory({ db, config, story: storyToDelete });
}
Expand All @@ -208,6 +229,8 @@
break
}
}

return updatedStoriesSlugs
}

const seedDatabase = async (db, config) => {
Expand Down Expand Up @@ -245,7 +268,7 @@
body: {
'size': 1000,
'sort': [
{'name.keyword': 'asc'}
{ 'name.keyword': 'asc' }
],
'query': {
'constant_score': {
Expand Down Expand Up @@ -306,14 +329,14 @@
body: {
'size': 1000,
'sort': [
{'name.keyword': 'asc'}
{ 'name.keyword': 'asc' }
],
'query': {
'constant_score': {
'filter': {
'bool': {
'must': [
{'term': {'is_active': true}}
{ 'term': { 'is_active': true } }
]
}
}
Expand All @@ -328,7 +351,7 @@
body: {
'size': 1000,
'sort': [
{'name.keyword': 'asc'}
{ 'name.keyword': 'asc' }
],
'query': {}
}
Expand Down
Loading