Next.js Discord

Discord Forum

Setting maxDuration in server action gives an error

Answered
Transvaal lion posted this in #help-forum
Open in Discord
Transvaal lionOP
I'm following this guide and am trying to set the maxDuration in a server action: https://vercel.com/docs/functions/configuring-functions/duration

I keep getting this stupid error though: × Only async functions are allowed to be exported in a "use server" file.
Answered by Ray
try set it on the route segment that executes the action
View full answer

98 Replies

Answer
@Ray try set it on the route segment that executes the action
Transvaal lionOP
i dont think it's workign
im in a bit of a pickle rn
im using server actions for my backend and one of the functions usually takes like 20-40s
it's working locally but not on prod (im using vercel)
nothing is shown in logs because i think server actions dont show up there so i have no idea whats happening
im pretty sure it's exceeding the max execution time though
import Input from "../components/lego-form";

export const maxDuration = 120;

export default function Home() {
  return <Input value="" />;
}
this is what i have lol
Transvaal lionOP
how do i check
@Transvaal lion how do i check
project > log
Transvaal lionOP
i dont see any server actions
click the log for the page
Transvaal lionOP
only serverless routes
ok
yes click the route
and check the value of execution duration /lmit
Transvaal lionOP
yeah it's 120
so it must be something else then i guess
your action still got timeout?
have you tried this?
@Ray your action still got timeout?
Transvaal lionOP
idk
like how am i supposed to know
@Transvaal lion like how am i supposed to know
test it? lol
Transvaal lionOP
myh function is called generate
but i dont see it in th elogs
it's in production though like
i can't see anything going on
wtf is hte point of server actions if you cant se elogs
should i just use like api routes
the server action should make the POST request to the route, so as long as you have max duration on that route, it should be fine
Transvaal lionOP
i have one api route which i can see
ok lemme see
ok thanks
i see it now
man why is all this stuff so hard to understand like i can barely find any info about this
ic im getting a 303 status code
303 mean you doing redirect there
Transvaal lionOP
yeah
right?
Transvaal lionOP
yeah
yeah
Transvaal lionOP
this all works locally though
im gonna shoot myself soon
does everything work on prod?
Transvaal lionOP
here is the website
the only thing that doesnt work is the image generation
export async function generateLegoSet(formData: FormData) {
  const userPrompt = (formData.get("prompt") as string) ?? "";
  if (!userPrompt) return;

  const legoSet = await prisma.legoSet.create({
    data: {
      userPrompt: userPrompt,
    },
  });

  proccessImage(legoSet);
  redirect(`/l/${legoSet.id}`);
}

async function proccessImage(legoSet: LegoSet) {
  try {
    noStore();
    const prompt = await generateLegoBoxPrompt(legoSet.userPrompt);

    const image = await openai.images.generate({
      model: "dall-e-3",
      prompt: prompt!,
      style: "vivid",
    });
    const dalleImageUrl = image.data[0].url;
    const uploadedImage = (await utapi.uploadFilesFromUrl([dalleImageUrl!]))[0]
      .data!;

    await prisma.legoSet.update({
      where: { id: legoSet.id },
      data: {
        gptPrompt: prompt,
        imageUrl: uploadedImage.url,
      },
    });
    console.log(uploadedImage);
  } catch (error) {
    console.error(error);
    return { message: "An error occurred, please try again." };
  }
}
the code fails somewhere in processImage on production
it infinitely loads in production but generates the image locally
any error from the log?
Transvaal lionOP
nope
seems like the prompt generation is working
so i assume it fails at uploading the image or updating the database
oh I think because you didn't await proccessImage there and it got timeout somewhere
@Ray have you tried this?
try this maybe
@Ray have you tried this?
Transvaal lionOP
this is a server action though
will it work?
not sure but worth a try
@Ray oh I think because you didn't await `proccessImage` there and it got timeout somewhere
Transvaal lionOP
i cant await it though, idk if you remember but you helped me with this, i wanted to redirect even while it's loading
ok i will try
yeah I know
nice
Transvaal lionOP
i still see 303 errors in the logs but i think it's ifne
@Ray have you tried this?
Transvaal lionOP
gonna try this now and remove the await maybe
yes because you redirect there
Transvaal lionOP
yeah it's not working
Error: The pattern "server/*.ts" defined in functions doesn't match any Serverless Functions.
i tried multiple patterns too
{
  "functions": {
    "server/*.ts": {
      "maxDuration": 120
    }
  }
}
where is proccessImage defined?
maybe try turning proccessImage to route handler and set maxDuration there
Transvaal lionOP
ok
so server actions
are usually used for smaller tasks?
im tryna wrap my head around all this nextjs serverless shit lol
and do this
export async function generateLegoSet(formData: FormData) {
  const userPrompt = (formData.get("prompt") as string) ?? "";
  if (!userPrompt) return;

  const legoSet = await prisma.legoSet.create({
    data: {
      userPrompt: userPrompt,
    },
  });

  fetch('/api/proccess/image', {});
  redirect(`/l/${legoSet.id}`);
}
@Transvaal lion so server actions
no, since you are not awaiting the action, and we are not able to set the max duration for it and that action got timeout somewhere
you might want to do some verification in '/api/proccess/image'
Transvaal lionOP
ok
thanks
when do you recommend using routes vs actions then
I use server action for most of time
only use route handler for image/file response
Transvaal lionOP
ah okay
@Transvaal lion ah okay
let me know when it works😆
Transvaal lionOP
alr 👍
thank you
@Ray let me know when it works😆
Transvaal lionOP
IT WORKS
THNAK OYU
@Transvaal lion IT WORKS
nice