11---
2+ import { ViewTransitions } from ' astro:transitions' ;
23import { getCollection } from ' astro:content' ;
3- import { getLocalePrefix , resolveLocale , type Locale } from ' ../../i18n/config' ;
4+ import { getLocalePrefix , type Locale } from ' ../../i18n/config' ;
45
56interface Props {
67 locale: Locale ;
78}
89
9- const { locale : incomingLocale } = Astro .props as Props ;
10- const locale = resolveLocale (incomingLocale );
10+ const { locale } = Astro .props as Props ;
1111
1212const entries = await getCollection (' entries' , ({ data }) => ! data .draft && data .lang === locale );
1313
@@ -17,15 +17,10 @@ const candidates = entries.map((entry) => ({
1717 url: ` ${localePrefix }/posts/${entry .data .translationKey ?? entry .slug } ` .replace (/ \/\/ / g , ' /' ),
1818}));
1919
20- const randomEntry = candidates .length > 0
21- ? candidates [Math .floor (Math .random () * candidates .length )]
22- : undefined ;
23-
24- const randomUrl = randomEntry ?.url ?? (localePrefix || ' /' );
2520const loadingText = locale === ' ko' ? ' 랜덤 기록을 불러오는 중...' : ' Loading a random entry...' ;
2621const emptyText = locale === ' ko' ? ' 표시할 기록이 없습니다.' : ' No entries available.' ;
2722const pageTitle = locale === ' ko' ? ' 랜덤 기록 - Code Brewer' : ' Random Entry - Code Brewer' ;
28- const shouldRedirect = Boolean ( randomEntry ) ;
23+ const fallbackUrl = localePrefix || ' / ' ;
2924---
3025
3126<!DOCTYPE html >
@@ -34,10 +29,32 @@ const shouldRedirect = Boolean(randomEntry);
3429 <meta charset =" UTF-8" />
3530 <meta name =" viewport" content =" width=device-width, initial-scale=1.0" />
3631 <title >{ pageTitle } </title >
37- { shouldRedirect && <meta http-equiv = " refresh" content = { ` 0;url=${randomUrl } ` } />}
38- { shouldRedirect && (
39- <script is :inline >{ ` window.location.href = '${randomUrl }'; ` } </script >
40- )}
32+ <ViewTransitions />
33+ <script is:inline define:vars ={ { candidates , fallbackUrl }} >
34+ // Pick random entry on EVERY page load (client-side)
35+ const pickRandomAndNavigate = () => {
36+ if (candidates && candidates.length > 0) {
37+ const randomIndex = Math.floor(Math.random() * candidates.length);
38+ const randomUrl = candidates[randomIndex].url;
39+
40+ console.log('Redirecting to:', randomUrl);
41+
42+ // Create a link and click it to trigger View Transitions
43+ const link = document.createElement('a');
44+ link.href = randomUrl;
45+ link.style.display = 'none';
46+ document.body.appendChild(link);
47+ link.click();
48+ document.body.removeChild(link);
49+ } else {
50+ // No entries available, redirect to home
51+ window.location.href = fallbackUrl;
52+ }
53+ };
54+
55+ // Small delay to show loading animation
56+ setTimeout(pickRandomAndNavigate, 300);
57+ </script >
4158 <style >
4259 body {
4360 margin: 0;
@@ -70,7 +87,7 @@ const shouldRedirect = Boolean(randomEntry);
7087 <body >
7188 <div class =" loader" >
7289 <div class =" spinner" ></div >
73- <p >{ shouldRedirect ? loadingText : emptyText } </p >
90+ <p >{ candidates . length > 0 ? loadingText : emptyText } </p >
7491 </div >
7592 </body >
7693</html >
0 commit comments