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}
|
||||
|
||||
@@ -4,11 +4,14 @@ import { ARTICLE_LABEL } from '@nalssigak/shared/lib'
|
||||
const SHARED = new Set(['bear', 'bike', 'bronze', 'camping', 'carwash', 'chimaek', 'cloud', 'coldwind', 'delivery', 'dog', 'garden', 'glasses', 'gold', 'hiking', 'iced_hot', 'jjamppong', 'laundry', 'letter', 'makeup', 'makgeolli', 'maple', 'moon', 'muggy', 'naengmyeon', 'photo', 'picnic', 'rabbit', 'rain', 'running', 'silver', 'snow', 'stars', 'sun_cloud', 'sun', 'sunset', 'taxi', 'umbrella', 'vent', 'walk', 'yajang', 'baseball', 'study', 'netflix', 'chestnut', 'peanut', 'tangerine', 'sweet_potato', 'fallen_leaf', 'kimchi', 'apple', 'mask', 'golf', 'jipkok', 'fishing', 'drive', 'kite', 'waterplay', 'riverwind', 'trot', 'pajeon', 'bingsu', 'blanket', 'indoordry', 'flower', 'bungeoppang', 'singing_rain', 'hangang', 'themepark', 'date', 'frizz', 'boiler', 'sunscreen', 'mosquito', 'tennis', 'hyodo', 'thanks', 'gift', 'party', 'game', 'selfie', 'sentimental', 'ginkgo', 'top', 'contact'])
|
||||
const OWN = new Set(['fireworks', 'bulb', 'calendar', 'thermo', 'sheaf', 'popup', 'exhibition', 'concert', 'crystal_ball', 'yin_yang', 'tshirt', 'clapper', 'speech', 'droplet', 'leaf', 'rainbow', 'milky_way', 'shooting_star', 'alarm_clock', 'check_mark_button', 'sparkles', 'warning', 'locked', 'money_bag', 'red_heart', 'briefcase', 'spiral_calendar', 'two_hearts', 'sunrise', 'candy', 'lollipop', 'spider', 'spider_web', 'bat', 'ghost', 'skull', 'jack_o_lantern', 'pepero_choco', 'pepero_almond', 'pepero_strawberry', 'pepero_nude', 'pepero_pair', 'pepero_bundle', 'pepero_box', 'pepero_basket', 'xmas_candy_cane', 'xmas_ornament', 'xmas_gingerbread', 'xmas_santa_hat', 'xmas_stocking', 'xmas_wreath', 'xmas_santa', 'christmas_tree', 'pear', 'dango', 'moon_viewing', 'full_moon', 'suneung_pencil', 'suneung_eraser', 'suneung_heatpack', 'suneung_yeot', 'suneung_mochi', 'suneung_lunchbox', 'suneung_ticket', 'suneung_bouquet', 'newyear_popper', 'newyear_pouch', 'newyear_tteokguk', 'newyear_bell', 'newyear_sunset'])
|
||||
|
||||
/** 아이콘 이름 → 파일 경로 (공용 public/icons, B 전용 public/b/icons). 'none'(데이터 없는 칸)은 그림 없음 */
|
||||
/**
|
||||
* 아이콘 이름 → 파일 경로 (공용 public/icons, B 전용 public/b/icons). 'none'(데이터 없는 칸)은 그림 없음.
|
||||
* 웹은 PNG 옆의 WebP 사본을 쓴다 (PNG 의 1/5 크기). 아이콘 PNG 를 추가하거나 바꾸면 `python frontend/scripts/icons_webp.py` 로 WebP 도 만든다
|
||||
*/
|
||||
export function iconSrc(name: string) {
|
||||
if (name === 'none') return null
|
||||
if (OWN.has(name)) return `/b/icons/${name}.png`
|
||||
return `/icons/${SHARED.has(name) ? name : 'sun_cloud'}.png`
|
||||
if (OWN.has(name)) return `/b/icons/${name}.webp`
|
||||
return `/icons/${SHARED.has(name) ? name : 'sun_cloud'}.webp`
|
||||
}
|
||||
|
||||
export const fall = (d: string, f = '4.8s') => ({ '--d': d, '--f': f }) as CSSProperties
|
||||
|
||||
Reference in new issue
Block a user