Skip to content

Commit

Permalink
Merge pull request #1457 from nextcloud/poc-cache-hasavatar
Browse files Browse the repository at this point in the history
Cache hasAvatar info in sessionStorage to avoid flicker
  • Loading branch information
PVince81 authored Oct 19, 2020
2 parents 8c7576c + 7613d44 commit 141a716
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"dependencies": {
"@nextcloud/auth": "^1.2.3",
"@nextcloud/axios": "^1.3.2",
"@nextcloud/browser-storage": "^0.1.1",
"@nextcloud/capabilities": "^1.0.2",
"@nextcloud/dialogs": "^3.0.0",
"@nextcloud/event-bus": "^1.1.4",
Expand Down
32 changes: 32 additions & 0 deletions src/components/Avatar/Avatar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
</template>

<script>
import { getBuilder } from '@nextcloud/browser-storage'
import { directive as ClickOutside } from 'v-click-outside'
import PopoverMenu from '../PopoverMenu'
import { getCurrentUser } from '@nextcloud/auth'
Expand All @@ -100,6 +101,20 @@ import Tooltip from '../../directives/Tooltip'
import usernameToColor from '../../functions/usernameToColor'
import { userStatus } from '../../mixins'
const browserStorage = getBuilder('nextcloud').persist().build()
function getUserHasAvatar(userId) {
const flag = browserStorage.getItem('user-has-avatar.' + userId)
if (typeof flag === 'string') {
return Boolean(flag)
}
return null
}
function setUserHasAvatar(userId, flag) {
browserStorage.setItem('user-has-avatar.' + userId, flag)
}
export default {
name: 'Avatar',
directives: {
Expand Down Expand Up @@ -476,17 +491,34 @@ export default {
urlGenerator(this.user, this.size * 4) + ' 4x',
].join(', ')
// skip loading
const userHasAvatar = getUserHasAvatar(this.user)
if (typeof userHasAvatar === 'boolean') {
this.isAvatarLoaded = true
this.avatarUrlLoaded = avatarUrl
if (!this.isUrlDefined) {
this.avatarSrcSetLoaded = srcset
}
if (userHasAvatar === false) {
this.userDoesNotExist = true
}
return
}
const img = new Image()
img.onload = () => {
this.avatarUrlLoaded = avatarUrl
if (!this.isUrlDefined) {
this.avatarSrcSetLoaded = srcset
}
this.isAvatarLoaded = true
// re-get to avoid concurrent access
setUserHasAvatar(this.user, true)
}
img.onerror = () => {
this.userDoesNotExist = true
this.isAvatarLoaded = true
setUserHasAvatar(this.user, false)
}
if (!this.isUrlDefined) {
Expand Down

0 comments on commit 141a716

Please sign in to comment.