Next.js应用路由器,指定不使用'fetch'的服务器端函数的重新验证

h9a6wy2h  于 2023-05-28  发布在  其他
关注(0)|答案(1)|浏览(172)

我在我的Next.js项目中使用了新的App Router。我的页面充满了从桶中获取的Markdown文件的内容。在我的Vercel主机上,这些页面被永久缓存,因此对Markdown文件的更改不会显示。
文档显示了如何在fetch参数中指定缓存过期时间。但是我的函数没有使用fetch,它使用的是Firebase SDK。
我还尝试添加一个额外的步骤,创建一个返回文件内容的内部API路由,然后页面路由可以fetch该api路由,指定适当的重新验证参数。但是Next.js似乎不允许从服务器端获取内部API路由。
下面是我的一个路由的代码,其中downloadFromStorage返回bucket中文件的内容。

import {TextPage} from "@/components";
import {downloadFromStorage} from "@/app/api/file-handling";

export default async function PageTerms(props) {
    const text=await downloadFromStorage('pages/terms.md')
    return (<TextPage text={text.toString()}/>)
}

(the downloadFromStorage函数)

import {storage} from "./firebase"
export async function downloadFromStorage(filename, bucketName = BUCKET) {
    const bucket = storage.bucket(bucketName);
    const file = bucket.file(filename);
    return new Promise((resolve, reject) => {
        file.download((err, contents) => {
            if (err) {
                reject(err);
                return;
            }
            resolve(contents);
        });
    });
}
hivapdat

hivapdat1#

您可以使用revalidate选项添加路由段配置。这将根据导出的整数重新验证整个段。

// revalidate every 60 seconds
export const revalidate = 60;

您可以在官方文档中了解有关段配置和重新验证选项的更多信息。

相关问题