Skip to content

Commit

Permalink
feat(#639): add cache control to druxt views
Browse files Browse the repository at this point in the history
  • Loading branch information
Decipher committed Jul 3, 2023
1 parent a8585cd commit 0c58202
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
7 changes: 7 additions & 0 deletions .changeset/angry-panthers-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"druxt-views": minor
---

feat(#639): added druxt/views/flushResults mutation
feat(#639): added bypassCache option to druxt/views/getResults action
feat(#639): added druxt.query.bypassCache option to DruxtView
29 changes: 27 additions & 2 deletions packages/views/src/components/DruxtView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,14 @@ export default {
},
},
created() {
// If static, re-fetch data allowing for cache-bypass.
// @TODO - Don't re-fetch in serverless configuration.
if (this.$store.app.context.isStatic) {
this.$fetch()
}
},
methods: {
/**
* Builds the query for the JSON:API request.
Expand Down Expand Up @@ -446,12 +454,22 @@ export default {
async fetchData(settings) {
const viewId = this.viewId || (((this.view || {}).data || {}).attributes || {}).drupal_internal__id
if (viewId) {
// Check if we need to bypass cache.
let bypassCache = false
if (typeof settings.query.bypassCache === 'boolean') {
bypassCache = settings.query.bypassCache
}
// Build query.
const query = this.getQuery(settings)
// Execute the resquest.
this.resource = await this.getResults({
displayId: this.displayId,
prefix: this.lang,
query: stringify(query),
viewId
viewId,
bypassCache
})
}
},
Expand All @@ -474,8 +492,15 @@ export default {
/**
* Component settings.
*/
settings: ({ $druxt }, wrapperSettings) => {
settings: (context, wrapperSettings) => {
const { $druxt } = context
const settings = merge($druxt.settings.views || {}, wrapperSettings, { arrayMerge: (dest, src) => src })
// Evaluate the bypass cache function.
if (typeof settings.query.bypassCache === 'function') {
settings.query.bypassCache = !!settings.query.bypassCache(context)
}
return {
query: settings.query || {},
}
Expand Down
21 changes: 18 additions & 3 deletions packages/views/src/stores/views.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,22 @@ const DruxtViewsStore = ({ store }) => {
if (!state.results[viewId][displayId]) Vue.set(state.results[viewId], displayId, {})
if (!state.results[viewId][displayId][prefix]) Vue.set(state.results[viewId][displayId], prefix, {})
Vue.set(state.results[viewId][displayId][prefix], hash, results)
}
},

/**
* @name flushResults
* @mutator {object} addResults=results Removes JSON:API Views results from the Vuex state object.
*
* @example @lang js
* this.$store.commit('druxt/views/flushResults', { viewId, displayId, prefix, hash })
*/
flushResults (state, { viewId, displayId, prefix, hash }) {
if (!viewId) Vue.set(state, 'results', {})
else if (viewId && !displayId) Vue.set(state.results, viewId, {})
else if (viewId && displayId && !prefix) Vue.set(state.results[viewId], displayId, {})
else if (viewId && displayId && prefix && !hash) Vue.set(state.results[viewId][displayId], prefix, {})
else if (viewId && displayId && prefix && hash) Vue.set(state.results[viewId][displayId][prefix], hash, {})
},
},

/**
Expand All @@ -79,9 +94,9 @@ const DruxtViewsStore = ({ store }) => {
* query
* })
*/
async getResults ({ commit, state }, { viewId, displayId, query, prefix }) {
async getResults ({ commit, state }, { viewId, displayId, query, prefix, bypassCache = false }) {
const hash = query ? md5(this.$druxt.buildQueryUrl('', query)) : '_default'
if (typeof (((state.results[viewId] || {})[displayId] || {})[prefix] || {})[hash] !== 'undefined') {
if (!bypassCache && typeof (((state.results[viewId] || {})[displayId] || {})[prefix] || {})[hash] !== 'undefined') {
return state.results[viewId][displayId][prefix][hash]
}

Expand Down

0 comments on commit 0c58202

Please sign in to comment.