Skip to content

Commit 5741e70

Browse files
committed
LazyImage on Safari (#5626)
This pull request improves the lazy loading behavior and caching strategy for images in the `LazyImage.vue` component. The most significant changes are focused on optimizing image rendering and resource management, as well as improving code clarity. **Lazy loading behavior improvements:** * Changed the `<img>` element to render only when `cachedSrc` is available, ensuring that images are not displayed before they are ready. * Updated watchers in `LazyImage.vue` to use clearer variable names (`shouldLoadVal` instead of `shouldLoad`) for better readability and maintainability. [[1]](diffhunk://#diff-3a1bfa7eb8cb26b04bea73f7b4b4e3c01e9d20a7eba6c3738fb47f96da1a7c95L80-R81) [[2]](diffhunk://#diff-3a1bfa7eb8cb26b04bea73f7b4b4e3c01e9d20a7eba6c3738fb47f96da1a7c95L96-R96) **Caching strategy enhancement:** * Modified the `fetch` call in `mediaCacheService.ts` to use `{ cache: 'force-cache' }`, which leverages the browser's cache more aggressively when loading media, potentially improving performance and reducing network requests. ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-5626-LazyImage-on-Safari-2716d73d365081eeb1d3c2a96be4d408) by [Unito](https://www.unito.io)
1 parent d7a59f4 commit 5741e70

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/components/common/LazyImage.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
class="absolute inset-0"
1111
/>
1212
<img
13-
v-show="isImageLoaded"
13+
v-if="cachedSrc"
1414
ref="imageRef"
1515
:src="cachedSrc"
1616
:alt="alt"
@@ -77,8 +77,8 @@ const shouldLoad = computed(() => isIntersecting.value)
7777
7878
watch(
7979
shouldLoad,
80-
async (shouldLoad) => {
81-
if (shouldLoad && src && !cachedSrc.value && !hasError.value) {
80+
async (shouldLoadVal) => {
81+
if (shouldLoadVal && src && !cachedSrc.value && !hasError.value) {
8282
try {
8383
const cachedMedia = await getCachedMedia(src)
8484
if (cachedMedia.error) {
@@ -93,7 +93,7 @@ watch(
9393
console.warn('Failed to load cached media:', error)
9494
cachedSrc.value = src
9595
}
96-
} else if (!shouldLoad) {
96+
} else if (!shouldLoadVal) {
9797
if (cachedSrc.value?.startsWith('blob:')) {
9898
releaseUrl(src)
9999
}

src/services/mediaCacheService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class MediaCacheService {
113113

114114
try {
115115
// Fetch the media
116-
const response = await fetch(src)
116+
const response = await fetch(src, { cache: 'force-cache' })
117117
if (!response.ok) {
118118
throw new Error(`Failed to fetch: ${response.status}`)
119119
}

0 commit comments

Comments
 (0)