这是我刚刚在Vercel部署的website.我正在使用Prisma和Next.js构建一个Web应用程序,我遇到了一个问题,在我手动重新部署该应用程序之前,内容不会实时更新.以下是场景:
- 我的Next.js应用程序中有一个从Prisma数据库获取数据的API端点.
- 当我通过应用程序在数据库中创建或更新数据时,更改会立即反映在开发环境中,但在我重新部署应用程序之前,它们不会反映在生产环境中.
以下是我在前端获取数据的方式:
const { data: posts, error } = useSWR(`/api/getPosts`, fetcher, {refreshInterval:1000});
个
这是发布内容的API端点:
// Addposts to prisma backend
import { NextResponse, NextRequest } from 'next/server';
import prisma from '../../../prisma/client';
// Function
export async function POST(request:NextRequest) {
const data = await request.json();
const title = data.title;
const user = await prisma.user.findUnique({
where: {
email : data.email
}
})
if (!user){
// return error
return NextResponse.json({error: "User not found"}, {status: 404})
}
if (!title){
// throw error
return NextResponse.json({error: "Title is required"}, {status: 400})
}
if (title.length > 300){
return NextResponse.json({error:"Title should not be more than 300 characters"}, {status:400});
}
const userId = user?.id;
const post = await prisma.post.create({
data: {
title,
userId
}
})
try{
return NextResponse.json({post},{status:200})
}catch(error){
return NextResponse.json({error}, {status:500})
}
}
获取所有帖子的API端点:
import { NextRequest, NextResponse } from 'next/server'
import prisma from '../../../prisma/client'
import { NextApiResponse } from 'next';
export async function GET(request:NextRequest){
const posts = await prisma.Post.findMany({
include: {
user: true
},
orderBy:{
createdAt: 'desc'
}
})
try{
// return all the posts
return NextResponse.json({posts},{status:200})
}catch(error){
return NextResponse.json(error, {status:500});
}
}
我如何才能确保内容更新立即反映在生产环境中,而不需要手动重新部署?
这是GitHub回购的link美元.
UPDATE个
I am able to make POST request and make changes to the db, I think the problem has to be with the GET request since the data appears to be static even when I refresh the page.个
以下是我在Vercel上的运行时日志(log):