Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onUploadCompleted not triggered in vercel deploiement #687

Open
JeremCy opened this issue May 28, 2024 · 4 comments
Open

onUploadCompleted not triggered in vercel deploiement #687

JeremCy opened this issue May 28, 2024 · 4 comments

Comments

@JeremCy
Copy link

JeremCy commented May 28, 2024

Package.json info: "next": "14.2.3", "@vercel/blob": "^0.23.3"

Hello,
I was developing a open application form for my application,
when i test it using ngrok the onUploadCompleted is triggered and update my job application table.

When i try the same test in the vercel environement the create job application and the upload is working, but not the update to cv_url column.

form action function:

async function action(data: z.infer<typeof schema>) {
    try {
      setLoading(true);
      const file = data.cv;
      const fileNames = file.name;
      const apiCall = await CreateCandidature(data);
      if (apiCall.status === 201) {
        let candidature: Candidature = await apiCall.json();
        await upload(fileNames, file, {
          access: "public",
          handleUploadUrl: "/api/cv/upload",
          clientPayload: candidature.id.toString(),
        });
        toast({
          title: "Candidature created",
          description: "Candidature has been created successfully",
        });
        setLoading(false);
        form.reset();
      } else {
        setLoading(false);
        toast({
          title: "Error",
          description: "An error has occurred",
        });
      }
    } catch (error) {
      setLoading(false);
      console.error(error);
      toast({
        title: "Error",
        description: "An error has occurred",
      });
    }
  }

my upload route:

export async function POST(request: Request): Promise<NextResponse> {
  const body = (await request.json()) as HandleUploadBody;
  try {
    const jsonResponse = await handleUpload({
      body,
      request,
      onBeforeGenerateToken: async (pathname, clientPayload) => {
        // Generate a client token for the browser to upload the file
        // ⚠️ Authenticate and authorize users before generating the token.
        // Otherwise, you're allowing anonymous uploads.

        return {
          allowedContentTypes: [
            "application/pdf",
          ],
          tokenPayload: JSON.stringify({
            pathname,
            clientPayload,
          }),
        };
      },
      onUploadCompleted: async ({ blob, tokenPayload }) => {
        // Get notified of client upload completion
        // ⚠️ This will not work on `localhost` websites,
        // Use ngrok or similar to get the full upload flow

        console.log("blob upload completed", blob, tokenPayload);
        try {
          // Run any logic after the file upload completed
          //update candidature
          if (!tokenPayload) throw new Error("No token payload found");
          const payload = JSON.parse(tokenPayload);
          console.info("Updating candidature payload=" + payload);
          !payload?.clientPayload
            ? console.error("No client payload found")
            : console.info("Client payload found");
          const candidature = await prisma.candidature.update({
            where: { id: Number(payload.clientPayload) },
            data: {
              cv: blob.url,
            },
          });
          console.info("Candidature updated", candidature);
        } catch (error) {
          console.error("Error updating candidature", error);
          throw new Error("Could not update candidature");
        }
      },
    });

    return NextResponse.json(jsonResponse);
  } catch (error) {
    return NextResponse.json(
      { error: (error as Error).message },
      { status: 400 }, // The webhook will retry 5 times waiting for a 200
    );
  }
}
@LFVH
Copy link

LFVH commented Sep 4, 2024

Hi! I've just had a problem where blob was uploaded but onUploadCompleted would not trigger so I've tried the same "happy path" with another domain (assigned to my deployment by vercel) and it has worked :D

@voldmar
Copy link

voldmar commented Sep 6, 2024

Hey,

I have the same problem. Do you have Vercel Authentication for deployments protection turned on?

@JeremCy
Copy link
Author

JeremCy commented Sep 11, 2024

Hey @voldmar,
Yes I have the Vercel Authentication for deployments protection turned on.

@voldmar
Copy link

voldmar commented Sep 11, 2024

@JeremCy Looks like it blocks the callbacks. I am making all the callback stuff after the upload

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants