#Fetch relative path

3 messages · Page 1 of 1 (latest)

fierce badge
#

App Router.
I wrote a serverless function and want to use the appropriate path when using fetch.
const res = await fetch('/api/serverless-example')

But for this I need to use 'use client'

However, if the function component is async, then it must be server-side.

fair yewBOT
#

🔎 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)

cursive crypt
#
  1. Since this is an RSC then you won't have access to the current path. This is currently something even I have issues with, but that's just how it works. You can either state the absolute path (you can get this from by storing the value in the .env file) or try to get the path from the headers using next/headers.

  2. If you have access to it, you can make use of server actions. I've found that in most cases where I would've used a Route Handler in the past, I ended up using it on one page a single time (maybe on page load) and that's in. Replacing those cases with server actions just made the code more readable and even function better in some cases imo. Only if you're gonna reuse an endpoint on the client side do I think you really need to make use of Route Handlers now.