feat: add support for SSR builds (experimental) #210
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description 📖
This pull request adds support for SSR builds, using an approach based on this prototype.
The goal is to support use cases such as SSR with Inertia, and pave the way for other cases that benefit from an SSR build.
Background 📜
By detecting whether the
ssr
flag was set when triggering the build,vite-plugin-ruby
will set an SSR entrypoint instead, and skip any output options related to fingerprinting the resulting assets.This is experimental, these initial conventions are subject to change in patch releases:
{sourceCodeDir}/ssr/ssr.js
file. TypeScript and JSX are supported.-ssr
prefix to be separate from client-side builds, for example:public/vite-ssr
.SSR 🚀
The SSR entrypoint can import any universal code (supporting both node.js and browsers), and leverage the
~/
alias as needed.You can leverage
import.meta.env.SSR
to execute different code depending on the environment, for example:Building in SSR mode
To create an SSR build:
To run the SSR build:
Upcoming 🔮
EDIT: Shipped the following improvements in #212.
This experimental version does not address build caching, which requires further changes in
vite_ruby
to ensure the SSR build is not skipped (for now, using the--force
flag should do the trick).It would be desirable to customize the output dir of the SSR build (to be outside of
public
, for example).Finally, perhaps adding an
ssrBuildEnabled
setting to automatically run an SSR build along with theassets:precompile
task.