79457904

Date: 2025-02-21 15:43:23
Score: 1
Natty:
Report link

The issue you're facing is due to Next.js's server-side rendering (SSR) behavior. When your Home component runs, it executes on the server at build time (or on demand with caching). This means that Math.floor(Math.random() * 9) is executed once on the server, and the result is sent to the client. Since the page is cached by default on platforms like Vercel, refreshing the page does not generate a new random number.

when using npm run dev, Next.js runs in development mode where it does not apply aggressive caching. Each request re-runs the function, so you get a new random value every time.

To fix this, move the random selection to the client side using useEffect, so a new question is picked whenever the page loads. Alternatively, use getServerSideProps instead of getStaticProps to ensure a fresh random question is selected on every request. The first approach is more efficient, while the second guarantees a new question even for bots and crawlers.

Reasons:
  • Long answer (-0.5):
  • No code block (0.5):
  • Low reputation (1):
Posted by: Muhammad