在 Vercel 上部署 NextJS 应用程序时,来自 Notion API 的数据未更新

问题描述 投票:0回答:1

我在 NextJS 中有一个博客应用程序,它从 Notion API 获取帖子内容。它可以在本地主机上完美运行,我在 Notion 中进行更改,并且它们可以正常加载。但使用 Vercel 生产部署时,当 Notion 发生更改时,不会加载更改。可能会发生什么?

Github 链接:https://github.com/ewrtonl/ewerton.dev

我不知道如何解决这个问题,我尝试直接通过Vercel部署它,而不是通过Github提交然后部署它,它也不起作用。

import { Client } from "@notionhq/client";
import { NotionDatabaseResponse } from "../_types/notion";

const notion = new Client({ auth: process.env.NOTION_API_KEY });

export async function getPosts() {
  const databaseId = process.env.NOTION_DATABASE_ID || "";
  const response = await notion.databases.query({
    database_id: databaseId,
    filter: {
      or: [
        {
          property: "published",
          checkbox: {
            equals: true,
          },
        },
      ],
    },
  });

  const typedResponse = (response as unknown) as NotionDatabaseResponse;

  return typedResponse.results.map((post) => {
    return {
      id: post.id,
      title: post.properties.title.title[0].plain_text,
      slug: post.properties.slug.rich_text[0].plain_text,
      tags: post.properties.tags.multi_select.map((tag) => tag.name),
      createdAt: post.created_time,
    };
  })
}
typescript next.js vercel notion-api
1个回答
0
投票

部署 nextJS 项目时,该项目是使用

npm run build
命令构建的,该命令尝试以静态方式生成页面并缓存这些页面。接下来在构建并缓存结果页面时运行
getPosts()
函数,但不再运行此函数。

您必须明确声明此页面不会静态呈现。您可以通过输入以下内容来做到这一点:

export default async function Page(){
    //do whatever you want with the data
    const posts = getPosts();
    ...
}

在您的

page.tsx
文件中。

© www.soinside.com 2019 - 2024. All rights reserved.