vinxi: "use server" chunks are duplicated in build
Is:
If you create a file with “use server” and use it in a component/route file, the file code will be duplicated to two chunks. It will be in server/chunks/build/someRoute.mjs
and also server/chunks/nitro/node-server.mjs
.
Should:
Ideally it should only be in the node-server
chunk and from there exported. The someRoute
chunk should only import the functions from node-server
.
About this issue
- Original URL
- State: open
- Created 6 months ago
- Reactions: 1
- Comments: 21 (12 by maintainers)
Commits related to this issue
- feat: implement getServerFunctionMeta Refs: nksaraf/vinxi#51 — committed to lufrai/solid-start by katywings 6 months ago
- feat: implement getServerFunctionMeta Refs: nksaraf/vinxi#51 — committed to lufrai/solid-start by katywings 6 months ago
- feat: implement getServerFunctionMeta Refs: nksaraf/vinxi#51 — committed to lufrai/solid-start by katywings 6 months ago
- Squashed commit of the following: commit f5ec449f4df13c0f74613cb305c5f06c38778af6 Author: Ryan Carniato <ryansolid@gmail.com> Date: Wed Mar 6 10:44:27 2024 -0800 update config to anticipate Vi... — committed to solidjs/solid-start by ryansolid 4 months ago
- feat: implement getServerFunctionMeta Refs: nksaraf/vinxi#51 — committed to solidjs/solid-start by katywings 6 months ago
- 0.7.x - A bunch of small changes (#1365) * transparent errors and support http status forwarding in server fns * fix FS router circular dep by moving import * update config to anticipate Vite u... — committed to solidjs/solid-start by ryansolid 4 months ago
Okay 😃. I tried to write a proof-of-concept PR for solid-start, adding id and name to the request context and exporting a
getRpcInfo
helper function from@solid/start/server
.I don’t know why, but using
getRequestEvent
in@solid/start/server
breaks thevite dev server
, it crashes with no useful error message.vite build
on the other hand works fine, with a warning that makes zero sense to me 😂:"getRpcInfo" is imported from external module "h3" but never used in "node_modules/.pnpm/vinxi@0.0.61/node_modules/vinxi/runtime/server.js"
I created a WIP PR in solid-start anyways and would love your feedback -> https://github.com/solidjs/solid-start/pull/1203
This doesn’t even need to be vinxi level. This createServerReference is created by solid-start, vinxi is runtime independent.
createServerReference
can expose anything it wants. and you can also create the url system you want. Vinxi does come with a default createServerReference, but its meant to be overriden with your own specific one.I agree the duplication and execution is not ideal.
I think this is a nice hack. globalThis is actually your best bet for truly deduped instances of things. Because both HMR and different builds can be fixed with that.
it is meant to be, so if it doesn’t probably a bug, probably need to
optimizeDeps.exclude
those dependencies that might haveuse server
in themFeel free to write up any ideas you have here, or questions about the current process.
I would do it regardless, if I can find a safe, not too much slower way of doing it, I agree that its worth pursuing, just not top priority right now.
actually the assumptions one should make while writing a Vinxi app (or apps designed for the serverless world) is that the JS global namespace is not as meaningful. Most like everything is running on different instances. This could later be scaled to different routes on different serverless functions, or ssr/server-functions on different lambda functions.
To make room for these optimizations later, I want people to break the impression that they should assume that there is some identity of these functions as a normal JS functions. For both the server and the client, you should treat as special remote code.
But regardless, yesss always looking for help in coming up with better bundling strategies. I will probably to have to write about the current strategy then.
Yeah there are some of these inefficiencies in the bundling right now but that’s because I have kept correctness the priority. Its there in someRoute because it could be called from the Component itself, so its part of that server’s bundle. It’s part of node-server.mjs because it can be called from the internet as well as a server function request.
Right now the way the builds work, the individual parts of the app ((ssr + server functions)) are built first (thus can have duplicated parts). Then the main server build takes the output of the individual parts and bundles them together. By that time its usually too late to dedupe.
We will work on better bundling strategies, but for now this is a decent compromise for the simplicity in the build process it enables. And since the output is for the server, the effect’s usually not that large because the library code is not duplicated.