#Help improving the loading time

1 messages · Page 1 of 1 (latest)

sturdy etherBOT
#

Hi @random knot. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

I can help you improve the loading time for your Next.js page by implementing proper loading states. Currently, your code fetches data before rendering anything, which means users see nothing until the data is fully loaded.

Next.js provides several approaches to improve this experience:

Using Suspense for Streaming

You can use React's Suspense component to show a loading state while data is being fetched:

import { Suspense } from 'react'
import { StatsDisplay } from './components/stats-display'

async function getPlayerData(player) {
  const res = await fetch(`something something here`, { cache: "no-store" });
  if (!res.ok) {
    throw new Error('Failed to fetch player data');
  }
  return res.json();
}

// Create a separate component for fetching data
async function PlayerStats({ playerName }) {
  const data = await getPlayerData(playerName);
  return <StatsDisplay data={data} playerName={playerName} />;
}

export default function Home() {
  return (
    <Suspense fallback={
      <div className="min-h-screen bg-gray-900 flex items-center justify-center">
        <div className="text-white font-minecraft text-2xl">Loading player data...</div>
      </div>
    }>
      <PlayerStats playerName="CyaniceBlitz" />
    </Suspense>
  );
}

This approach uses React Suspense to show the loading UI immediately while the data fetching happens in the background. When the data is ready, the UI will automatically update with the player stats. This creates a better user experience as the page loads instantly with a loading indicator.

Using loading.js (App Router)

If you're using the App Router in Next.js, you can create a loading.js file in the same folder as your page to automatically show a loading state:

#
// app/loading.js
export default function Loading() {
  return (
    <div className="min-h-screen bg-gray-900 flex items-center justify-center">
      <div className="text-white font-minecraft text-2xl">Loading player data...</div>
    </div>
  );
}

This file will automatically wrap your page in a Suspense boundary and show the loading UI while your page is loading.

Improving Data Fetching

You might also want to consider caching your data when appropriate instead of using { cache: "no-store" } for every request. This can significantly improve loading times for subsequent visits:

// Cache data for 60 seconds
const res = await fetch(`something something here`, { next: { revalidate: 60 } });

These approaches will help display a loading state immediately while the data is being fetched in the background, creating a more responsive user experience.

Loading UI and Streaming provides more details on implementing loading states in Next.js.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@random knot Kapa.ai is still learning and improving, please let me know how I did by reacting below.