pingdotgg / uploadthing

File uploads for modern web devs
https://uploadthing.com
MIT License
4.28k stars 319 forks source link

[bug]: Production Environment - Error 504 #475

Closed pat-mw closed 1 year ago

pat-mw commented 1 year ago

Provide environment information

System:
    OS: Linux 6.1 Debian GNU/Linux 11 (bullseye) 11 (bullseye)
    CPU: (8) x64 Intel(R) Core(TM) i7-10610U CPU @ 1.80GHz
    Memory: 12.28 GB / 14.16 GB
    Container: Yes
    Shell: 5.1.4 - /bin/bash
  Binaries:
    Node: 20.9.0 - ~/.config/nvm/versions/node/v20.9.0/bin/node
    npm: 10.1.0 - ~/.config/nvm/versions/node/v20.9.0/bin/npm
    bun: 1.0.7 - /usr/local/bin/bun
  npmPackages:
    @uploadthing/react: ^6.0.0 => 6.0.0 
    typescript: ^5.1.6 => 5.2.2 
    uploadthing: ^6.0.1 => 6.0.1

Describe the bug

In my dev environment, Uploadthing works exactly as expected. In my production environment, however, I'm having a bug, without much useful context:

Something went wrong. Please contact UploadThing and provide the following cause: SyntaxError: Unexpected token 'A', "An error o"... is not valid JSON

I'm reporting as the bug message told me to. It's 3am for me and I don't want to dig any deeper right now but I'll try and look into it properly in the next few days.

If anyone else has encountered this, in the meantime, please let me know :_

Link to reproduction

https://mrdn.vercel.app

To reproduce

Log in via Discord or Auth0, go to:

https://mrdn.vercel.app/dashboard/new-listing

Use file uploader.

See error

Additional information

Weirdly enough, It seems as though the files are indeed being uploaded (I can see them on the uploadthing dashboard) so I'm assuming the issue is something to do with the callbacks

Also, in case it helps.

It seems as though I'm getting a 504 from uploadthings api.

Then, my server-side logs (from Vercel) show the file succesfully uploaded.

Then, there is a 10second timeout.

image

๐Ÿ‘จโ€๐Ÿ‘งโ€๐Ÿ‘ฆ Contributing

Code of Conduct

pat-mw commented 1 year ago

This may help add context.

This is my uploadthing router, following the docs down to a tee (but here you can see where the logs are)

import { createUploadthing, type FileRouter } from "uploadthing/next";
import { getServerAuthSession } from "@/server/auth";
const f = createUploadthing();

const auth = async (req: Request) => {
    const session = await getServerAuthSession();
    if (!session) return null;
    if (!session.user) return null;
    return session.user;
};

// FileRouter for your app, can contain multiple FileRoutes
export const ourFileRouter = {
  // Define as many FileRoutes as you like, each with a unique routeSlug
  imageUploader: f({ image: { maxFileSize: "2MB", maxFileCount: 10 } })
    // Set permissions and file types for this FileRoute
    .middleware(async ({ req }) => {
      // This code runs on your server before upload
      const user = await auth(req);

      // If you throw, the user will not be able to upload
      if (!user) throw new Error("Upload Unauthorized - Please log in...");

      // Whatever is returned here is accessible in onUploadComplete as `metadata`
      return { userId: user.id };
    })
    .onUploadComplete(({ metadata, file }) => {
      // This code RUNS ON YOUR SERVER after upload
      console.log("Upload complete for userId:", metadata.userId);

      console.log("file url", file.url);

      // !!! Whatever is returned here is sent to the clientside `onClientUploadComplete` callback
      return { uploadedBy: metadata.userId, fileUrl: file.url};
    }),
} satisfies FileRouter;

export type OurFileRouter = typeof ourFileRouter;
pat-mw commented 1 year ago

Also, I noticed that - whilst this error is not thrown on Dev, there is times when the uploadthing uploader gets stuck even after the upload is completed.

This only happens sometimes, and I don't really know the reason why -- whilst developing I have just refreshed the page and tried again and it usually works the second time.

Another bit of context (which i think is unrelated but I'm just being padantic): I'm using the SSR implementation with suspense as recommended in the uploadthing guide.

https://docs.uploadthing.com/getting-started/appdir#optional-uploadthing-ssr-plugin

viniciuspjardim commented 1 year ago

The same error is happening to me, even the Vercel timeout errors are the same. I'm adding UT to the project today, so I might have missed something. I'll probably investigate more tomorrow!

"@uploadthing/react": "^6.0.0",
"uploadthing": "^6.0.1",
"next": "^14.0.3",

image image

Zolastic commented 1 year ago

im facing this error too

markflorkowski commented 1 year ago

latest release should resolve this. https://github.com/pingdotgg/uploadthing/releases

pat-mw commented 1 year ago

quick testing shows it works, thanks @markflorkowski -- I'll report back if the issue comes up again but seems to be fine now.

Super quick to respond, great work team (y)