SSG NextJS的generateStaticParams函数内的传递参数

qc6wkl3g  于 2023-06-05  发布在  其他
关注(0)|答案(1)|浏览(306)

对于我的SSG页面,我需要传递一个参数,该参数是语言到我的API调用函数,该函数在generateStaticParams函数中,我在cookie中存储了语言,我无法访问,是否有方法可以将参数传递给我的函数?

export async function generateStaticParams() {
  const getAllCategories = await getCategories(); // this needs language
  return getAllCategories.map((category: Category) => ({
    id: category.id,
    category: category.groupName,
  }));
}
kulphzqa

kulphzqa1#

您可以在Next13中使用Nextjs官方库的cookie

import { cookies } from 'next/headers';

export async function generateStaticParams() {

    const cookieStore = cookies();
    const language = cookieStore.get('language');

    const getAllCategories = await getCategories(language); // this needs language
    return getAllCategories.map((category: Category) => ({
        id: category.id,
        category: category.groupName,
    }));
}

相关问题