Next.js Discord

Discord Forum

Route Handlers returns a 404 error (with no error) - when trying to use PDFKit

Answered
Milkfish posted this in #help-forum
Open in Discord
MilkfishOP
Hi everyone, I'm trying to use a Route Handler to dynamically generate a PDF document using PDFKit.

Running into an odd error where it simply returns 404 - but if I remove the PDF document code, the route handler returns correctly.

It seems like the import PDFDocument from 'pdfkit' code is causing an error with the route handler, perhaps a bundler error - but not returning any error to the console
Answered by Milkfish
well damn. That was my issue after all... looks like my next config was never being loaded, hence the component not being bundled correctly.

Still not sure what the weird bug was earlier around no error being shown at all - I think there is probably still a pnpm or monorepo issue amongst it too,
View full answer

31 Replies

MilkfishOP
@Milkfish Click to see attachment
most likely the purchase == null path is reached
then it is a logic bug
add a console.log before return notFound() and see if it is logged
btw you don't need the return before notFound(); notFound() throws an error so return isn't necessary
MilkfishOP
That's the weird part, the purchase is found...
Even if I put a response right at the top of the function, I still get a 404
If I comment out the PDFDocument, then the code returns 'Hello world'...

So seems like it's an issue with importing from PDFKit, which I'm at a loss of how to correct
@joulev btw you don't need the `return` before `notFound()`; `notFound()` throws an error so `return` isn't necessary
MilkfishOP
On a side note, I'm only using the return so typescript can catch any unavailable code in the future!
@Milkfish On a side note, I'm only using the return so typescript can catch any unavailable code in the future!
notFound() returns never so you don't need to do that
@joulev `notFound()` returns `never` so you don't need to do that
MilkfishOP
Awesome, didn't realise that
@joulev this part then idk though, it looks really weird
MilkfishOP
Yep, no idea. I'm not even getting any error output which is the most frustrating part because I have no idea where to start on debugging it
can you give me a minimal reproduction repository?
MilkfishOP
Might be a PNPM monorepo issue - trying to do a minimal reproduction on code sandbox is showing me errors
MilkfishOP
So after bailing out to npm, I'm getting errors (hooray!) - not sure if I should raise another post, but now getting the following error...

I've tried to add pdfkit to the experimental serverComponentsExternalPackages configuration with no luck yet
@Milkfish Click to see attachment
try deleting .next and try again
MilkfishOP
Just tried, still the same issue
then can you give me a github link to a minimal repro so i can clone?
MilkfishOP
working on it, thank you
MilkfishOP
So...
Does next.js not support the config in .cjs format?
My reproduction was working perfectly.... and I just changed my config format from cjs and boom, my repo was working perfectly
what config?
MilkfishOP
(If it doesn't I feel real dumb)
next.config
it does support both commonjs with next.config.js and ecmascript modules with next.config.mjs
but i have never seen next.config.cjs, can't say if it works or not, likely not
MilkfishOP
well damn. That was my issue after all... looks like my next config was never being loaded, hence the component not being bundled correctly.

Still not sure what the weird bug was earlier around no error being shown at all - I think there is probably still a pnpm or monorepo issue amongst it too,
Answer
MilkfishOP
Thanks for the (indirect) help!
nice, glad it worked