Skip to content

Commit

Permalink
feat(post media): cancel media request when unloaded
Browse files Browse the repository at this point in the history
  • Loading branch information
AlejandroAkbal committed Mar 15, 2024
1 parent 7493fa4 commit 6550fd9
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions components/pages/posts/post/PostMedia.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
const props = defineProps<PostMediaProps>()
const mediaElement = ref<HTMLElement | null>(null)
const localSrc = shallowRef(props.mediaSrc)
const localPosterSrc = shallowRef(props.mediaPosterSrc)
Expand All @@ -28,11 +30,36 @@
const triedToLoadWithProxy = shallowRef(false)
onBeforeUnmount(() => {
let finalMediaElement = mediaElement.value
// If its a Vue component, get the actual element
if (finalMediaElement?.$el) {
finalMediaElement = finalMediaElement.$el
}
// If its a picture, get the img element
if (finalMediaElement.tagName === 'PICTURE') {
finalMediaElement = finalMediaElement.querySelector('img') as HTMLImageElement
}
// Cancel any pending media requests - https://stackoverflow.com/a/28060352
finalMediaElement?.removeAttribute('src')
if (isVideo.value) {
finalMediaElement?.load()
}
})
function onMediaError(event: Event) {
if (hasError.value) {
return
}
if (!event.target?.src) {
return
}
// Proxy videos, images are already proxied
if (isVideo.value && !triedToLoadWithProxy.value && isPremium.value) {
triedToLoadWithProxy.value = true
Expand Down Expand Up @@ -144,6 +171,7 @@
<!-- Fix(rounded borders): add the same rounded borders that the parent has -->
<template v-if="!isPremium">
<img
ref="mediaElement"
:alt="mediaAlt"
:src="localSrc"
:height="mediaSrcHeight"
Expand All @@ -161,6 +189,7 @@
<template v-else>
<!-- Fix(rounded borders): add the same rounded borders that the parent has -->
<NuxtPicture
ref="mediaElement"
:alt="mediaAlt"
:src="localSrc"
:height="mediaSrcHeight"
Expand All @@ -182,6 +211,7 @@
<!-- TODO: Add load animation -->
<!-- Fix(rounded borders): add the same rounded borders that the parent has -->
<video
ref="mediaElement"
v-intersection-observer="[onVideoIntersectionObserver, { rootMargin: '100px' }]"
:src="localSrc"
:height="mediaSrcHeight"
Expand Down

0 comments on commit 6550fd9

Please sign in to comment.