Skip to content

Commit

Permalink
chore(#639): update bypassCache behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
Decipher committed Jun 30, 2023
1 parent cfc7dde commit 7ba1ff0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 20 deletions.
12 changes: 8 additions & 4 deletions packages/druxt/src/stores/druxt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions packages/entity/src/components/DruxtEntity.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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') {
Expand Down
30 changes: 24 additions & 6 deletions packages/views/src/components/DruxtView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ export default {
default: 'default',
},
/**
* Module settings object.
*
* @type {ModuleSettings}
* @default {}
*/
settings: {
type: Object,
default: () => ({}),
},
/**
* JSON:API Resource type.
*
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 || {},
}
},
Expand Down
21 changes: 14 additions & 7 deletions packages/views/src/stores/views.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
Expand Down

0 comments on commit 7ba1ff0

Please sign in to comment.