Server Actions
Server Actions enable server-side functionality through JavaScript functions that Next.js automatically converts into server POST endpoints. They provide an efficient way to modify data and refresh content that was initially rendered by Server Components.JetShip implements Server Actions following standard Next.js conventions.
Server Actions are primarily used to perform database operations on the server while maintaining a function-like interface for the client. The most common use case is updating database records through Server Actions.
Creating a Server Action requires two steps: adding the use server
directive at the beginning of a file and exporting the desired function.
'use server'
import { createClient } from '@repo/supabase/server'
export async function myServerAction(formData: FormData) {
const supabase = await createClient()
const { error } = await supabase
.from('profiles')
.update({
name: formData.get('name'),
bio: formData.get('bio')
})
.eq('id', formData.get('id'))
if (error) {
return { error: error.message }
}
return { message: 'Profile updated successfully' }
}