next-learn icon indicating copy to clipboard operation
next-learn copied to clipboard

In Chapter 12 Step 6, `redirect` call from Server Action points to the wrong path

Open davinotdavid opened this issue 2 years ago • 1 comments

In Chapter 12's Revalidate and Redirect, it says that the redirect call should be to redirect('dashboard/invoices'), however when I run it locally it points to the wrong path.

Server Action code (app/lib/actions.ts):

'use server';

import { z } from 'zod';
import { sql } from '@vercel/postgres';
import { revalidatePath } from 'next/cache';
import { redirect } from 'next/navigation';

const FormSchema = z.object({
  id: z.string(),
  customerId: z.string(),
  amount: z.coerce.number(),
  status: z.enum(['pending', 'paid']),
  date: z.string(),
})
 
const CreateInvoice = FormSchema.omit({ id: true, date: true });

export async function createInvoice(formData: FormData) {
  const { customerId, amount, status } = CreateInvoice.parse({
    customerId: formData.get('customerId'),
    amount: formData.get('amount'),
    status: formData.get('status'),
  });

  const amountInCents = amount * 100;
  const date = new Date().toISOString().split('T')[0];

  await sql`
    INSERT INTO invoices (customer_id, amount, status, date)
    VALUES (${customerId}, ${amountInCents}, ${status}, ${date})
  `;

  revalidatePath('/dashboard/invoices');
  redirect('dashboard/invoices');
}

This action is being invoked from a button in localhost:3000/dashboard/invoices/create and when it is invoked, it attempts to redirect to localhost:3000/dashboard/invoices/dashboard/invoices which doesn't exist.

Does this mean that redirect calls from Server Actions use relative paths based off of where they are invoked?

davinotdavid avatar Jan 20 '24 20:01 davinotdavid

Ah! I missed one / here on the redirect call, my bad!

davinotdavid avatar Jan 20 '24 20:01 davinotdavid