TheEdoRan / next-safe-action

Type safe and validated Server Actions in your Next.js project.
https://next-safe-action.dev
MIT License
2.26k stars 35 forks source link

[BUG] Schema parsing breaks in edge runtime #105

Closed Xexr closed 6 months ago

Xexr commented 6 months ago

Is there an existing issue for this?

Library version (optional)

7.0.0-next.28

Ask a question

Hey @TheEdoRan

I'm back, it's been a while, but fantastic to see how much next-safe-action has come on since the early days.

I'm trying to move to v7 to use the excellent middleware chaining and specifically to setup Sentry.

I'm using the latest next.28 release but keep getting this runtime error:

[TypeError: A dynamic import callback was not specified.] { code: 'ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING' }

As far as I can tell, it happens when I specify a schema and try and access parsedInput.

If I use an action which doesn't provide a schema, I don't get the error.

I'm using the latest node v20 LTS (I was using v18 LTS before, but that led to a segmentation fault error when using next-safe-action).

Other key packages:

"next": "14.2.2", "@typeschema/zod": "^0.13.3", "zod": "^3.23.4" "typescript": "^5.4.5"

Any thoughts? I'm at a loss.

Many thanks

Additional context

No response

TheEdoRan commented 6 months ago

Hi @Xexr, nice to see you back, thank you for your kind words. Regarding the issue, can you please provide a link to a minimal reproduction repo/StackBlitz environment so I can investigate on it? Thanks.

Xexr commented 6 months ago

Sure, here's a minimal reproduction.

The issue is the edge runtime, it doesn't work with edge enabled:

https://github.com/Xexr/next-safe-action-v7

TheEdoRan commented 6 months ago

I tracked down the issue to TypeSchema validate function, which is what parses and validates client input(s), a fundamental part of this library. I'm going to open an issue in the TypeSchema repo, but I don't know if this one is easily fixable. Furthermore, Vercel is apparently abandoning edge rendering.

Edit - TypeSchema issue opened:

Xexr commented 6 months ago

Thanks @TheEdoRan

I thought it was probably due to typeschema as that seemed liked a significant change.

Note that Vercel are 'abandoning' edge for their own implementation of next, eg on v0 etc but they haven't stated that they're removing it from next or the vercel platform itself. Lee clarifies further down the thread.

They state that the v20 node runtime is faster but I don't think that's conclusively proven generically vs just their implementation yet. Indeed it's counter to my own experience. Edge is still far faster and cheaper. I also fence it regionally so don't have the db round trip issue he talks about.

Theo has a great video on it, worth a watch if you're interested.

Point being, it's not dead yet.

I'd love to move to v7, it sounds great otherwise.

Let's see what the typeschema folks come back with.

TheEdoRan commented 6 months ago

Thanks for the insight. If the issue is not easily fixable on the TypeSchema side, I'll export the same safe action client stuff from the /zod path, that will just support Zod as validation library, and actually use its parse function instead of the TypeSchema one.

TaylorFacen commented 6 months ago

Thanks for the insight. If the issue is not easily fixable on the TypeSchema side, I'll export the same safe action client stuff from the /zod path, that will just support Zod as validation library, and actually use its parse function instead of the TypeSchema one.

Hi, following this issue as I'm seeing it on my end as well. Has this already been implemented?

TheEdoRan commented 6 months ago

Has this already been implemented?

@TaylorFacen not yet, but it will be soon, along with a troubleshooting section on the docs website.

Xexr commented 6 months ago

It seems to be working well so far on next.36 with the /zod path export

Thanks very much for this, super helpful @TheEdoRan 🙂

TheEdoRan commented 6 months ago

@Xexr oh nice! Wanted to test the new version with a proper project to see if everything worked as expected, but it seems you've already done it for me. So you confirm that everything is working with no issues whatsoever? Thanks!

By the way, here's the new troubleshooting page on the website.

TaylorFacen commented 6 months ago

It's working in my project as well. Thank you so much!

TheEdoRan commented 6 months ago

It's working in my project as well. Thank you so much!

Glad to hear that @TaylorFacen! Gonna close this issue since the problem seems to be solved with /zod import.

Xexr commented 6 months ago

It's been working well for me so far - no major issues.

The docs are looking good too, easy to understand.

I love how the middleware chaining enables different clients that progressively add more context where needed.

One thing I've struggled with is getting Sentry to work properly. I've followed the example you gave on another thread.

But no issues were ever logged if I just used Sentry's server action instrumentation as you documented.

I think it might be another edge related issue... see the below issue I opened with Sentry should anyone else have the same problem:

https://github.com/getsentry/sentry-javascript/issues/11884

I've got it working for now by explicitly adding the following to handleServerErrorLog as well as the instrumentation:

Sentry.captureException(e);
await Sentry.flush();

It works now and issues are captured, but I'm not convinced this is working exactly as they intended - I think withServerActionInstrumentation is supposed to automatically log errors without the need to call captureException seperately.

Anyways, great stuff Edo!

TheEdoRan commented 3 months ago

A bit late, but this issue has been solved in v7.2.0. next-safe-action now ships with built-in support for multiple validation libraries, at the time of this writing Zod (the default one), Valibot and Yup. Now you can use libraries other than Zod with the Edge Runtime. More information about that in this discussion and on this page of the website.