Skip to content

Commit 141a716

Browse files
authoredOct 19, 2020
Merge pull request #1457 from nextcloud/poc-cache-hasavatar
Cache hasAvatar info in sessionStorage to avoid flicker
2 parents 8c7576c + 7613d44 commit 141a716

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed
 

‎package-lock.json

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"dependencies": {
3939
"@nextcloud/auth": "^1.2.3",
4040
"@nextcloud/axios": "^1.3.2",
41+
"@nextcloud/browser-storage": "^0.1.1",
4142
"@nextcloud/capabilities": "^1.0.2",
4243
"@nextcloud/dialogs": "^3.0.0",
4344
"@nextcloud/event-bus": "^1.1.4",

‎src/components/Avatar/Avatar.vue

+32
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
</template>
9191

9292
<script>
93+
import { getBuilder } from '@nextcloud/browser-storage'
9394
import { directive as ClickOutside } from 'v-click-outside'
9495
import PopoverMenu from '../PopoverMenu'
9596
import { getCurrentUser } from '@nextcloud/auth'
@@ -100,6 +101,20 @@ import Tooltip from '../../directives/Tooltip'
100101
import usernameToColor from '../../functions/usernameToColor'
101102
import { userStatus } from '../../mixins'
102103

104+
const browserStorage = getBuilder('nextcloud').persist().build()
105+
106+
function getUserHasAvatar(userId) {
107+
const flag = browserStorage.getItem('user-has-avatar.' + userId)
108+
if (typeof flag === 'string') {
109+
return Boolean(flag)
110+
}
111+
return null
112+
}
113+
114+
function setUserHasAvatar(userId, flag) {
115+
browserStorage.setItem('user-has-avatar.' + userId, flag)
116+
}
117+
103118
export default {
104119
name: 'Avatar',
105120
directives: {
@@ -476,17 +491,34 @@ export default {
476491
urlGenerator(this.user, this.size * 4) + ' 4x',
477492
].join(', ')
478493

494+
// skip loading
495+
const userHasAvatar = getUserHasAvatar(this.user)
496+
if (typeof userHasAvatar === 'boolean') {
497+
this.isAvatarLoaded = true
498+
this.avatarUrlLoaded = avatarUrl
499+
if (!this.isUrlDefined) {
500+
this.avatarSrcSetLoaded = srcset
501+
}
502+
if (userHasAvatar === false) {
503+
this.userDoesNotExist = true
504+
}
505+
return
506+
}
507+
479508
const img = new Image()
480509
img.onload = () => {
481510
this.avatarUrlLoaded = avatarUrl
482511
if (!this.isUrlDefined) {
483512
this.avatarSrcSetLoaded = srcset
484513
}
485514
this.isAvatarLoaded = true
515+
// re-get to avoid concurrent access
516+
setUserHasAvatar(this.user, true)
486517
}
487518
img.onerror = () => {
488519
this.userDoesNotExist = true
489520
this.isAvatarLoaded = true
521+
setUserHasAvatar(this.user, false)
490522
}
491523

492524
if (!this.isUrlDefined) {

0 commit comments

Comments
 (0)
Please sign in to comment.