Open rchl opened 1 year ago
Hey there @rchl, thanks for the suggestion. The good news is that you already can do this! ☺️ The PheroClient
expects an implementation of fetch
, so you can add something like isomorphic-fetch to make it work server-side. One catch is that it could be that the CLI isn’t optimized for a project that is both a phero-server and a phero-client at the same time. But if your server is a separated project from your nuxt-project, you could totally make that work right now.
I believe that isomorphic-fetch
would still make a network request, just not using fetch
but some alternative request library or even pure HTTP.
I'm thinking about just making a pure function call.
Ah right, I see! Interesting thought. In our experience (with Next) doing an http call in these cases isn’t all that bad, but I get what you want to do. I’m thinking: if you’ve got access to the actual functions, to be able to call them, why would you need a client at all? Couldn’t you call the actual function in those cases? You would need to make a difference between a call from the nuxt-server and next-client, and find a way to share code between your nuxt-server and phero-server, but if you don’t want to go that route, I’d stay with doing an http call for the sake of simplicity.
Having access to the functions would in theory work in simple cases but would require importing those functions separately for the server-side. That would be an issue for files that are "hybrid" in nature, like for example Vue files, in which case those can be processed on both the server and client side so having server-specific imports wouldn't really work there.
Also, things like middleware support wouldn't work out of the box as that is something that would likely need to be handled within such server-side client.
That said, I understand if that's something that is not in scope of this package. At least until it's more mature and maybe you can start thinking about adding more features. I would anyway for Nuxt also prefer to have some direct integration with express
instead of starting separate server to handle those API request but that's a separate feature request.
BTW. I'm one of the maintainers of https://github.com/ezypeeze/nuxt-neo which is a module for Nuxt that provides functionality similar to Phero and uses this hybrid approach of calling API functions directly on the server but doing a HTTP requests on the client. The part that is missing from it, and what makes it not so convenient to use, is the lack of auto-generation of types that Phero has. So I'm just dreaming of a combined approach.
We’re going to work on clear example projects with combinations of different kind of stacks. It could very well be that we come up with a nice approach in that process. Excellent point about middleware.. Integration with an existing expres server is something we already talked about by the way, I’ll keep you posted about that!
I haven't used this project yet but as a concept it is very interesting. I'm thinking that it could be used in frameworks like
Nuxt
which handle both server and client-side rendering.From the documentation it appears that when using Phero it will always make a HTTP request to the server so my suggestion is to also generate a server-side client that could be used to call the API functions directly. That way, in frameworks like Nuxt, if the page is loaded on the server side, a server-to-server HTTP request could be avoided and instead the API function would be called directly.