Replies: 3 comments 5 replies
-
Hello!
You'll ever need to have some conditional rendering handled by the client, or use something like |
Beta Was this translation helpful? Give feedback.
-
with this control my isMobile controls looks empty until page load completed. What is the best way check device type and using in component |
Beta Was this translation helpful? Give feedback.
-
I wanted to pre-render a page conditionally based on the device type on React/NextJS/SSR. I was able to do it by storing the user-agent in a context and reading it from a hook We create a context to store the parsed user-agent header. // userAgentContext.ts
import { userAgent } from "next/server";
import { createContext } from "react";
export type UserAgent = ReturnType<typeof userAgent>;
export const UserAgentContext = createContext<UserAgent | undefined>(undefined); We create a hook to consume the context and check device type. // useIsMobile.ts
import { useContext } from "react";
import { UserAgentContext } from "./userAgentContext.ts";
export const useIsMobile = () => {
const userAgent = useContext(UserAgentContext);
if (!userAgent) {
throw new Error("useIsMobile must be used within a UserAgentContext.Provider");
}
return userAgent.device.type === "mobile";
}; We can use a sever component as the layout (app router) to get the user agent from request headers // layout.tsx
"use server";
import { headers } from "next/headers";
import { userAgent } from "next/server";
import { LayoutClient } from "./layoutClient.ts";
export default async function Layout({ children }: { children: React.ReactNode }) {
const reqUserAgent = userAgent({ headers: headers() });
return <LayoutClient reqUserAgent={reqUserAgent}>{children}</LayoutClient>
} We can then pass the user agent to a client component that will provide the initial value for the context. Since client components with // layoutClient.tsx
"use client";
import { UserAgentContext } from "./userAgentContext.ts";
export default function LayoutClient({ reqUserAgent, children }: { reqUserAgent: UserAgent, children: React.ReactNode }) {
return <UserAgentContext.Provider value={reqUserAgent}>{children}</UserAgentContext.Provider>;
} Now when this page is rendered for the first time on the server with initial values, useIsMobile() should accurately return a value based on the user-agent device type. // page.tsx
"use client";
export default function Page() {
const isMobile = useIsMobile();
return isMobile ? <>Mobile</> : <>Desktop</>;
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
Currently I am using a getStaticProps to cache my page for a hour. But with this I am loosing context of request to decide device type from header.
`
import React from 'react'
import { GetStaticProps } from 'next'
// Theme layout
import Layout from '../../layouts/LayoutDefault'
import { useTheme } from '@material-ui/core/styles'
//js
import axios from '../../assets/js/utils/request/axios'
//Components
import { SeoHelper } from '../../components/Shared/SeoHelper'
const Dob = ({
}) => {
}
export const getStaticProps: GetStaticProps = async (context) => {
const [apiResponse] = await Promise.all(
[
axios.get(
https://bdemirezen.test.hesapkurdu.com/dob/product/get-active
),]
);
const productList = apiResponse.data;
}
export default Dob;
`
So, I need to decide is request Mobile or Desktop on serverside not with css. Is there any way to use 2 GetStaticProps one of these will have 1 hour cache and the other one will execute every request.
If it is not possible, I am open for further solutions.
Thank You.
Beta Was this translation helpful? Give feedback.
All reactions