Is it possible to run multiple Vike builds in a single process (e.g., separate mobile/desktop bundles)? #3203
Replies: 2 comments 1 reply
|
What is your end goal? What issue are trying to solve?
There currently isn't a way to achieve that — the build But note that:
In general, I ain't sure that would be the right approach. I'm inclined to think that a cleaner appraoch would be the BFF pattern (Backend For Frontend):
While it adds (negligible?) operational complexity, it seems to be the cleanest and most future proof approach. |
|
Thanks for taking the time to respond.
The goal is to reduce the amount of JS and CSS sent to the client. Here's a contrived example of how mobile/desktop code is currently handled in my app: <HeaderMobile v-if="IS_MOBILE" :class="$style.header" />
<HeaderDesktop v-else :class="$style.header" />.header {
@include mobile {
// mobile-specific styles
}
@include desktop {
// desktop-specific styles
}
}
The issue is that users always download code for both platforms, even though only one is used. It can be mitigated with "defineAsyncComponent", but it does not fully solve the problem. The idea was to define This is something I've seen in another project (Webpack-based, no SSR), where separate platform-specific bundles are generated. |
Uh oh!
There was an error while loading. Please reload this page.
Context
I have a Vue + Vike app. The server side is an Express app (
server.js) that handles incoming requests and callsrenderPage(), which executes code from dist to generate the page output.The app serves both mobile and desktop clients, and many underlying components have entirely separate implementations for each platform. Currently, both mobile and desktop code is bundled into a single server and client build, meaning all users download code for both platforms.
What I want to achieve
I'd like to produce two separate Vike builds -
/dist/mobileand/dist/desktop- so each bundle only includes code for its target platform. Then in the Express request handler, I'd detect the platform (mobile or desktop) and makerenderPageexecute the code of the corresponding build:Current limitations
Looking at Vike's API,
renderPagedoesn't accept a build path or build reference - it operates against whichever build was loaded at startup. There's no way to switch the active build per-request.The only workaround I see is running two separate Node.js processes and routing at the reverse proxy level, which adds operational complexity.
Why
defineAsyncComponentisn't a good fitI've considered wrapping platform-specific components with
defineAsyncComponentso Vite code-splits them into separate chunks. The problem is that the split goes very deep - a page component uses sub-components, which use their own sub-components, and mobile/desktop-specific variants can appear at any level of this tree. I'd end up having to wrap a huge number of components across the whole codebase. On top of that, everydefineAsyncComponentproduces a separate chunk file, so the build would be split into a large number of small files.Question
renderPage)?All reactions