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
Add a <server> block to SFC to combine server code with client code in one file. #20802
Comments
I am currently using nuxt-server as well If this method is used, how can we solve the client rendering problem of nuxt-link, Nuxt is currently only the first screen SSR. After nuxt-link jumps, there is no DB. query ("SELECT * From Users") on the client; |
After a nuxt-link jump, the code |
I think this would have some of the same benefits as server components:
The difference with server components:
|
Instead of |
I think this is a great candidate for implementing in user-land with a Nuxt module. I've done something similar as an experiment which I plan to open-source soon via a PR to https://github.com/TanStack/bling. |
Referencing comment on suggested API : #19772 (comment) |
@HendrikJan In case you're looking for something similar, I just found out about a really interesting module that does this : https://www.npmjs.com/package/numix I'm thinking about using this idea and adding it into https://github.com/Hebilicious/form-actions-nuxt |
Initial implementation https://github.com/Hebilicious/server-block-nuxt is out 🎉 Please try it and leave feedback. PR welcome ! |
@Hebilicious I think it is an interesting and nice take on the subject. I think, for the current version of Additionally, I feel like the default generated api routes could be clashing with existing user ones quite too often (without using the As a side note: I am thinking about something like Question: How would a route like |
Thanks for the feedback @BananaAcid
On a side note regarding #21370 , I would like to see the API routes on the manifest. |
@danielroe Can we consider this completed with the server block module ? Maybe in the future this can be brought directly into Nuxt, but I haven't seen that much interest/usage, so it's probably fine as a module ? |
Feedback welcome on this if anyone uses and finds it helpful 🙏 |
Describe the feature
Currently you can create an API in the
/server
folder and then consume the API in your components (/pages
or/components
).This means:
With this proposed feature you could instead combine this in one SFC file:
This should work both with SSR as in the client without the programmer noticing any difference.
On the server (SSR):
When run on the server, the variables exported in the
<server>
block would directly be available in the<script>
block.On the client:
When run on the client, an Ajax call will automatically be made (without the user having to program anything) to get the variables from the server.
This way the code in the
<server>
block will always run on the server, while the exported variables will always be available in the<script>
block.Inspiration:
I was inspired by Inertia.js where Laravel computes variables that become available in the component props. This works both when Laravel renders the SFC on the server (the variables are injected somehow) as on the client (some magic transfers the variables from the server to the client).
What makes this feature usefull?
What could be downsides?
Additional information
Final checks
The text was updated successfully, but these errors were encountered: