perf: fetch instance info only once

This commit is contained in:
三咲智子 Kevin Deng 2023-01-13 00:01:18 +08:00
parent ad70d9c475
commit d0d0503a13
No known key found for this signature in database
GPG key ID: 69992F2250DFD93E
3 changed files with 15 additions and 11 deletions

View file

@ -1,4 +1,4 @@
import { login as loginMasto } from 'masto' import { createClient, fetchV1Instance } from 'masto'
import type { mastodon } from 'masto' import type { mastodon } from 'masto'
import type { Ref } from 'vue' import type { Ref } from 'vue'
import type { MaybeComputedRef, RemovableRef } from '@vueuse/core' import type { MaybeComputedRef, RemovableRef } from '@vueuse/core'
@ -106,22 +106,26 @@ async function loginTo(user?: Omit<UserLogin, 'account'> & { account?: mastodon.
const route = useRoute() const route = useRoute()
const router = useRouter() const router = useRouter()
const server = user?.server || route.params.server as string || publicServer.value const server = user?.server || route.params.server as string || publicServer.value
const masto = await loginMasto({ const url = `https://${server}`
url: `https://${server}`, const instance = await fetchV1Instance({
url,
})
const masto = await createClient({
url,
streamingApiUrl: instance.urls.streamingApi,
accessToken: user?.token, accessToken: user?.token,
disableVersionCheck: true, disableVersionCheck: true,
}) })
if (!user?.token) { if (!user?.token) {
publicServer.value = server publicServer.value = server
publicInstance.value = await masto.v1.instances.fetch() publicInstance.value = instance
} }
else { else {
try { try {
const [me, instance, pushSubscription] = await Promise.all([ const [me, pushSubscription] = await Promise.all([
masto.v1.accounts.verifyCredentials(), masto.v1.accounts.verifyCredentials(),
masto.v1.instances.fetch(),
// if PWA is not enabled, don't get push subscription // if PWA is not enabled, don't get push subscription
useRuntimeConfig().public.pwaEnabled useRuntimeConfig().public.pwaEnabled
// we get 404 response instead empty data // we get 404 response instead empty data

View file

@ -53,7 +53,7 @@
"iso-639-1": "^2.1.15", "iso-639-1": "^2.1.15",
"js-yaml": "^4.1.0", "js-yaml": "^4.1.0",
"lru-cache": "^7.14.1", "lru-cache": "^7.14.1",
"masto": "^5.2.0", "masto": "^5.3.0",
"pinia": "^2.0.28", "pinia": "^2.0.28",
"shiki": "^0.12.1", "shiki": "^0.12.1",
"shiki-es": "^0.1.2", "shiki-es": "^0.1.2",

View file

@ -94,7 +94,7 @@ importers:
jsdom: ^21.0.0 jsdom: ^21.0.0
lint-staged: ^13.1.0 lint-staged: ^13.1.0
lru-cache: ^7.14.1 lru-cache: ^7.14.1
masto: ^5.2.0 masto: ^5.3.0
nuxt: ^3.0.0 nuxt: ^3.0.0
pinia: ^2.0.28 pinia: ^2.0.28
postcss-nested: ^6.0.0 postcss-nested: ^6.0.0
@ -152,7 +152,7 @@ importers:
iso-639-1: 2.1.15 iso-639-1: 2.1.15
js-yaml: 4.1.0 js-yaml: 4.1.0
lru-cache: 7.14.1 lru-cache: 7.14.1
masto: 5.2.0 masto: 5.3.0
pinia: 2.0.28_typescript@4.9.4 pinia: 2.0.28_typescript@4.9.4
shiki: 0.12.1 shiki: 0.12.1
shiki-es: 0.1.2 shiki-es: 0.1.2
@ -8049,8 +8049,8 @@ packages:
resolution: {integrity: sha512-Z1NL3Tb1M9wH4XESsCDEksWoKTdlUafKc4pt0GRwjUyXaCFZ+dc3g2erqB6zm3szA2IUSi7VnPI+o/9jnxh9hw==} resolution: {integrity: sha512-Z1NL3Tb1M9wH4XESsCDEksWoKTdlUafKc4pt0GRwjUyXaCFZ+dc3g2erqB6zm3szA2IUSi7VnPI+o/9jnxh9hw==}
dev: true dev: true
/masto/5.2.0: /masto/5.3.0:
resolution: {integrity: sha512-71pKUdPZDuQSPJqRJSwsZ/QqvO/arOkdRyXEyOdeU9QukQ30OEW1M9BCGBp/b9Lbf6R7d9oCCdefmxXCSAzlFg==} resolution: {integrity: sha512-gArycufHuEEQ6WCvWNmiIQq+AIAo2grWQwO7mpenNKb0wFKYSaYTeMthx0t+Ircso+vBtsXdbjGMfzZeIttlow==}
dependencies: dependencies:
'@mastojs/ponyfills': 1.0.4 '@mastojs/ponyfills': 1.0.4
change-case: 4.1.2 change-case: 4.1.2