Skip to content

Beautiful Open-Source Form Builder

Lazy load submission table images when visible (#1368)

Julien Nahum55110325fcf0
diff --git a/client/components/open/forms/components/FormSubmissions.vue b/client/components/open/forms/components/FormSubmissions.vue
index d9ac10f16..31a7eeccc 100644
--- a/client/components/open/forms/components/FormSubmissions.vue
+++ b/client/components/open/forms/components/FormSubmissions.vue
@@ -72,6 +72,31 @@ const {
refetch
} = paginatedList(computed(() => props.form?.id))
+// Renew expired file links once for all thumbnails entering the viewport together.
+let fileLinksRefresh = null
+let refreshedFileLinks = new Set()
+let fileLinksRefreshedAt = -Infinity
+const refreshSubmissionFiles = () => {
+ if (!fileLinksRefresh) {
+ fileLinksRefresh = refetch({ cancelRefetch: false, throwOnError: true })
+ .then((result) => {
+ refreshedFileLinks = new Set((result.data?.data || [])
+ .flatMap(record => Object.values(record.data || {}).flat())
+ .map(value => value?.file_url)
+ .filter(Boolean))
+ fileLinksRefreshedAt = performance.now()
+ })
+ .finally(() => { fileLinksRefresh = null })
+ }
+ return fileLinksRefresh
+}
+provide('submissionFileLinks', {
+ refresh: refreshSubmissionFiles,
+ // Trust newly issued links for less than their ten-minute server validity,
+ // even if the browser's wall clock is ahead of the server's clock.
+ isFresh: src => performance.now() - fileLinksRefreshedAt < 9 * 60 * 1000 && refreshedFileLinks.has(src),
+})
+
// Replace existing event handlers:
const handleSearch = (searchTerm) => {
setSearch(searchTerm)
diff --git a/client/components/open/tables/components/OpenFile.vue b/client/components/open/tables/components/OpenFile.vue
index 50fbf09f5..359ca5957 100644
--- a/client/components/open/tables/components/OpenFile.vue
+++ b/client/components/open/tables/components/OpenFile.vue
@@ -16,12 +16,12 @@
:aria-label="`Preview ${file.displayed_file_name}`"
@click="openFilePreview(file)"
>
- <img
+ <OpenFileThumbnail
class="h-full w-full object-cover"
:src="file.file_url"
:alt="file.file_name"
@error="failedImages.push(file.file_url)"
- >
+ />
</button>
<button
v-else-if="file.is_pdf"
@@ -138,6 +138,8 @@
</template>
<script setup>
+import OpenFileThumbnail from './OpenFileThumbnail.vue'
+
const props = defineProps({
value: {
type: Array,
diff --git a/client/components/open/tables/components/OpenFileThumbnail.vue b/client/components/open/tables/components/OpenFileThumbnail.vue
new file mode 100644
index 000000000..dfa7744cb
--- /dev/null
+++ b/client/components/open/tables/components/OpenFileThumbnail.vue
@@ -0,0 +1,78 @@
+<template>
+ <img
+ ref="image"
+ :src="imageSrc"
+ :alt="alt"
+ width="32"
+ height="32"
+ decoding="async"
+ @load="stop"
+ @error="handleImageError"
+ >
+</template>
+
+<script setup>
+import { useIntersectionObserver } from '@vueuse/core'
+
+const props = defineProps({
+ src: { type: String, required: true },
+ alt: { type: String, required: true },
+})
+const emit = defineEmits(['error'])
+
+const image = ref(null)
+const isVisible = ref(false)
+const imageSrc = ref()
+const fileLinks = inject('submissionFileLinks', null)
+let isDisposed = false
+onBeforeUnmount(() => { isDisposed = true })
+// Observe both scroll axes and ancestor clipping, without preloading offscreen files.
+// Keep the source after the first appearance so scrolling back does not reload it.
+const { stop, isSupported } = useIntersectionObserver(image, ([entry]) => {
+ isVisible.value = entry?.isIntersecting ?? false
+}, { rootMargin: '0px' })
+
+onMounted(() => {
+ if (!isSupported.value) isVisible.value = true
+})
+
+const renewImage = () => {
+ imageSrc.value = undefined
+ fileLinks.refresh()
+ .then(() => nextTick())
+ .then(() => {
+ // Refreshed keys usually remount this component. Handle an unchanged
+ // URL too (e.g. a fast refresh with a skewed browser clock).
+ if (!isDisposed && isVisible.value && fileLinks.isFresh(props.src)) {
+ imageSrc.value = props.src
+ }
+ })
+ .catch(() => {
+ console.warn('Unable to refresh submission file links. Refresh the table to retry.')
+ })
+}
+
+const handleImageError = (event) => {
+ const isSigned = new URL(props.src, window.location.href).searchParams.has('expires')
+ // A slow browser clock can miss expiration. Retry with freshly issued links
+ // before treating the file as broken; failures of trusted links are final.
+ if (fileLinks && isSigned && !fileLinks.isFresh(props.src)) {
+ renewImage()
+ return
+ }
+ emit('error', event)
+}
+
+watch([isVisible, () => props.src], ([visible, src]) => {
+ if (!visible) return
+
+ const expires = new URL(src, window.location.href).searchParams.get('expires')
+ if (fileLinks && !fileLinks.isFresh(src) && expires && Number(expires) * 1000 <= Date.now() + 30000) {
+ // Refetching replaces the signed URLs in the table. Never request an expired URL.
+ renewImage()
+ return
+ }
+
+ imageSrc.value = src
+})
+</script>
diff --git a/client/test/nuxt/open-file-thumbnail.spec.ts b/client/test/nuxt/open-file-thumbnail.spec.ts
new file mode 100644
index 000000000..089af032a
--- /dev/null
+++ b/client/test/nuxt/open-file-thumbnail.spec.ts
@@ -0,0 +1,73 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+import { flushPromises, mount } from '@vue/test-utils'
+import OpenFileThumbnail from '~/components/open/tables/components/OpenFileThumbnail.vue'
+
+let observers: MockIntersectionObserver[]
+
+class MockIntersectionObserver {
+ callback: IntersectionObserverCallback
+ observe = vi.fn()
+ unobserve = vi.fn()
+ disconnect = vi.fn()
+
+ constructor(callback: IntersectionObserverCallback) {
+ this.callback = callback
+ observers.push(this)
+ }
+
+ intersect(isIntersecting: boolean) {
+ this.callback([{ isIntersecting } as IntersectionObserverEntry], this as unknown as IntersectionObserver)
+ }
+}
+
+beforeEach(() => {
+ observers = []
+ vi.stubGlobal('IntersectionObserver', MockIntersectionObserver)
+})
+
+afterEach(() => vi.unstubAllGlobals())
+
+describe('submission table image loading', () => {
+ it('withholds the image URL until visible and retains it after scrolling away', async () => {
+ const wrapper = mount(OpenFileThumbnail, { props: { src: '/test-image.jpg', alt: 'Upload' } })
+ await flushPromises()
+ const observer = observers.find(observer => observer.observe.mock.calls.some(([el]) => el === wrapper.element))!
+
+ observer.intersect(false)
+ await flushPromises()
+ expect(wrapper.attributes('src')).toBeUndefined()
+
+ observer.intersect(true)
+ await flushPromises()
+ expect(wrapper.attributes('src')).toBe('/test-image.jpg')
+ await wrapper.trigger('load')
+ expect(observer.disconnect).toHaveBeenCalled()
+
+ // A queued exit notification must not remove an already loaded source.
+ observer.intersect(false)
+ await flushPromises()
+ expect(wrapper.attributes('src')).toBe('/test-image.jpg')
+ wrapper.unmount()
+ })
+
+ it('preserves error handling and disconnects when an unseen row is removed', async () => {
+ const onError = vi.fn()
+ const wrapper = mount(OpenFileThumbnail, { props: { src: '/missing.jpg', alt: 'Upload', onError } })
+ await flushPromises()
+ const observer = observers.find(observer => observer.observe.mock.calls.some(([el]) => el === wrapper.element))!
+
+ await wrapper.trigger('error')
+ expect(onError).toHaveBeenCalledOnce()
+ wrapper.unmount()
+ expect(observer.disconnect).toHaveBeenCalled()
+ })
+
+ it('still displays images when IntersectionObserver is unavailable', async () => {
+ Reflect.deleteProperty(window, 'IntersectionObserver')
+ Reflect.deleteProperty(globalThis, 'IntersectionObserver')
+ const wrapper = mount(OpenFileThumbnail, { props: { src: '/test-image.jpg', alt: 'Upload' } })
+ await flushPromises()
+ expect(wrapper.attributes('src')).toBe('/test-image.jpg')
+ wrapper.unmount()
+ })
+})
diff --git a/client/test/nuxt/submission-file-link-renewal.spec.ts b/client/test/nuxt/submission-file-link-renewal.spec.ts
new file mode 100644
index 000000000..d43c1f56f
--- /dev/null
+++ b/client/test/nuxt/submission-file-link-renewal.spec.ts
@@ -0,0 +1,157 @@
+import { beforeEach, afterEach, describe, expect, it, vi } from 'vitest'
+import { flushPromises, mount } from '@vue/test-utils'
+import { ref } from 'vue'
+import FormSubmissions from '~/components/open/forms/components/FormSubmissions.vue'
+import OpenFile from '~/components/open/tables/components/OpenFile.vue'
+
+const query = vi.hoisted(() => ({ refetch: vi.fn() }))
+vi.mock('~/composables/query/forms/useFormSubmissions', () => ({
+ useFormSubmissions: () => ({ paginatedList: () => ({ refetch: query.refetch }) }),
+}))
+
+let observers: MockIntersectionObserver[]
+class MockIntersectionObserver {
+ target?: Element
+ callback: IntersectionObserverCallback
+ disconnect = vi.fn()
+ constructor(callback: IntersectionObserverCallback) {
+ this.callback = callback
+ observers.push(this)
+ }
+ observe(target: Element) { this.target = target }
+ intersect(isIntersecting: boolean) {
+ this.callback([{ isIntersecting } as IntersectionObserverEntry], this as unknown as IntersectionObserver)
+ }
+}
+
+beforeEach(() => {
+ observers = []
+ query.refetch.mockReset()
+ vi.stubGlobal('IntersectionObserver', MockIntersectionObserver)
+})
+afterEach(() => {
+ vi.unstubAllGlobals()
+ vi.restoreAllMocks()
+})
+
+function mountTable(expires = Math.floor(Date.now() / 1000) - 1) {
+ const files = ref([
+ { file_name: 'first.jpg', file_url: `/first.jpg?expires=${expires}` },
+ { file_name: 'second.jpg', file_url: `/second.jpg?expires=${expires}` },
+ ])
+ const wrapper = mount(FormSubmissions, {
+ props: { form: { id: 1 } },
+ global: {
+ stubs: {
+ DashboardLoadingBlock: { props: ['loading'], template: '<div><slot /></div>' },
+ USkeleton: true,
+ UBadge: { template: '<span><slot /></span>' },
+ UButton: true,
+ Icon: true,
+ OpenTable: { components: { OpenFile }, setup: () => ({ files }), template: '<OpenFile :value="files" />' },
+ UModal: true,
+ },
+ },
+ })
+ return { wrapper, files }
+}
+
+describe('expired submission image links', () => {
+ it('renews concurrent expired images once and loads the refreshed URLs', async () => {
+ let resolveRefresh!: (result: unknown) => void
+ query.refetch.mockImplementation(() => new Promise(resolve => { resolveRefresh = resolve }))
+ const { wrapper, files } = mountTable()
+ await flushPromises()
+ observers.forEach(observer => observer.intersect(true))
+ await flushPromises()
+ expect(query.refetch).toHaveBeenCalledTimes(1)
+ expect(query.refetch).toHaveBeenCalledWith({ cancelRefetch: false, throwOnError: true })
+ expect(wrapper.findAll('img[src]')).toHaveLength(0)
+
+ const oldObservers = [...observers]
+ const expires = Math.floor(Date.now() / 1000) + 600
+ files.value = files.value.map(file => ({ ...file, file_url: file.file_url.replace(/expires=\d+/, `expires=${expires}`) }))
+ resolveRefresh({ data: { data: [{ data: { images: files.value } }] } })
+ await flushPromises()
+ expect(oldObservers.every(observer => observer.disconnect.mock.calls.length > 0)).toBe(true)
+ observers.filter(observer => !oldObservers.includes(observer)).forEach(observer => observer.intersect(true))
+ await flushPromises()
+ expect(wrapper.findAll('img').map(image => image.attributes('src'))).toEqual(files.value.map(file => file.file_url))
+ wrapper.unmount()
+ })
+
+ it('trusts successfully refreshed links when the browser clock is ahead', async () => {
+ const { wrapper, files } = mountTable()
+ await flushPromises()
+ const expires = Math.floor(Date.now() / 1000) + 600
+ files.value = files.value.map(file => ({ ...file, file_url: file.file_url.replace(/expires=\d+/, `expires=${expires}`) }))
+ await flushPromises()
+ // The server considers these links fresh, but this browser thinks they expired.
+ const now = Date.now()
+ const clock = vi.spyOn(Date, 'now').mockReturnValue(now + 20 * 60 * 1000)
+ query.refetch.mockResolvedValue({ data: { data: [{ data: { images: files.value } }] } })
+ observers.forEach(observer => observer.intersect(true))
+ await flushPromises()
+ expect(query.refetch).toHaveBeenCalledTimes(1)
+ expect(wrapper.findAll('img').map(image => image.attributes('src'))).toEqual(files.value.map(file => file.file_url))
+ await Promise.all(wrapper.findAll('img').map(image => image.trigger('load')))
+ expect(observers.slice(-2).every(observer => observer.disconnect.mock.calls.length > 0)).toBe(true)
+ clock.mockRestore()
+ wrapper.unmount()
+ })
+
+ it('renews failed signed images with a behind-clock browser and bounds broken-file retries', async () => {
+ const now = Date.now()
+ vi.spyOn(Date, 'now').mockReturnValue(now - 20 * 60 * 1000)
+ await flushPromises()
+ const { wrapper, files } = mountTable(Math.floor(now / 1000) - 1)
+ await flushPromises()
+ const initialObservers = [...observers]
+ initialObservers.forEach(observer => observer.intersect(true))
+ await flushPromises()
+ // The browser thinks the expired links are still valid until the request fails.
+ expect(query.refetch).not.toHaveBeenCalled()
+ expect(wrapper.findAll('img[src]')).toHaveLength(2)
+ const renewedFiles = files.value.map(file => ({ ...file, file_url: file.file_url.replace(/expires=\d+/, `expires=${Math.floor(now / 1000) + 600}`) }))
+ query.refetch.mockImplementation(() => {
+ files.value = renewedFiles
+ return Promise.resolve({ data: { data: [{ data: { images: renewedFiles } }] } })
+ })
+ await wrapper.findAll('img')[0].trigger('error')
+ await flushPromises()
+ expect(query.refetch).toHaveBeenCalledTimes(1)
+ observers.filter(observer => !initialObservers.includes(observer)).forEach(observer => observer.intersect(true))
+ await flushPromises()
+ expect(wrapper.findAll('img').map(image => image.attributes('src'))).toEqual(renewedFiles.map(file => file.file_url))
+
+ // A genuinely missing file still uses the normal download fallback after renewal.
+ await wrapper.findAll('img')[0].trigger('error')
+ await flushPromises()
+ expect(query.refetch).toHaveBeenCalledTimes(1)
+ expect(wrapper.findAll('img')).toHaveLength(1)
+ expect(wrapper.find('a').attributes('href')).toBe(renewedFiles[0].file_url)
+ wrapper.unmount()
+ })
+
+ it('keeps expired sources unset after a renewal failure and retries on re-entry', async () => {
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
+ query.refetch.mockRejectedValue(new Error('Offline'))
+ const { wrapper } = mountTable()
+ await flushPromises()
+ observers.forEach(observer => observer.intersect(true))
+ await flushPromises()
+ expect(query.refetch).toHaveBeenCalledTimes(1)
+ expect(wrapper.findAll('img[src]')).toHaveLength(0)
+ expect(wrapper.findAll('button[aria-label^="Preview"]')).toHaveLength(2)
+ expect(observers.every(observer => observer.disconnect.mock.calls.length === 0)).toBe(true)
+
+ observers.forEach(observer => observer.intersect(false))
+ await flushPromises()
+ observers.forEach(observer => observer.intersect(true))
+ await flushPromises()
+ expect(query.refetch).toHaveBeenCalledTimes(2)
+ expect(wrapper.findAll('img[src]')).toHaveLength(0)
+ wrapper.unmount()
+ warn.mockRestore()
+ })
+})
diff --git a/docs/configuration/external-file-links.mdx b/docs/configuration/external-file-links.mdx
index 91479936f..ed3ee2577 100644
--- a/docs/configuration/external-file-links.mdx
+++ b/docs/configuration/external-file-links.mdx
@@ -60,6 +60,12 @@ appear with their filename instead of exposing the raw API URL.
## Download limits
+In submission tables, uploaded images load when their thumbnail first enters
+the visible area. Scroll vertically or horizontally to load more thumbnails.
+Click a thumbnail to open the full image preview.
+If you keep the table open until file links expire, links are renewed when
+you scroll to an image that has not loaded yet.
+
Signed submission-file downloads use a separate quota of 600 requests per minute
and 3,000 per hour for each client IP and form. Image thumbnails count toward this
quota, but do not consume the general API quota.