#server actions with isr

1 messages · Page 1 of 1 (latest)

void yoke
#

Can I use server actions with generateStaticParams and revalidate route segment config!

grizzled nimbusBOT
#

Hi @void yoke. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Yes, you can use Server Actions in combination with generateStaticParams and revalidate route segment config in Next.js.

The generateStaticParams function can be used with dynamic route segments to define the list of route segment parameters that will be statically generated at build time instead of on-demand at request time. This function replaces the getStaticPaths function in the Pages Router. During next dev, generateStaticParams will be called when you navigate to a route. During next build, generateStaticParams runs before the corresponding Layouts or Pages are generated. However, during revalidation (ISR), generateStaticParams will not be called again [source (https://nextjs.org/docs/app/api-reference/functions/generate-static-params)].

The revalidate option is used to revalidate data at a timed interval. You can use the next.revalidate option of fetch to set the cache lifetime of a resource (in seconds). However, it's important to note that the revalidate option is only available when using the Node.js Runtime. This means using the revalidate option with runtime = 'edge' will not work [source (https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config)].

Server Actions in Next.js have been designed for deep integration with the rest of the data lifecycle, including the Next.js Cache, Incremental Static Regeneration (ISR), and the client router. Revalidating data through new APIs revalidatePath and revalidateTag mean that mutating, re-rendering the page, or redirecting can happen in one network roundtrip, ensuring the correct data is displayed on the client, even if the upstream provider is slow [source (https://nextjs.org/blog/next-13-4)].

Here's an example of a Server Action that uses revalidateTag:

#

@void yoke kapa.ai is still learning and improving, please let us know how it did by reacting below

void yoke
#

What about doing mutation in client component?

  1. Do I need to call router.refresh() after mutation done?
  2. Do I need to wrap router.refresh() in startTransition?
#

@grizzled nimbus

grizzled nimbusBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Sorry @void yoke, I seem to be having a bit of trouble. Don't worry though, I've pinged my supprt and they will try to fix me up! :technologist:

Perhaps try again in 15mins?

void yoke
#

@grizzled nimbus What about doing mutation in client component?
Do I need to call router.refresh() after mutation done?
Do I need to wrap router.refresh() in startTransition?

grizzled nimbusBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Yes, you can perform data mutations in client components using Server Actions in Next.js. Here's an example of a Server Action being called inside a Client Component:

'use client'
 
import { addItem } from './actions.js'
 
// Server Action being called inside a Client Component
export default function AddToCart({ productId }) {
 return (
 <form action={addItem}>
 <button type="submit">Add to Cart</button>
 </form>
 )
}

[source (https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions)]

After a mutation is done, you might want to refresh the data on your page. In Next.js, you can use the router.refresh() method to reload the current page. This can be useful when you want to fetch the latest data after a mutation.

However, the extracts provided do not specifically mention the need to call router.refresh() after a mutation or the need to wrap router.refresh() in startTransition. The decision to use these methods would depend on the specific requirements of your application.

It's also worth noting that with Server Actions, revalidating data through new APIs revalidatePath and revalidateTag mean that mutating, re-rendering the page, or redirecting can happen in one network roundtrip, ensuring the correct data is displayed on the client, even if the upstream provider is slow [source (https://nextjs.org/blog/next-13-4)]. This might eliminate the need for manually refreshing the page in some cases.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@void yoke kapa.ai is still learning and improving, please let us know how it did by reacting below

void yoke
#

@grizzled nimbus I mean without server actions, since it only support dynamic rendering

grizzled nimbusBOT
#

Let me check the knowledge base for you. One moment! :books:

#

If you're not using Server Actions and you're performing data mutations on the client side, you might indeed need to manually refresh the page to fetch the latest data after a mutation. You can use the router.refresh() method from the next/router package to do this.

Here's an example of how you might use it:

import { useRouter } from 'next/router'

export default function YourComponent() {
  const router = useRouter()

  const handleMutation = async () => {
    // Perform your data mutation here

    // Then refresh the page
    router.refresh()
  }

  // Your component rendering
}

As for wrapping router.refresh() in startTransition, this would depend on whether you want to provide a smoother user experience by delaying the display of loading states during the page transition. The startTransition function from React 18's concurrent mode can be used to achieve this.

Here's an example:

import { startTransition, useTransition } from 'react'
import { useRouter } from 'next/router'

export default function YourComponent() {
  const router = useRouter()

  const handleMutation = async () => {
    // Perform your data mutation here

    // Then refresh the page with startTransition
    startTransition(() => {
      router.refresh()
    })
  }

  // Your component rendering
}

Please note that these examples are based on general Next.js and React usage, and the exact implementation might vary depending on the specifics of your application and data mutation. The provided extracts do not specifically mention the use of router.refresh() or startTransition in this context.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@void yoke kapa.ai is still learning and improving, please let us know how it did by reacting below

void yoke
#

@grizzled nimbus What if I do NOT want to have a loading state?

grizzled nimbusBOT
#

Let me check the knowledge base for you. One moment! :books:

#

If you do not want to display a loading state after a data mutation on the client side, you don't need to use startTransition when calling router.refresh(). The startTransition function is used to delay the display of loading states during transitions, so if you don't want a loading state, you can simply call router.refresh() directly.

Here's an example:

import { useRouter } from 'next/router'

export default function YourComponent() {
  const router = useRouter()

  const handleMutation = async () => {
    // Perform your data mutation here

    // Then refresh the page
    router.refresh()
  }

  // Your component rendering
}

In this example, the page will refresh immediately after the data mutation, without displaying a loading state. Please note that the user might still experience a brief moment of waiting while the page is refreshing, depending on the performance of your application and the user's network conditions.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: