Support for Custom Server Wrapper and Custom Exports #3334
Replies: 3 comments 5 replies
|
I found a solution (described below) for the build. // server/entrypoint.ts
import 'virtual:@brillout/vite-plugin-server-entry:serverEntry'
export { default } from './app' // export server app as default
// vite.config.ts
{
name: 'emit-server-index',
apply: 'build',
enforce: 'post',
config() {
return {
environments: {
ssr: {
resolve: {
noExternal: true
},
build: {
rolldownOptions: {
input: {
index: 'server/entrypoint.ts' // build dist/server/index.mjs from server/entrypoint.ts
}
}
}
}
}
}
}
}Note: @brillout @magne4000 how can I add the /api route in development with |
|
@rtritto Again, this isn't our plan and we're quite busy with higher priorities. We'll work on this later (most likely #3233, no ETA). In the meantime you can use If you want to contribute, then spend a substantial amount of time to understand our perspective. Carefully re-read every message we sent you. You can push back, but then make it as clear, as succinct, and as easy as possible for us to understand. We unfortunately all have a hard time trying to understand you, and we have to repeat ourselves quite a lot (I unfortunately feel like you aren't listening). Thanks for understanding. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Vike currently integrates Universal Deploy, which uses a default serve file as the server wrapper for the Node environment.
A user should be able to create and use a custom server wrapper instead of being forced to use the default one that doesn't support any export.
The Problem: Custom server wrapper & Server entry (
dist/server/entry.mjs)On using a custom server wrapper (e.g.
server/entrypoint.tsasexport default app), Vike throws the errorThe server production entry is missing(see #3229) during production runtime.This happens because the
dist/server/entry.mjsgenerated by Universal Deploy is not being imported. To fix this,dist/server/entry.mjsneeds to be imported intodist/server/index.mjsduring the build step.This could be solved with two different approaches injecting
virtual:vike:global-entry:server:if (resolvedEntry && id === resolvedEntry) { - if (!code.includes("virtual:ud:")) this.warn(`{ entry: "${entry}" } is missing "virtual:ud:catch-all" import.`); + const s = new MagicString(code); + s.prepend(`import 'virtual:vike:global-entry:server'\n`); // or use a new UD option like `virtualEntry` to pass `virtual:vike:global-entry:server` from Vike + return { + code: s.toString(), + map: s.generateMap({ hires: true }) + }; }Since Universal Deploy is already integrated into Vike, Vike could easily expose the entry option (see Vike Feature Request that uses universal-deploy PR #31).
Without Universal Deploy disabled (so without the
+serverfile), users are forced to install third-party packages that is just to boot their custom server wrapper.The Use Case: Custom Exports
Why is a custom server wrapper so important? Because in production environments, developers often need to define custom exports directly from the server bundle. Currently Vike doesn't support custom exports (e.g. exports defined in the
+serverfile are ingnored and replaced with the content of the Universal Deploy'sserve)An use case is with the manual integration in Vercel (see manual integration of Vercel):
To handle standard SSR requests, we need to export the fetch handler:
Sometimes we need to run background tasks using the same server context:
A custom server wrapper is essential here, as it would allow developers to gracefully export the server app instance alongside any custom exports needed by the deployment platform.
Reproductions
References
+server.jswithrenderPage()instead of Universal Deploy #3233All reactions