Skip to content

Commit

Permalink
fix: remove flashing text on page reload (#1939)
Browse files Browse the repository at this point in the history
  • Loading branch information
userquin committed Apr 16, 2023
1 parent 1358132 commit c712593
Show file tree
Hide file tree
Showing 43 changed files with 88 additions and 61 deletions.
2 changes: 1 addition & 1 deletion components/nav/NavTitle.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ router.afterEach(() => {
@click.prevent="onClickLogo"
>
<NavLogo shrink-0 aspect="1/1" sm:h-8 xl:h-10 class="rtl-flip" />
<div hidden xl:block text-secondary>
<div v-show="isHydrated" hidden xl:block text-secondary>
{{ $t('app_name') }} <sup text-sm italic mt-1>{{ env === 'release' ? 'alpha' : env }}</sup>
</div>
</NuxtLink>
Expand Down
2 changes: 1 addition & 1 deletion components/status/StatusDetails.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const createdAt = useFormattedDateTime(status.createdAt)
const { t } = useI18n()
useHead({
useHydratedHead({
title: () => `${getDisplayName(status.account)} ${t('common.in')} ${t('app_name')}: "${removeHTMLTags(status.content) || ''}"`,
})
</script>
Expand Down
2 changes: 1 addition & 1 deletion composables/setups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function setupPageHeader() {
return acc
}, {} as Record<string, Directions>)

useHead({
useHydratedHead({
htmlAttrs: {
lang: () => locale.value,
dir: () => localeMap[locale.value] ?? 'ltr',
Expand Down
24 changes: 24 additions & 0 deletions composables/vue.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { ComponentInternalInstance } from 'vue'
import { onActivated, onDeactivated, ref } from 'vue'
import type { ActiveHeadEntry, HeadEntryOptions, UseHeadInput } from '@vueuse/head'
import type { SchemaAugmentations } from '@unhead/schema'

export const isHydrated = ref(false)

Expand Down Expand Up @@ -34,3 +36,25 @@ export function onReactivated(hook: Function, target?: ComponentInternalInstance
}, target)
onDeactivated(() => initial.value = false)
}

export function useHydratedHead<T extends SchemaAugmentations>(input: UseHeadInput<T>, options?: HeadEntryOptions): ActiveHeadEntry<UseHeadInput<T>> | void {
if (input && typeof input === 'object' && !('value' in input)) {
const title = 'title' in input ? input.title : undefined
if (process.server && title) {
input.meta = input.meta || []
if (Array.isArray(input.meta)) {
input.meta.push(
{ property: 'og:title', content: (typeof input.title === 'function' ? input.title() : input.title) as string },
)
}
}
else if (title) {
(input as any).title = () => isHydrated.value ? typeof title === 'function' ? title() : title : ''
}
}
return useHead(() => {
if (!isHydrated.value)
return {}
return resolveUnref(input)
}, options)
}
2 changes: 1 addition & 1 deletion pages/[[server]]/@[account]/index/followers.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const paginator = account ? useMastoClient().v1.accounts.listFollowers(account.i
const isSelf = useSelfAccount(account)
if (account) {
useHead({
useHydratedHead({
title: () => `${t('account.followers')} | ${getDisplayName(account)} (@${account.acct})`,
})
}
Expand Down
2 changes: 1 addition & 1 deletion pages/[[server]]/@[account]/index/following.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const paginator = account ? useMastoClient().v1.accounts.listFollowing(account.i
const isSelf = useSelfAccount(account)
if (account) {
useHead({
useHydratedHead({
title: () => `${t('account.following')} | ${getDisplayName(account)} (@${account.acct})`,
})
}
Expand Down
2 changes: 1 addition & 1 deletion pages/[[server]]/@[account]/index/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function reorderAndFilter(items: mastodon.v1.Status[]) {
const paginator = useMastoClient().v1.accounts.listStatuses(account.id, { limit: 30, excludeReplies: true })
if (account) {
useHead({
useHydratedHead({
title: () => `${t('account.posts')} | ${getDisplayName(account)} (@${account.acct})`,
})
}
Expand Down
2 changes: 1 addition & 1 deletion pages/[[server]]/@[account]/index/media.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const account = await fetchAccountByHandle(handle)
const paginator = useMastoClient().v1.accounts.listStatuses(account.id, { onlyMedia: true, excludeReplies: false })
if (account) {
useHead({
useHydratedHead({
title: () => `${t('tab.media')} | ${getDisplayName(account)} (@${account.acct})`,
})
}
Expand Down
2 changes: 1 addition & 1 deletion pages/[[server]]/@[account]/index/with_replies.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const account = await fetchAccountByHandle(handle)
const paginator = useMastoClient().v1.accounts.listStatuses(account.id, { excludeReplies: false })
if (account) {
useHead({
useHydratedHead({
title: () => `${t('tab.posts_with_replies')} | ${getDisplayName(account)} (@${account.acct})`,
})
}
Expand Down
2 changes: 1 addition & 1 deletion pages/[[server]]/explore/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const paginator = useMastoClient().v1.trends.listStatuses()
const hideNewsTips = useLocalStorage(STORAGE_KEY_HIDE_EXPLORE_POSTS_TIPS, false)
useHead({
useHydratedHead({
title: () => `${t('tab.posts')} | ${t('nav.explore')}`,
})
</script>
Expand Down
2 changes: 1 addition & 1 deletion pages/[[server]]/explore/links.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const paginator = useMastoClient().v1.trends.listLinks()
const hideNewsTips = useLocalStorage(STORAGE_KEY_HIDE_EXPLORE_NEWS_TIPS, false)
useHead({
useHydratedHead({
title: () => `${t('tab.news')} | ${t('nav.explore')}`,
})
</script>
Expand Down
2 changes: 1 addition & 1 deletion pages/[[server]]/explore/tags.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const paginator = client.v1.trends.listTags({
const hideTagsTips = useLocalStorage(STORAGE_KEY_HIDE_EXPLORE_TAGS_TIPS, false)
useHead({
useHydratedHead({
title: () => `${t('tab.hashtags')} | ${t('nav.explore')}`,
})
</script>
Expand Down
2 changes: 1 addition & 1 deletion pages/[[server]]/explore/users.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { t } = useI18n()
// limit: 20 is the default configuration of the official client
const paginator = useMastoClient().v2.suggestions.list({ limit: 20 })
useHead({
useHydratedHead({
title: () => `${t('tab.for_you')} | ${t('nav.explore')}`,
})
</script>
Expand Down
2 changes: 1 addition & 1 deletion pages/[[server]]/list/[list]/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const { client } = $(useMasto())
const { data: listInfo, refresh } = $(await useAsyncData(() => client.v1.lists.fetch(list), { default: () => shallowRef() }))
if (listInfo) {
useHead({
useHydratedHead({
title: () => `${listInfo.title} | ${route.fullPath.endsWith('/accounts') ? t('tab.accounts') : t('tab.posts')} | ${t('nav.lists')}`,
})
}
Expand Down
2 changes: 1 addition & 1 deletion pages/[[server]]/lists.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const client = useMastoClient()
const paginator = client.v1.lists.list()
useHead({
useHydratedHead({
title: () => t('nav.lists'),
})
Expand Down
2 changes: 1 addition & 1 deletion pages/[[server]]/public/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const { t } = useI18n()
useHead({
useHydratedHead({
title: () => t('title.federated_timeline'),
})
</script>
Expand Down
2 changes: 1 addition & 1 deletion pages/[[server]]/public/local.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const { t } = useI18n()
useHead({
useHydratedHead({
title: () => t('title.local_timeline'),
})
</script>
Expand Down
2 changes: 1 addition & 1 deletion pages/[[server]]/tags/[tag].vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const paginator = client.v1.timelines.listHashtag(tagName)
const stream = useStreaming(client => client.v1.stream.streamTagTimeline(tagName))
if (tag) {
useHead({
useHydratedHead({
title: () => `#${tag.name}`,
})
}
Expand Down
2 changes: 1 addition & 1 deletion pages/blocks.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ definePageMeta({
const { t } = useI18n()
useHead({
useHydratedHead({
title: () => t('nav.blocked_users'),
})
</script>
Expand Down
2 changes: 1 addition & 1 deletion pages/bookmarks.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ definePageMeta({
const { t } = useI18n()
useHead({
useHydratedHead({
title: () => t('nav.bookmarks'),
})
</script>
Expand Down
2 changes: 1 addition & 1 deletion pages/compose.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
const { t } = useI18n()
useHead({
useHydratedHead({
title: () => t('nav.compose'),
})
</script>
Expand Down
2 changes: 1 addition & 1 deletion pages/conversations.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ definePageMeta({
const { t } = useI18n()
useHead({
useHydratedHead({
title: () => t('nav.conversations'),
})
</script>
Expand Down
2 changes: 1 addition & 1 deletion pages/domain_blocks.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ definePageMeta({
const { t } = useI18n()
useHead({
useHydratedHead({
title: () => t('nav.blocked_domains'),
})
</script>
Expand Down
2 changes: 1 addition & 1 deletion pages/favourites.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ definePageMeta({
const { t } = useI18n()
useHead({
useHydratedHead({
title: () => t('nav.favourites'),
})
</script>
Expand Down
2 changes: 1 addition & 1 deletion pages/home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if (process.client && route.path === '/signin/callback')
router.push('/home')
const { t } = useI18n()
useHead({
useHydratedHead({
title: () => t('nav.home'),
})
</script>
Expand Down
2 changes: 1 addition & 1 deletion pages/mutes.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ definePageMeta({
const { t } = useI18n()
useHead({
useHydratedHead({
title: () => t('nav.muted_users'),
})
</script>
Expand Down
2 changes: 1 addition & 1 deletion pages/notifications/index.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
const { t } = useI18n()
useHead({
useHydratedHead({
title: () => `${t('tab.notifications_all')} | ${t('nav.notifications')}`,
})
</script>
Expand Down
2 changes: 1 addition & 1 deletion pages/notifications/mention.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script setup lang="ts">
const { t } = useI18n()
useHead({
useHydratedHead({
title: () => `${t('tab.notifications_mention')} | ${t('nav.notifications')}`,
})
</script>
Expand Down
2 changes: 1 addition & 1 deletion pages/pinned.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ definePageMeta({
const { t } = useI18n()
useHead({
useHydratedHead({
title: () => t('account.pinned'),
})
</script>
Expand Down
16 changes: 8 additions & 8 deletions pages/settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ definePageMeta({
const { t } = useI18n()
useHead({
useHydratedHead({
title: () => t('nav.settings'),
})
Expand All @@ -22,12 +22,12 @@ const isRootPath = computedEager(() => route.name === 'settings')
<template #title>
<div timeline-title-style flex items-center gap-2 @click="$scrollToTop">
<div i-ri:settings-3-line />
<span>{{ $t('nav.settings') }}</span>
<span>{{ isHydrated ? $t('nav.settings') : '' }}</span>
</div>
</template>
<div xl:w-97 lg:w-78 w-full>
<SettingsItem
v-if="isHydrated && currentUser "
v-if="isHydrated && currentUser"
command
icon="i-ri:user-line"
:text="$t('settings.profile.label')"
Expand All @@ -37,7 +37,7 @@ const isRootPath = computedEager(() => route.name === 'settings')
<SettingsItem
command
icon="i-ri-compasses-2-line"
:text="$t('settings.interface.label')"
:text="isHydrated ? $t('settings.interface.label') : ''"
to="/settings/interface"
:match="$route.path.startsWith('/settings/interface/')"
/>
Expand All @@ -52,28 +52,28 @@ const isRootPath = computedEager(() => route.name === 'settings')
<SettingsItem
command
icon="i-ri-globe-line"
:text="$t('settings.language.label')"
:text="isHydrated ? $t('settings.language.label') : ''"
to="/settings/language"
:match="$route.path.startsWith('/settings/language/')"
/>
<SettingsItem
command
icon="i-ri-equalizer-line"
:text="$t('settings.preferences.label')"
:text="isHydrated ? $t('settings.preferences.label') : ''"
to="/settings/preferences"
:match="$route.path.startsWith('/settings/preferences/')"
/>
<SettingsItem
command
icon="i-ri-group-line"
:text="$t('settings.users.label')"
:text="isHydrated ? $t('settings.users.label') : ''"
to="/settings/users"
:match="$route.path.startsWith('/settings/users/')"
/>
<SettingsItem
command
icon="i-ri:information-line"
:text="$t('settings.about.label')"
:text="isHydrated ? $t('settings.about.label') : ''"
to="/settings/about"
:match="$route.path.startsWith('/settings/about/')"
/>
Expand Down
2 changes: 1 addition & 1 deletion pages/settings/about/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const buildInfo = useBuildInfo()
const { t } = useI18n()
useHead({
useHydratedHead({
title: () => `${t('settings.about.label')} | ${t('nav.settings')}`,
})
Expand Down
2 changes: 1 addition & 1 deletion pages/settings/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div min-h-screen flex justify-center items-center>
<div text-center flex="~ col gap-2" items-center>
<div i-ri:settings-3-line text-5xl />
<span text-xl>{{ $t('settings.select_a_settings') }}</span>
<span text-xl>{{ isHydrated ? $t('settings.select_a_settings') : '' }}</span>
</div>
</div>
</template>
2 changes: 1 addition & 1 deletion pages/settings/interface/index.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
const { t } = useI18n()
useHead({
useHydratedHead({
title: () => `${t('settings.interface.label')} | ${t('nav.settings')}`,
})
</script>
Expand Down
2 changes: 1 addition & 1 deletion pages/settings/language/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { t, locale } = useI18n()
const translationStatus: ElkTranslationStatus = await import('~/elk-translation-status.json').then(m => m.default)
useHead({
useHydratedHead({
title: () => `${t('settings.language.label')} | ${t('nav.settings')}`,
})
const status = computed(() => {
Expand Down
Loading

0 comments on commit c712593

Please sign in to comment.