#How do I revalidate and redirect?

3 messages · Page 1 of 1 (latest)

compact patrol
#

If I create a product in /add-product and want to revalidate the home page and get redirect there.

I'm doing it this way but it doesn't feel right. Any idea?

'use server'
import prisma from '@/lib/prisma'
import { revalidatePath } from 'next/cache'
import { redirect } from 'next/navigation'

export const createProduct = async (formData: FormData) => {
const name = formData.get('name')?.toString()
const description = formData.get('description')?.toString()
const price = parseFloat(formData.get('price') as string)
const amount = parseInt(formData.get('amount') as string, 10)

if (!name || !description || !price || !amount) {
return
}

const existingProduct = await prisma.product.findFirst({
where: { name },
})

if (existingProduct) {
return false
}

await prisma.product.create({
data: {
name,
description,
price,
amount,
},
})

revalidatePath('/')
redirect('/')
}

torn schoonerBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

grand mantle
#

this works ?