Next.js Discord

Discord Forum

generateStaticParams, but load is slow.

Unanswered
Turkish Angora posted this in #help-forum
Open in Discord
Turkish AngoraOP
I have an article page that displays all my articles and I have a <Link> tag to each individual article.

My individual articles are under articles/[id]/[articleName]/page.tsx.

My problem is that when I click an article, the load is sometimes up to 3 seconds and I have no idea why. I thought the generateStaticParams would pre-render all my articles and thus making the load time instant.

PS: I have tried adding a delay to the getData function and that does make the loading slower, so the problem seems to be here.

Thanks in advance! This has been bugging me for so long!

Here is my code:
import Navbar from "@/app/components/Navbar/Navbar";
import React from "react";
import Footer from "@/app/components/Footer/Footer";
import "./article.scss";
import NewTextArticle from "@/app/components/TextArticle/NewTextArticle";
import { fetchArticle, fetchArticles } from "@/app/supabase/fetchArticles";
import { Metadata } from "next";

export async function generateStaticParams() {
    const articleData: any = await fetchArticles();

    return articleData.map(({ id }: any) => id);
}

export async function generateMetadata({ params }: any): Promise<Metadata> {
    const articleData = await fetchArticle(params.id);

    return {
        title: articleData?.header,
        description: articleData?.sub_header,
        openGraph: {
            images: [
                {
                    url: articleData?.front_page_image,
                    alt: articleData?.image_sub_title,
                },
            ],
        },
    };
}

const Article = async ({ params }: any) => {
    const articleData = await getData(params);
    console.log(articleData);

    return (
        <>
            <div className="article-background">
                <Navbar />
                <NewTextArticle articleObject={articleData.props.articleData} content={articleData.props.articleData?.content} params={params} />
                <Footer />
            </div>
        </>
    );
};

async function getData(context: any) {
    const { id } = context;

    const articleData = await fetchArticle(id);

    return {
        props: {
            articleData,
            params: context,
        },
    };
}

export default Article;

8 Replies

Madeiran sardinella
Hi, why are you using this route with two dinamic params?
articles/[id]/[articleName]/page.tsx.
@Turkish Angora To be honest, it's just because I think the link looks better that way. For example like this: https://phantomcoreai.com/articles/90/Exploring-the-Strategic-Alliance-Between-OpenAI-and-Reddit:-A-Game-Changer-for-AI-Integration-in-Social-Media
Madeiran sardinella
Idk if that is the cause of the performance issue but I'll make something like:
articles/[slug]/page.tsx.
Because you are trying to look for two params and exporting just one.
I'd try
@Madeiran sardinella Idk if that is the cause of the performance issue but I'll make something like: articles/[slug]/page.tsx. Because you are trying to look for two params and exporting just one. I'd try
Turkish AngoraOP
I see. Thank you so much for your reply. Really appreciate it. Just one quick question.

How would I then navigate to the single article? Don't I need the Id from the url?

This is my current code. I return these cards in my articlePage.tsx:

<ArticleCard
    image={article.front_page_image}
    key={index}
    desc={article?.content[0].elements[1]?.content}
    title={article?.header}
    author={article?.author}
    href={`/articles/${article.id}/${article.header.replaceAll(" ", "-")}`}
    date={`${new Date(article.date_updated).toLocaleDateString("en-US", {
        month: "long",
        day: "numeric",
        year: "numeric",
    })} - ${article.read_time}`}
  />
Turkish AngoraOP
@Madeiran sardinella Thank you so much brother! I just made the change, and its much faster.
@Turkish Angora <@961768338231541820> Thank you so much brother! I just made the change, and its much faster.
Madeiran sardinella
That's great, your welcome bro!