Replies: 9 comments 14 replies
-
Note that this would have to be an SSR-only feature. |
Beta Was this translation helpful? Give feedback.
-
I've wanted this for a long time, this would be huge! I have never wrapped my brain around exactly how this would work, but I'd be really pumped about a proof-of-concept here. |
Beta Was this translation helpful? Give feedback.
-
interesting, would it be possible to explain how this would execute ? Would Astro then automaticaly create an API endpoint, and generate client script for the post and return the result ? Would that API endpoint be protected from being called by other functions or beyond ? |
Beta Was this translation helpful? Give feedback.
-
I was just thinking about this after I saw it in the Remix discussions: Defining loaders and actions anywhere in the module graph. I saw Ryan Florence's comment where he said:
I experimented with creating the ActionFunction and LoaderFunction types similar to Remix and got pretty lucky. The LoaderFunction is basically the equivalent to Astro's frontmatter in each .astro file, but the actions are new. I created some actions and was able to trigger them on POST requests, but they still have to return HTML/the rendered Astro components. The map idea mentioned above was something I considered as well since you could have multiple actions in a single route or component. While I was able to return a single rendered Astro component from a route, I'd rather return a JSON response rather than HTML. It's convenient to return HTML if you're using something like HTMX where you'd be swapping in HTML into the DOM anyways. Most developer's consumers of the data would be Typescript components anyways (imo). At my workplace, we currently use a CMS that relies on postbacks to update pages (using ASP.NET 4.7.2), but our vendor plans to upgrade to Vue and API endpoints in the future. I think it's highly beneficial to co-locate the block components and their actions in the same space. If Astro components could have actions independent of the routes while still mapping to the route it would be a game-changer! |
Beta Was this translation helpful? Give feedback.
-
Dropping this proposal after @wassfila brought up a good point about serverless. #490 (reply in thread) |
Beta Was this translation helpful? Give feedback.
-
I would like to see some activity on this RFC again. My initial idea for the API looked very similar to matthewp's example. However, the frontmatter and how it handles state is already confusing to new users: some lines are metadata for the module, some are only executed during build, and others are executed once per request. Adding long living functions and their captured values into the mix will likely make it astro more unapproachable. I still think the astro file is the easiest place to author server functions, so I'm eager to hear ideas, but in the meanwhile, here's a pattern I'm comfortable with. Remotely callable functions are authored in and exported from src/serverfunctionsThey can be imported by any server side or client side moduleThe server build imports the actual implementationThe client build transforms all imports into fetch calls |
Beta Was this translation helpful? Give feedback.
-
Personally I like OP's example regardless of if it works with serverless environments. Put up a disclaimer in the docs and call it macaroni imho. Surely there's ways long term to convert the functions in serverless mode to spin up new endpoint shenanigans instead of an RPC. This would be too huge of a win to not head this direction if you ask me. |
Beta Was this translation helpful? Give feedback.
-
SummaryAstro is uniquely positioned to offer a way to author client-callable server-side functions with fewer caveats than comparable solutions. Background & MotivationThis RFC is inspired by the recent advent of framework-native RPC solutions in the JS ecosystem. Next.js users can leverage server components, and Nuxt has an active RFC. I think Astro could do something even better, with fewer of the downsides of server functions elsewhere. OverviewNote: All naming is tentative. Authoring server functionsServer functions can be authored in two places:
In
|
Beta Was this translation helpful? Give feedback.
-
Closing as we have Astro actions now |
Beta Was this translation helpful? Give feedback.
-
Body
Summary
It would be great if you could pass a function to a client component by reference. The function would exist on the server and the client could call it; effectively an RPC.
Background & Motivation
Inspired by the recent implementation of "server functions" in the JS ecosystem, I think Astro could do something even better, with fewer of the downsides of server functions elsewhere.
With server functions you write a function in client code that runs on the server. This means you can't access variables within the closure of the function. Some frameworks do "scope copying", which serializes the scope and passes it to the RPC call. This only works with serializable variables and can be expensive to serialize a large closure.
Since Astro is server-first, we could simply pass a function; the actual function with its real closure, to the client as a reference.
Goals
Example
Beta Was this translation helpful? Give feedback.
All reactions