Blazor prerender + WASM - form submission handler execution #59193
Labels
area-blazor
Includes: Blazor, Razor Components
✔️ Resolution: Duplicate
Resolved as a duplicate of another issue
Status: Resolved
Example scenario:
Question of the day - where does
InteractiveWebAssembly submit
get printed? The answer is actually more complex than one might think. It depends on whether the WASM application has loaded and initialized* yet or not.InteractiveAuto
and the WASM has not been loaded yet, the message would've been once again printed to server console, but this time the code has access to interactivity APIs.* I am unsure of the exact switchover point, though it's not really important in this context.
The problem
Some logic requires running exclusively on server (e.g. database access). As it stands now, there is no straightforward way to force that to happen. Of course targeting WASM means that the developer should aim to interact with server via
IHttpClient
, but the mere fact that there is a possibility for static server rendering to handle the form submission forces this consideration.Of course one could register an
IHttpClient
on the server that would point tolocalhost
, but then "ambient" information such as user authentication, source IP, etc. is lost.Currently this dilemma can be avoided using 2 approaches, both sub optimal:
RendererInfo
), and then only rely onIHttpClient
- worse UX, negates benefit from SSR. Also does not really make sense in context ofInteractiveAuto
where the application can be in WS-based interactivityA potential "simple" solution
Introduce
OnSubmitServer
callback that would automatically send a request to the server even in WASM mode. This will enable something similar to the Remix framework, where a form is handled by either full page load or, if the JS is loaded, the form submission is intercepted but still sent to the server, AJAX-style, ensuring that interactivity remains (e.g. live validation).More elaborate potential solution
While form submission is the obvious "problem" here, running a piece of logic specifically on server is a common need. It could be valuable to introduce some kind of generic RPC system, e.g.
RunOnServer
would branch depending on the current render mode:Of course this will come with a lot of gotchas - the payloads (in both directions) will need to be serialized, component state will be lost, etc.
Related issues
#55307 and #51468 touch on potentially related topics
SDK
P.S. If there is an easier existing solution please do let me know 😄
The text was updated successfully, but these errors were encountered: