Next.js只运行一次getServerSideProps,当Next/router访问时不会获取新数据



我有一个带有SSR的Next.js应用程序,我在其中导出getServerSideProps异步函数,如下所示:

export const getServerSideProps = getGenericServerSideProps([""]);

getGenericServerSideProps是一个异步函数,它是从另一个文件导入的,该文件调用无头CMS来获取数据:

export const getInitialProps = (slices: string[], ...callbacks) => async (
context: NextPageContext
): Promise<CmsPageResponse> => {
for (const callback of callbacks) {
if (Array.isArray(callback)) {
continue;
}
callback();
}
const lang = getLangFromContext(context);
const { slug } = context.query as { slug: string[] };
const paths = getPaths(slug, lang);
return await getCmsPageManager([...slices, ...paths].join("/"), lang);
};
export const getGenericServerSideProps = (slices: string[], ...callbacks) => async (
context: NextPageContext
): Promise<any> => {
const appConfiguration = getAppConfiguration();
const props = await getInitialProps(slices, callbacks)(context);
return { props: { ...props, appConfiguration } };
};

如果我通过URL访问页面(例如直接访问app/page1(,它会运行getServerSideProps并给我新的数据。

如果我试图第二次访问同一页面,但通过路由器链接:

<Link href={ROUTES[Pages.Home]} as={ROUTES[Pages.Home]}><a>Link</a></Link>

它不执行getServerSideProps,并向我提供旧数据。

如何在每次访问页面时强制Next.js执行getServerSideProps?现在,它似乎在使用构建应用程序时创建的道具(如果我重新构建应用程序,新道具和旧道具会同步,但如果我对CMS中的数据进行更改,页面会在第二次渲染时显示旧数据(。

留下一个错误的答案,因为我已经弄清楚了。

显然,该项目已经在next.config.js中为请求的所有json文件设置了自定义缓存控制头,并有很长的无效期,next.js显然将其应用于页面最初加载GSSP后通过AJAX请求的JSON文件。

检查您的next.config.js人员。

最新更新