Next.js Discord

Discord Forum

Server function working on development environment but not on production (vercel)

Answered
Western paper wasp posted this in #help-forum
Open in Discord
Western paper waspOP
Hey, so basically i created a project that need to use two different server fonction :

lib/action/time-keeper.ts

"use server"

import { GameInfiniteserver } from "./cosmoperation/toggleGameInfiniteserver";
import { supabase } from "@/app/api/supabase/supabaseClient";


export async function timeKeeper(startTime: number) {
    console.log(`Game start time is: ${startTime}`);

    const delayMs = 28 * 1000; 

    setTimeout(async () => {
        try{
        await GameInfiniteserver("sei18j0wumtq8yewt7ka8403q7v7qfezhlsc53aq3hm7uvq8gj9xdnjs4rctnz"); //OTHER SERVER FUNCTION
        } catch (error) {
            console.error("error on executing the sc : ", error)
        }
        setTimeout(async () => {
            try {
                const { data, error } = await supabase
                    .from('players_data')
                    .delete()
                    .not('id', 'is', null)
              
                if (error) throw error;
                console.log('All rows deleted successfully:', data);
              } catch (error) {
                console.error('Error deleting rows:', error);
              }
              
        }, 10000);
    }, delayMs);
}
Answered by Ray
seem like a problem with setTimeout, try this instead
"use server";

import { GameInfiniteserver } from "./cosmoperation/toggleGameInfiniteserver";
import { supabase } from "@/app/api/supabase/supabaseClient";

async function sleep(ms: number) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

export async function timeKeeper(startTime: number) {
  console.log(`Game start time is: ${startTime}`);

  const delayMs = 28 * 1000;

  await sleep(delayMs);
  try {
    await GameInfiniteserver(
      "sei18j0wumtq8yewt7ka8403q7v7qfezhlsc53aq3hm7uvq8gj9xdnjs4rctnz"
    ); //OTHER SERVER FUNCTION
  } catch (error) {
    console.error("error on executing the sc : ", error);
  }

  await sleep(10000);
  try {
    const { data, error } = await supabase
      .from("players_data")
      .delete()
      .not("id", "is", null);

    if (error) throw error;
    console.log("All rows deleted successfully:", data);
  } catch (error) {
    console.error("Error deleting rows:", error);
  }
}
View full answer

19 Replies

Western paper waspOP
Here is where i use the server function :

components/Wheel.tsx

"use client";

import React, { useEffect, useState } from 'react';
import RealtimeWheel from './realtime-wheel';
import { createClientComponentClient } from "@supabase/auth-helpers-nextjs";
import { timeKeeper } from '@/lib/action/time-keeper';


const WheelOfFortune: React.FC = () => {
  const [timer, setTimer] = useState(30);
  const [gameStartTime, setGameStartTime] = useState<number | null>(null);
  const supabase = createClientComponentClient();
    
    
    interface GameStartPayload {
      new: {
        start_time: string;
      };
    }
    
    useEffect(() => {
    const channel = supabase.channel('game_time').on('postgres_changes' as any, {
            event: '*' as any,
            schema: 'public',
            table: "game_start"
        }, (payload: GameStartPayload) => {
            const startTime = new Date(payload.new.start_time).getTime();
            try {
              timeKeeper(startTime); //CALLING THE SERVER FUNCTION
              setGameStartTime(startTime);
            } catch (error)
            {
              console.error("error on executing the sc : ", error)
          }
            
        }).subscribe();
  
    return () => {
        supabase.removeChannel(channel);
    }
  }, [supabase]);


  return (
    <RealtimeWheel 
        timer={timer}
        gameStartTime={gameStartTime}
        setTimer={setTimer}
    />
);
};
export default WheelOfFortune;


The problem here is that when running my project on development side it works well with the server function running but when i push it on vercel it seems that the server function won't execute but there is no error
Western paper waspOP
I don’t have any idea why and I’m pretty stuck on this for now
@Western paper wasp Hey, so basically i created a project that need to use two different server fonction : ts lib/action/time-keeper.ts "use server" import { GameInfiniteserver } from "./cosmoperation/toggleGameInfiniteserver"; import { supabase } from "@/app/api/supabase/supabaseClient"; export async function timeKeeper(startTime: number) { console.log(`Game start time is: ${startTime}`); const delayMs = 28 * 1000; setTimeout(async () => { try{ await GameInfiniteserver("sei18j0wumtq8yewt7ka8403q7v7qfezhlsc53aq3hm7uvq8gj9xdnjs4rctnz"); //OTHER SERVER FUNCTION } catch (error) { console.error("error on executing the sc : ", error) } setTimeout(async () => { try { const { data, error } = await supabase .from('players_data') .delete() .not('id', 'is', null) if (error) throw error; console.log('All rows deleted successfully:', data); } catch (error) { console.error('Error deleting rows:', error); } }, 10000); }, delayMs); }
seem like a problem with setTimeout, try this instead
"use server";

import { GameInfiniteserver } from "./cosmoperation/toggleGameInfiniteserver";
import { supabase } from "@/app/api/supabase/supabaseClient";

async function sleep(ms: number) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

export async function timeKeeper(startTime: number) {
  console.log(`Game start time is: ${startTime}`);

  const delayMs = 28 * 1000;

  await sleep(delayMs);
  try {
    await GameInfiniteserver(
      "sei18j0wumtq8yewt7ka8403q7v7qfezhlsc53aq3hm7uvq8gj9xdnjs4rctnz"
    ); //OTHER SERVER FUNCTION
  } catch (error) {
    console.error("error on executing the sc : ", error);
  }

  await sleep(10000);
  try {
    const { data, error } = await supabase
      .from("players_data")
      .delete()
      .not("id", "is", null);

    if (error) throw error;
    console.log("All rows deleted successfully:", data);
  } catch (error) {
    console.error("Error deleting rows:", error);
  }
}
Answer
@Western paper wasp the error occur after 10 sec
are you on hobby plan?
Western paper waspOP
Yes on free plan
you will need to upgrade to pro plan if you need more than 10sec
Western paper waspOP
If i need a function to run more than 10 sec on server ?
Okkkkk that's why then !
@Western paper wasp If i need a function to run more than 10 sec on server ?
yes because the max duration is 10s for hobby plan
@Ray yes because the max duration is 10s for hobby plan
Western paper waspOP
I still have an error :
The same but now i've switched to pro plan
@Western paper wasp I still have an error :
set the maxDuration on the page.tsx where you use the action
Western paper waspOP
Basically like this ?

"use client";

import React, { useEffect, useState } from 'react';
import RealtimeWheel from './realtime-wheel';
import { createClientComponentClient } from "@supabase/auth-helpers-nextjs";
import { timeKeeper } from '@/lib/action/time-keeper';

export const maxDuration = 40;

const WheelOfFortune: React.FC = () => {
  const [timer, setTimer] = useState(30);
  const [gameStartTime, setGameStartTime] = useState<number | null>(null);
  const supabase = createClientComponentClient();
    
    
    interface GameStartPayload {
      new: {
        start_time: string;
      };
    }
    
    useEffect(() => {
    const channel = supabase.channel('game_time').on('postgres_changes' as any, {
            event: '*' as any,
            schema: 'public',
            table: "game_start"
        }, (payload: GameStartPayload) => {
            const startTime = new Date(payload.new.start_time).getTime();
            try {
              timeKeeper(startTime);
              setGameStartTime(startTime);
            } catch (error)
            {
              console.error("error on executing the sc : ", error)
          }
            
        }).subscribe();
  
    return () => {
        supabase.removeChannel(channel);
    }
  }, [supabase]);


  return (
    <RealtimeWheel 
        timer={timer}
        gameStartTime={gameStartTime}
        setTimer={setTimer}
    />
);
};
export default WheelOfFortune;
Western paper waspOP
nop its a component called on the page.tsx but thats where I use the server function
put it in page.tsx
Western paper waspOP
Ok great thank you it works !