File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 55 ClientOnly ,
66 createRootRouteWithContext ,
77 HeadContent ,
8+ redirect ,
89 ScriptOnce ,
910 Scripts ,
1011} from "@tanstack/react-router" ;
@@ -16,6 +17,7 @@ import { Button } from "@/components/ui/button";
1617import { Card , CardContent , CardHeader } from "@/components/ui/card" ;
1718import { Toaster } from "@/components/ui/sonner" ;
1819import { client , idbName } from "@/db" ;
20+ import { getIsMobile } from "@/server/functions/getIsMobile" ;
1921import { seo } from "@/utils/seo" ;
2022import appCss from "../styles.css?url" ;
2123
@@ -24,6 +26,20 @@ interface MyRouterContext {
2426}
2527
2628export const Route = createRootRouteWithContext < MyRouterContext > ( ) ( {
29+ beforeLoad : async ( { location } ) => {
30+ // Skip redirect if already on /mobile to avoid infinite loop
31+ if ( location . pathname === "/mobile" ) {
32+ return ;
33+ }
34+
35+ const isMobile = await getIsMobile ( ) ;
36+
37+ if ( isMobile ) {
38+ throw redirect ( {
39+ to : "/mobile" ,
40+ } ) ;
41+ }
42+ } ,
2743 head : ( ) => ( {
2844 meta : [
2945 {
Original file line number Diff line number Diff line change 11import { createFileRoute , redirect } from "@tanstack/react-router" ;
2- import { createServerFn } from "@tanstack/react-start" ;
3- import { getRequestHeader } from "@tanstack/react-start/server" ;
4-
5- const getIsMobile = createServerFn ( ) . handler ( async ( ) => {
6- const userAgent = getRequestHeader ( "user-agent" ) || "" ;
7- const mobileRegex =
8- / A n d r o i d | w e b O S | i P h o n e | i P a d | i P o d | B l a c k B e r r y | I E M o b i l e | O p e r a M i n i / i;
9- return mobileRegex . test ( userAgent ) ;
10- } ) ;
112
123export const Route = createFileRoute ( "/" ) ( {
134 beforeLoad : async ( ) => {
14- const isMobile = await getIsMobile ( ) ;
15-
16- if ( isMobile ) {
17- throw redirect ( {
18- to : "/mobile" ,
19- } ) ;
20- }
21-
225 throw redirect ( {
236 to : "/projects" ,
247 } ) ;
Original file line number Diff line number Diff line change 1+ import { createServerFn } from "@tanstack/react-start" ;
2+ import { getRequestHeader } from "@tanstack/react-start/server" ;
3+
4+ export const getIsMobile = createServerFn ( ) . handler ( async ( ) => {
5+ const userAgent = getRequestHeader ( "user-agent" ) || "" ;
6+ const mobileRegex =
7+ / A n d r o i d | w e b O S | i P h o n e | i P a d | i P o d | B l a c k B e r r y | I E M o b i l e | O p e r a M i n i / i;
8+ return mobileRegex . test ( userAgent ) ;
9+ } ) ;
You can’t perform that action at this time.
0 commit comments