From 8eff35466b24440b0e14b27565cf7983ab37ed80 Mon Sep 17 00:00:00 2001 From: Stuart Clark Date: Fri, 30 Jun 2023 10:19:03 +1000 Subject: [PATCH] chore(#639): update bypassCache behaviour --- packages/druxt/src/stores/druxt.js | 12 +++++--- .../entity/src/components/DruxtEntity.vue | 6 ++-- packages/views/src/components/DruxtView.vue | 30 +++++++++++++++---- packages/views/src/stores/views.js | 21 ++++++++----- 4 files changed, 49 insertions(+), 20 deletions(-) diff --git a/packages/druxt/src/stores/druxt.js b/packages/druxt/src/stores/druxt.js index 5365e3595..c08b143df 100644 --- a/packages/druxt/src/stores/druxt.js +++ b/packages/druxt/src/stores/druxt.js @@ -244,7 +244,7 @@ const DruxtStore = ({ store }) => { */ async getResource ({ commit, dispatch, state }, { type, id, query, prefix, bypassCache = false }) { // Get the resource from the store if it's avaialble. - const storedResource = !bypassCache && ((state.resources[type] || {})[id] || {})[prefix] ? + const storedResource = ((state.resources[type] || {})[id] || {})[prefix] ? { ...state.resources[type][id][prefix] } : null @@ -322,9 +322,13 @@ const DruxtStore = ({ store }) => { // Request the resource from the DruxtClient if required. let resource - if (!storedResource || fields) { - resource = await this.$druxt.getResource(type, id, getDrupalJsonApiParams(queryObject), prefix) - commit('addResource', { prefix, resource: { ...resource } }) + if (bypassCache || !storedResource || fields) { + try { + resource = await this.$druxt.getResource(type, id, getDrupalJsonApiParams(queryObject), prefix) + commit('addResource', { prefix, resource: { ...resource } }) + } catch(e) { + // Do nothing, just don't error. + } } // Build resource to be returned. diff --git a/packages/entity/src/components/DruxtEntity.vue b/packages/entity/src/components/DruxtEntity.vue index 9a7830fe5..ad18feaef 100644 --- a/packages/entity/src/components/DruxtEntity.vue +++ b/packages/entity/src/components/DruxtEntity.vue @@ -187,7 +187,7 @@ export default { }, }, - created() { + mounted() { // If static, re-fetch data allowing for cache-bypass. // @TODO - Don't re-fetch in serverless configuration. if (this.$store.app.context.isStatic) { @@ -365,9 +365,9 @@ export default { // Start with the `nuxt.config.js` `druxt.settings.entity` settings and // merge the Wrapper component settings on top. - let mergedSettings = merge($druxt.settings.entity || {}, wrapperSettings, { arrayMerge: (dest, src) => src }) + let mergedSettings = merge(($druxt.settings || {}).entity || {}, wrapperSettings || {}, { arrayMerge: (dest, src) => src }) // Merge the DruxtEntity component `settings` property on top. - mergedSettings = merge(mergedSettings || {}, settings, { arrayMerge: (dest, src) => src }) + mergedSettings = merge(mergedSettings || {}, settings || {}, { arrayMerge: (dest, src) => src }) // Evaluate the bypass cache function. if (typeof (mergedSettings.query || {}).bypassCache === 'function') { diff --git a/packages/views/src/components/DruxtView.vue b/packages/views/src/components/DruxtView.vue index 8d76d969d..ea071699d 100644 --- a/packages/views/src/components/DruxtView.vue +++ b/packages/views/src/components/DruxtView.vue @@ -78,6 +78,17 @@ export default { default: 'default', }, + /** + * Module settings object. + * + * @type {ModuleSettings} + * @default {} + */ + settings: { + type: Object, + default: () => ({}), + }, + /** * JSON:API Resource type. * @@ -329,7 +340,7 @@ export default { }, }, - created() { + mounted() { // If static, re-fetch data allowing for cache-bypass. // @TODO - Don't re-fetch in serverless configuration. if (this.$store.app.context.isStatic) { @@ -493,16 +504,23 @@ export default { * Component settings. */ settings: (context, wrapperSettings) => { - const { $druxt } = context - const settings = merge($druxt.settings.views || {}, wrapperSettings, { arrayMerge: (dest, src) => src }) + const { $druxt, settings } = context + + // Start with the `nuxt.config.js` `druxt.settings.views` settings and + // merge the Wrapper component settings on top. + let mergedSettings = merge(($druxt.settings || {}).views || {}, wrapperSettings || {}, { arrayMerge: (dest, src) => src }) + // Merge the DruxtViews component `settings` property on top. + mergedSettings = merge(mergedSettings || {}, settings || {}, { arrayMerge: (dest, src) => src }) // Evaluate the bypass cache function. - if (typeof (settings.query || {}).bypassCache === 'function') { - settings.query.bypassCache = !!settings.query.bypassCache(context) + if (typeof (mergedSettings.query || {}).bypassCache === 'function') { + mergedSettings.query.bypassCache = !!mergedSettings.query.bypassCache(context) } + console.log('mergedSettings', mergedSettings) + return { - query: settings.query || {}, + query: mergedSettings.query || {}, } }, diff --git a/packages/views/src/stores/views.js b/packages/views/src/stores/views.js index 76cbdc981..e4dc3657a 100644 --- a/packages/views/src/stores/views.js +++ b/packages/views/src/stores/views.js @@ -102,15 +102,22 @@ const DruxtViewsStore = ({ store }) => { */ async getResults ({ commit, state }, { viewId, displayId, query, prefix, bypassCache = false }) { const hash = query ? md5(this.$druxt.buildQueryUrl('', query)) : '_default' - if (!bypassCache && typeof (((state.results[viewId] || {})[displayId] || {})[prefix] || {})[hash] !== 'undefined') { - return state.results[viewId][displayId][prefix][hash] - } - - const results = await this.$druxt.getResource(`views--${viewId}`, displayId, query, prefix) - commit('addResults', { results, viewId, displayId, prefix, hash }) + let cache + if (typeof (((state.results[viewId] || {})[displayId] || {})[prefix] || {})[hash] !== 'undefined') { + cache = state.results[viewId][displayId][prefix][hash] + if (!bypassCache) return cache + } - return results + try { + const results = await this.$druxt.getResource(`views--${viewId}`, displayId, query, prefix) + commit('addResults', { results, viewId, displayId, prefix, hash }) + return results + } + catch(e) { + // Fall back to cache if possible. + return cache ? cache : false + } } } }