Skip to content

Commit afc23fd

Browse files
committed
perf: lazily resolve download URL on button click
Instead of eagerly fetching the download URL on component mount, resolve it on demand when the user clicks the download button.
1 parent f515d1a commit afc23fd

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/components/FileResolve.vue

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,27 @@ const { entity, metadata } = defineProps<{
1717
}>();
1818
1919
const data = ref();
20-
const downloadUrl = ref('');
2120
const streamUrl = ref('');
2221
2322
const resolveFile = async () => {
2423
if (entity.entityType !== 'http://schema.org/MediaObject') {
2524
return;
2625
}
2726
28-
downloadUrl.value = (await api.getFileUrl(entity.fileId, metadata.filename, true)) || '';
2927
streamUrl.value = (await api.getFileUrl(entity.fileId, metadata.filename, false)) || '';
3028
};
3129
30+
const handleDownload = async () => {
31+
if (entity.entityType !== 'http://schema.org/MediaObject') {
32+
return;
33+
}
34+
35+
const url = await api.getFileUrl(entity.fileId, metadata.filename, true);
36+
if (url) {
37+
window.location.href = url;
38+
}
39+
};
40+
3241
const extension = metadata.filename.split('.').pop() || '';
3342
const encodingFormat = [metadata.encodingFormat].flat();
3443
const plainEncodingFormats = encodingFormat.filter((ef) => typeof ef === 'string');
@@ -94,10 +103,8 @@ resolveFile();
94103

95104
<el-row class="flex justify-center" v-if="entity.access.content">
96105
<el-button-group class="m-2">
97-
<el-link class="mr-2" underline="never" :href="downloadUrl">
98-
<el-button type="default">Download File&nbsp;<font-awesome-icon icon="fa fa-download" />
99-
</el-button>
100-
</el-link>
106+
<el-button type="default" @click="handleDownload">Download File&nbsp;<font-awesome-icon icon="fa fa-download" />
107+
</el-button>
101108
</el-button-group>
102109
</el-row>
103110
</el-col>

0 commit comments

Comments
 (0)