11---
2- import { ViewTransitions } from ' astro:transitions' ;
32import { getCollection } from ' astro:content' ;
43import { getLocalePrefix , type Locale } from ' ../../i18n/config' ;
54
@@ -12,15 +11,12 @@ const { locale } = Astro.props as Props;
1211const entries = await getCollection (' entries' , ({ data }) => ! data .draft && data .lang === locale );
1312
1413const localePrefix = getLocalePrefix (locale );
14+ const postUrls = entries .map ((entry ) =>
15+ ` ${localePrefix }/posts/${entry .data .translationKey ?? entry .slug } ` .replace (/ \/\/ / g , ' /' )
16+ );
1517
16- const candidates = entries .map ((entry ) => ({
17- url: ` ${localePrefix }/posts/${entry .data .translationKey ?? entry .slug } ` .replace (/ \/\/ / g , ' /' ),
18- }));
19-
20- const loadingText = locale === ' ko' ? ' 랜덤 기록을 불러오는 중...' : ' Loading a random entry...' ;
21- const emptyText = locale === ' ko' ? ' 표시할 기록이 없습니다.' : ' No entries available.' ;
22- const pageTitle = locale === ' ko' ? ' 랜덤 기록 - Code Brewer' : ' Random Entry - Code Brewer' ;
2318const fallbackUrl = localePrefix || ' /' ;
19+ const pageTitle = locale === ' ko' ? ' Random Post - Code Brewer' : ' Random Entry - Code Brewer' ;
2420---
2521
2622<!DOCTYPE html >
@@ -29,65 +25,21 @@ const fallbackUrl = localePrefix || '/';
2925 <meta charset =" UTF-8" />
3026 <meta name =" viewport" content =" width=device-width, initial-scale=1.0" />
3127 <title >{ pageTitle } </title >
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);
28+ <script is:inline define:vars ={ { postUrls , fallbackUrl }} >
29+ // 한 번만 실행되도록 보장
30+ (function() {
31+ if (window.__randomRedirected) return;
32+ window.__randomRedirected = true;
33+
34+ // 랜덤 포스트로 즉시 리다이렉트
35+ if (postUrls && postUrls.length > 0) {
36+ const randomIndex = Math.floor(Math.random() * postUrls.length);
37+ window.location.replace(postUrls[randomIndex]);
4938 } else {
50- // No entries available, redirect to home
51- window.location.href = fallbackUrl;
39+ window.location.replace(fallbackUrl);
5240 }
53- };
54-
55- // Small delay to show loading animation
56- setTimeout(pickRandomAndNavigate, 300);
41+ })();
5742 </script >
58- <style >
59- body {
60- margin: 0;
61- padding: 0;
62- background: #0D0D15;
63- color: #E8E4D0;
64- font-family: system-ui, -apple-system, sans-serif;
65- display: flex;
66- align-items: center;
67- justify-content: center;
68- height: 100vh;
69- }
70- .loader {
71- text-align: center;
72- }
73- .spinner {
74- border: 3px solid rgba(136, 170, 255, 0.1);
75- border-top-color: #88AAFF;
76- border-radius: 50%;
77- width: 40px;
78- height: 40px;
79- animation: spin 0.8s linear infinite;
80- margin: 0 auto 1rem;
81- }
82- @keyframes spin {
83- to { transform: rotate(360deg); }
84- }
85- </style >
8643 </head >
87- <body >
88- <div class =" loader" >
89- <div class =" spinner" ></div >
90- <p >{ candidates .length > 0 ? loadingText : emptyText } </p >
91- </div >
92- </body >
44+ <body ></body >
9345</html >
0 commit comments