fix: 홈 첫 로드 가볍게 — 쇼츠 유튜브 플레이어를 첫 조작 뒤에 불러오고 첫 표지는 먼저 받음(LCP), 아이콘은 WebP 사본(4.6MB → 0.8MB)·계절 사진 재압축, 쇼츠_미리보기 이벤트 추가
This commit is contained in:
1 parent
6383911fb3
commit
38ae9374fd
174 files changed
+90
-10
No files matched your search
@@ -75,11 +75,35 @@ function useVisible<T extends Element>(threshold = 0.5) {
|
||||
return [ref, visible] as const
|
||||
}
|
||||
|
||||
/** 오늘 화면 히어로 아래 쇼츠 줄: 첫 카드만 소리 없이 재생, 누르면 쇼츠 페이지(/shorts/영상ID)에서 위아래로 넘겨 본다 */
|
||||
// scroll 은 넣지 않는다: 브라우저가 스크롤 위치를 되돌릴 때도 나서 조작으로 볼 수 없다
|
||||
const TOUCH_EVENTS = ['pointerdown', 'touchstart', 'wheel', 'keydown', 'mousemove'] as const
|
||||
// 한 번 조작했으면 다른 화면에 다녀와도 그대로 둔다
|
||||
let touchedOnce = false
|
||||
|
||||
/** 사용자가 화면을 처음 조작(누르기·굴리기·키)한 뒤부터 true. 유튜브 플레이어(약 850KB)를 첫 화면 로드에서 빼려고 쓴다 */
|
||||
function useTouched() {
|
||||
const [touched, setTouched] = useState(touchedOnce)
|
||||
useEffect(() => {
|
||||
if (touchedOnce) return
|
||||
const opts = { capture: true, passive: true }
|
||||
const off = () => TOUCH_EVENTS.forEach((t) => window.removeEventListener(t, on, opts))
|
||||
const on = () => {
|
||||
touchedOnce = true
|
||||
setTouched(true)
|
||||
off()
|
||||
}
|
||||
TOUCH_EVENTS.forEach((t) => window.addEventListener(t, on, opts))
|
||||
return off
|
||||
}, [])
|
||||
return touched
|
||||
}
|
||||
|
||||
/** 오늘 화면 히어로 아래 쇼츠 줄: 첫 조작 뒤 첫 카드만 소리 없이 재생, 누르면 쇼츠 페이지(/shorts/영상ID)에서 위아래로 넘겨 본다 */
|
||||
export function ShortsRail() {
|
||||
const q = useQuery(listShortsOptions())
|
||||
const items = q.data?.items ?? []
|
||||
const [ref, visible] = useVisible<HTMLElement>()
|
||||
const touched = useTouched()
|
||||
|
||||
if (q.isPending) {
|
||||
return (
|
||||
@@ -99,33 +123,40 @@ export function ShortsRail() {
|
||||
<Rail label="쇼츠 목록">
|
||||
{items.map((s, i) => (
|
||||
// 토스 앱 웹뷰는 유튜브 플레이어가 무거워, 줄에선 표지만 보이고 누르면 재생
|
||||
<ShortCard key={s.id} s={s} playing={!IS_TOSS && i === 0 && visible} />
|
||||
<ShortCard key={s.id} s={s} first={i === 0} playing={!IS_TOSS && i === 0 && visible && touched} />
|
||||
))}
|
||||
</Rail>
|
||||
</section>
|
||||
)
|
||||
}
|
||||
|
||||
function ShortCard({ s, playing }: { s: ShortItem; playing: boolean }) {
|
||||
function ShortCard({ s, first, playing }: { s: ShortItem; first: boolean; playing: boolean }) {
|
||||
// 재생이 시작된 뒤에만 플레이어를 보인다: 자동재생이 막히면(아이폰 절전 등) 유튜브 재생 버튼 대신 표지 그대로
|
||||
const [started, setStarted] = useState(false)
|
||||
const frame = useRef<HTMLIFrameElement>(null)
|
||||
// 플레이어는 반복 재생하며 재생 상태를 여러 번 보내므로 미리보기 이벤트는 한 번만
|
||||
const previewSent = useRef(false)
|
||||
useEffect(() => {
|
||||
if (!playing) return
|
||||
const onMessage = (e: MessageEvent) => {
|
||||
if (e.origin !== YT_ORIGIN || typeof e.data !== 'string' || e.source !== frame.current?.contentWindow) return
|
||||
try {
|
||||
if ((JSON.parse(e.data) as { info?: { playerState?: number } }).info?.playerState === PLAYING) setStarted(true)
|
||||
if ((JSON.parse(e.data) as { info?: { playerState?: number } }).info?.playerState !== PLAYING) return
|
||||
setStarted(true)
|
||||
if (previewSent.current) return
|
||||
previewSent.current = true
|
||||
track('shorts_preview', { video_id: s.id })
|
||||
} catch {
|
||||
return
|
||||
}
|
||||
}
|
||||
window.addEventListener('message', onMessage)
|
||||
return () => window.removeEventListener('message', onMessage)
|
||||
}, [playing])
|
||||
}, [playing, s.id])
|
||||
return (
|
||||
<Link to={`/shorts/${s.id}`} aria-label={`${s.title} 보기`} className="relative block aspect-[9/16] w-[120px] cursor-pointer overflow-hidden rounded-xl bg-[#1d2233] p-0 text-left text-white">
|
||||
<img src={`https://i.ytimg.com/vi/${s.id}/hqdefault.jpg`} alt="" loading="lazy" className="absolute inset-0 size-full object-cover" />
|
||||
{/* 첫 카드 표지는 첫 화면에서 가장 큰 그림(LCP)이라 미루지 않고 먼저 받는다 */}
|
||||
<img src={`https://i.ytimg.com/vi/${s.id}/hqdefault.jpg`} alt="" loading={first ? 'eager' : 'lazy'} fetchPriority={first ? 'high' : 'auto'} className="absolute inset-0 size-full object-cover" />
|
||||
{playing && (
|
||||
<iframe
|
||||
ref={frame}
|
||||
|
||||
Reference in new issue
Block a user