From c68c59ed254fcb584c5a423d0d8abfcdc3261a26 Mon Sep 17 00:00:00 2001 From: Stuart Clark Date: Thu, 29 Jun 2023 13:39:31 +1000 Subject: [PATCH] feat(#639): add bypassCache setting to DruxtEntity --- .changeset/khaki-glasses-sing.md | 5 ++++ .../entity/src/components/DruxtEntity.vue | 30 +++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 .changeset/khaki-glasses-sing.md diff --git a/.changeset/khaki-glasses-sing.md b/.changeset/khaki-glasses-sing.md new file mode 100644 index 000000000..0879dc757 --- /dev/null +++ b/.changeset/khaki-glasses-sing.md @@ -0,0 +1,5 @@ +--- +"druxt-entity": minor +--- + +feat(#639): add bypassCache druxt setting to DruxtEntity components. diff --git a/packages/entity/src/components/DruxtEntity.vue b/packages/entity/src/components/DruxtEntity.vue index 87f9a0eed..9a7830fe5 100644 --- a/packages/entity/src/components/DruxtEntity.vue +++ b/packages/entity/src/components/DruxtEntity.vue @@ -187,6 +187,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. @@ -318,12 +326,22 @@ export default { } if (this.uuid && !this.value) { + // 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. const resource = await this.getResource({ id: this.uuid, prefix: this.lang, type: this.type, - query + query, + bypassCache }) const entity = { ...(resource.data || {}) } entity.included = resource.included @@ -342,12 +360,20 @@ export default { /** * Component settings. */ - settings: ({ $druxt, settings }, wrapperSettings) => { + settings: (context, wrapperSettings) => { + const { $druxt, settings } = context + // 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 }) // Merge the DruxtEntity component `settings` property on top. mergedSettings = merge(mergedSettings || {}, settings, { arrayMerge: (dest, src) => src }) + + // Evaluate the bypass cache function. + if (typeof (mergedSettings.query || {}).bypassCache === 'function') { + mergedSettings.query.bypassCache = !!mergedSettings.query.bypassCache(context) + } + // Currently only returning the query settings. return { query: mergedSettings.query || {},