Skip to content

Commit

Permalink
Use etag as cache buster
Browse files Browse the repository at this point in the history
Signed-off-by: Louis Chemineau <louis@chmn.me>
  • Loading branch information
artonge committed Jun 7, 2023
1 parent 4794a82 commit 55d9e0b
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 12 deletions.
4 changes: 2 additions & 2 deletions js/viewer-main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/viewer-main.js.map

Large diffs are not rendered by default.

9 changes: 1 addition & 8 deletions src/components/Images.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
:mime="mime"
:src="src"
:fileid="fileid"
@updated="onUpdate"
@close="onClose" />

<img v-else-if="data !== null"
Expand All @@ -35,7 +34,7 @@
loaded,
zoomed: zoomRatio !== 1
}"
:src="`${data}${cacheBuster}`"
:src="data"
:style="{
marginTop: shiftY + 'px',
marginLeft: shiftX + 'px',
Expand Down Expand Up @@ -82,8 +81,6 @@ export default {
shiftY: 0,
zoomRatio: 1,
fallback: false,
cacheBuster: '',
cacheBusterCounter: 0,
}
},
Expand Down Expand Up @@ -267,10 +264,6 @@ export default {
this.fallback = true
}
},
onUpdate() {
this.cacheBuster = `?buster=${this.cacheBusterCounter++}`
},
},
}
</script>
Expand Down
5 changes: 4 additions & 1 deletion src/mixins/PreviewUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export default {
previewUrl: this.previewUrl,
hasPreview: this.hasPreview,
davPath: this.davPath,
etag: this.$attrs.etag,
})
},

Expand All @@ -65,9 +66,10 @@ export default {
* @param {boolean} data.hasPreview have the file an existing preview ?
* @param {string} data.davPath the absolute dav path
* @param {string} data.filename the file name
* @param {string} [data.etag] the etag of the file
* @return {string} the absolute url
*/
getPreviewIfAny({ fileid, filename, previewUrl, hasPreview, davPath }) {
getPreviewIfAny({ fileid, filename, previewUrl, hasPreview, davPath, etag }) {
if (previewUrl) {
return previewUrl
}
Expand All @@ -76,6 +78,7 @@ export default {
+ `&x=${Math.floor(screen.width * devicePixelRatio)}`
+ `&y=${Math.floor(screen.height * devicePixelRatio)}`
+ '&a=true'
+ (etag !== undefined ? `&etag=${etag.replace(/&quot;/g, '')}` : '')

if (hasPreview) {
// TODO: find a nicer standard way of doing this?
Expand Down
1 change: 1 addition & 0 deletions src/services/FileInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default async function(path, options) {
<d:getlastmodified />
<d:getcontenttype />
<d:resourcetype />
<d:getetag />
<oc:fileid />
<oc:permissions />
<oc:size />
Expand Down
18 changes: 18 additions & 0 deletions src/views/Viewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ export default {
// React to Files' Sidebar events.
subscribe('files:sidebar:opened', this.handleAppSidebarOpen)
subscribe('files:sidebar:closed', this.handleAppSidebarClose)
subscribe('files:node:updated', this.handleFileUpdated)
subscribe('viewer:trapElements:changed', this.handleTrapElementsChange)
window.addEventListener('keydown', this.keyboardDeleteFile)
window.addEventListener('keydown', this.keyboardDownloadFile)
Expand Down Expand Up @@ -968,6 +969,23 @@ export default {
this.trapElements = []
},
// Update etag of updated file to break cache.
async handleFileUpdated({ fileid }) {
const index = this.fileList.findIndex(({ fileid: currentFileId }) => currentFileId === fileid)
const file = this.fileList[index]
if (file === undefined) {
return
}
const fetchedFile = await getFileInfo(file.basename)
this.fileList.splice(index, 1, { ...file, etag: fetchedFile.etag })
if (file.fileid === this.currentFile.fileid) {
this.currentFile.etag = fetchedFile.etag
}
},
onResize() {
const sidebar = document.querySelector('aside.app-sidebar')
if (sidebar) {
Expand Down

0 comments on commit 55d9e0b

Please sign in to comment.