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

[BUG] Schema parsing breaks in edge runtime #105

Closed
1 task done
Xexr opened this issue Apr 24, 2024 · 13 comments · Fixed by #115
Closed
1 task done

[BUG] Schema parsing breaks in edge runtime #105

Xexr opened this issue Apr 24, 2024 · 13 comments · Fixed by #115
Labels
bug Something isn't working

Comments

@Xexr
Copy link

Xexr commented Apr 24, 2024

Is there an existing issue for this?

  • I have searched the existing issues and found nothing that matches

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

@Xexr Xexr added the question Further information is requested label Apr 24, 2024
@Xexr Xexr changed the title [ASK] [ASK] A dynamic import callback was not specified Apr 24, 2024
@TheEdoRan
Copy link
Owner

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
Copy link
Author

Xexr commented Apr 24, 2024

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
Copy link
Owner

TheEdoRan commented Apr 25, 2024

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:

@TheEdoRan TheEdoRan changed the title [ASK] A dynamic import callback was not specified [BUG] Schema parsing breaks in edge runtime Apr 25, 2024
@TheEdoRan TheEdoRan added bug Something isn't working and removed question Further information is requested labels Apr 25, 2024
@Xexr
Copy link
Author

Xexr commented Apr 25, 2024

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
Copy link
Owner

TheEdoRan commented Apr 25, 2024

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
Copy link

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
Copy link
Owner

Has this already been implemented?

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

@Xexr
Copy link
Author

Xexr commented May 7, 2024

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
Copy link
Owner

@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
Copy link

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

@TheEdoRan
Copy link
Owner

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
Copy link
Author

Xexr commented May 8, 2024

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:

getsentry/sentry-javascript#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 added a commit that referenced this issue May 27, 2024
TypeSchema supports multiple validation libraries, but it can cause some issues with Vercel deployments and edge runtime. This commit moves the support for multiple validation libraries to `/typeschema` path, and root path now requires Zod as validation library.

re #105, #140
@TheEdoRan
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants