fix: 로그인 창을 처음 열 때 버벅이던 것 줄임 — 구글 로그인 스크립트는 한가할 때 미리 받고, 구글 버튼과 뒤 화면 멈추기는 창이 다 올라온 뒤에
This commit is contained in:
1 parent
7d84226105
commit
7dcd6c6a4c
5 files changed
+65
-36
No files matched your search
@@ -1,7 +1,7 @@
|
||||
import { useCallback, useEffect, useMemo, useState, type ReactNode } from 'react'
|
||||
import { NavLink, Outlet, useLocation, useNavigationType } from 'react-router'
|
||||
import { useIsFetching, useQuery } from '@tanstack/react-query'
|
||||
import { getSeasonWeekOptions } from '@nalssigak/shared/api/client/@tanstack/react-query.gen'
|
||||
import { useIsFetching, useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import { authConfigOptions, getSeasonWeekOptions } from '@nalssigak/shared/api/client/@tanstack/react-query.gen'
|
||||
import { IS_TOSS } from '../lib/platform'
|
||||
import { cx } from '../lib/utils'
|
||||
import { kindOfHour, kst, seasonOf, type TimeKind } from '@nalssigak/shared/lib'
|
||||
@@ -9,6 +9,7 @@ import './b.css'
|
||||
import { AuthProvider } from './auth'
|
||||
import { RegionProvider } from './region'
|
||||
import { useAuth } from './authContext'
|
||||
import { preloadGoogle } from './googleScript'
|
||||
import { useRegion } from './regionContext'
|
||||
import { useSheet } from './sheetContext'
|
||||
import { SheetProvider } from './Sheets'
|
||||
@@ -47,7 +48,16 @@ function TopBar() {
|
||||
const { region, locateStatus } = useRegion()
|
||||
const { open } = useSheet()
|
||||
const { me, mePending } = useAuth()
|
||||
const queryClient = useQueryClient()
|
||||
const tab = TABS.find((t) => t.to !== '/' && pathname.startsWith(t.to))
|
||||
const loggedOut = !IS_TOSS && !mePending && !me
|
||||
|
||||
// 로그인 창을 처음 열 때 버벅이지 않게 구글 로그인 준비물을 미리 받아 둔다
|
||||
useEffect(() => {
|
||||
if (!loggedOut) return
|
||||
void queryClient.prefetchQuery({ ...authConfigOptions(), staleTime: Infinity })
|
||||
return preloadGoogle()
|
||||
}, [loggedOut, queryClient])
|
||||
|
||||
return (
|
||||
<header className="sticky top-0 z-20 flex h-[calc(54px+env(safe-area-inset-top,0px))] items-center justify-between gap-2 px-4 pt-[env(safe-area-inset-top,0px)]">
|
||||
|
||||
@@ -34,7 +34,7 @@ import { ago, articleMeta, AVATAR_COLORS, cardBg, CHARACTERS, dateRange, loadOf,
|
||||
import { GoogleButton } from './googleLogin'
|
||||
import { useInstall, useInstallable } from './install'
|
||||
import { InstallSheet } from './installBanner'
|
||||
import { ARTICLE_CAT } from './lib'
|
||||
import { ARTICLE_CAT, SHEET_OPEN_MS } from './lib'
|
||||
import { MixSheet } from './myGak'
|
||||
import { ownPage, useRegion } from './regionContext'
|
||||
import { WeatherWhySheet } from './weatherWhy'
|
||||
@@ -109,8 +109,10 @@ function SheetHost({ state, onClose }: { state: SheetState; onClose: () => void
|
||||
const root = document.documentElement
|
||||
const before = root.style.overflow
|
||||
root.style.overflow = 'hidden'
|
||||
root.classList.add('b-sheet-open')
|
||||
// 뒤 화면 움직임 멈추기는 페이지 전체 스타일을 다시 계산하게 해서, 창이 다 올라온 뒤에 한다
|
||||
const pause = setTimeout(() => root.classList.add('b-sheet-open'), SHEET_OPEN_MS)
|
||||
return () => {
|
||||
clearTimeout(pause)
|
||||
root.style.overflow = before
|
||||
root.classList.remove('b-sheet-open')
|
||||
}
|
||||
|
||||
@@ -2,36 +2,8 @@ import { useMutation, useQuery } from '@tanstack/react-query'
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { authConfigOptions, googleLoginMutation } from '@nalssigak/shared/api/client/@tanstack/react-query.gen'
|
||||
import type { ResAuth } from '@nalssigak/shared/api/client/types.gen'
|
||||
|
||||
type GoogleId = {
|
||||
initialize: (o: { client_id: string; callback: (r: { credential?: string }) => void; ux_mode?: 'popup' }) => void
|
||||
renderButton: (el: HTMLElement, o: Record<string, string | number>) => void
|
||||
}
|
||||
declare global {
|
||||
interface Window {
|
||||
google?: { accounts: { id: GoogleId } }
|
||||
}
|
||||
}
|
||||
|
||||
const SCRIPT = 'https://accounts.google.com/gsi/client'
|
||||
let loading: Promise<GoogleId> | null = null
|
||||
|
||||
/** 구글 로그인 스크립트는 로그인 창을 처음 열 때 한 번만 받는다 */
|
||||
function loadGoogle(): Promise<GoogleId> {
|
||||
if (window.google?.accounts.id) return Promise.resolve(window.google.accounts.id)
|
||||
loading ??= new Promise((resolve, reject) => {
|
||||
const s = document.createElement('script')
|
||||
s.src = SCRIPT
|
||||
s.async = true
|
||||
s.onload = () => (window.google?.accounts.id ? resolve(window.google.accounts.id) : reject(new Error('gsi missing')))
|
||||
s.onerror = () => {
|
||||
loading = null
|
||||
reject(new Error('gsi load failed'))
|
||||
}
|
||||
document.head.appendChild(s)
|
||||
})
|
||||
return loading
|
||||
}
|
||||
import { loadGoogle } from './googleScript'
|
||||
import { SHEET_OPEN_MS } from './lib'
|
||||
|
||||
/** 구글로 계속하기 버튼. 서버에 클라이언트 ID가 없으면 아무것도 그리지 않는다 */
|
||||
export function GoogleButton({ onResult, onFail }: { onResult: (res: ResAuth) => void; onFail: () => void }) {
|
||||
@@ -49,8 +21,10 @@ export function GoogleButton({ onResult, onFail }: { onResult: (res: ResAuth) =>
|
||||
useEffect(() => {
|
||||
if (!clientId || !box.current) return
|
||||
let alive = true
|
||||
loadGoogle().then(
|
||||
(g) => {
|
||||
// 버튼(iframe)을 만드는 일이 무거워, 로그인 창이 다 올라온 뒤에 만든다. 자리는 min-h 로 잡아 둬서 밀리지 않는다
|
||||
const wait = new Promise((r) => setTimeout(r, SHEET_OPEN_MS))
|
||||
Promise.all([loadGoogle(), wait]).then(
|
||||
([g]) => {
|
||||
if (!alive || !box.current) return
|
||||
g.initialize({
|
||||
client_id: clientId,
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
export type GoogleId = {
|
||||
initialize: (o: { client_id: string; callback: (r: { credential?: string }) => void; ux_mode?: 'popup' }) => void
|
||||
renderButton: (el: HTMLElement, o: Record<string, string | number>) => void
|
||||
}
|
||||
declare global {
|
||||
interface Window {
|
||||
google?: { accounts: { id: GoogleId } }
|
||||
}
|
||||
}
|
||||
|
||||
const SCRIPT = 'https://accounts.google.com/gsi/client'
|
||||
let loading: Promise<GoogleId> | null = null
|
||||
|
||||
/** 구글 로그인 스크립트는 한 번만 받는다. 실패하면 다음에 다시 받는다 */
|
||||
export function loadGoogle(): Promise<GoogleId> {
|
||||
if (window.google?.accounts.id) return Promise.resolve(window.google.accounts.id)
|
||||
loading ??= new Promise((resolve, reject) => {
|
||||
const s = document.createElement('script')
|
||||
s.src = SCRIPT
|
||||
s.async = true
|
||||
s.onload = () => (window.google?.accounts.id ? resolve(window.google.accounts.id) : reject(new Error('gsi missing')))
|
||||
s.onerror = () => {
|
||||
loading = null
|
||||
reject(new Error('gsi load failed'))
|
||||
}
|
||||
document.head.appendChild(s)
|
||||
})
|
||||
return loading
|
||||
}
|
||||
|
||||
/** 로그인 안 한 사람은 화면이 한가할 때 스크립트를 미리 받아 둔다. 로그인 창이 올라오는 도중에 스크립트를 실행하느라 화면이 멈추지 않게. 돌려준 함수로 취소 */
|
||||
export function preloadGoogle(): () => void {
|
||||
const run = () => void loadGoogle().catch((e: unknown) => console.error('[google] preload', e))
|
||||
if ('requestIdleCallback' in window) {
|
||||
const id = requestIdleCallback(run, { timeout: 4000 })
|
||||
return () => cancelIdleCallback(id)
|
||||
}
|
||||
const id = setTimeout(run, 2500)
|
||||
return () => clearTimeout(id)
|
||||
}
|
||||
@@ -14,3 +14,6 @@ export function iconSrc(name: string) {
|
||||
export const fall = (d: string, f = '4.8s') => ({ '--d': d, '--f': f }) as CSSProperties
|
||||
|
||||
export const ARTICLE_CAT: Record<number, [string, string]> = { 1: [ARTICLE_LABEL[1], 'text-[#1f9d62]'], 2: [ARTICLE_LABEL[2], 'text-[#7a5cd6]'], 3: [ARTICLE_LABEL[3], 'text-(--orange)'] }
|
||||
|
||||
/** 아래 창이 다 올라오는 시간(.b-sheet 0.26s). 무거운 일은 이 뒤로 미뤄 올라오는 동안 버벅이지 않게 */
|
||||
export const SHEET_OPEN_MS = 280
|
||||
Reference in new issue
Block a user