Skip to content
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

feat: add support for SSR builds (experimental) #210

Merged
merged 1 commit into from
May 4, 2022
Merged

Conversation

ElMassimo
Copy link
Owner

@ElMassimo ElMassimo commented May 4, 2022

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:

  • The SSR entrypoint should be a {sourceCodeDir}/ssr/ssr.js file. TypeScript and JSX are supported.
  • The output dir will have an -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:

if (import.meta.env.SSR)
  require('fs').readFileSync(...) // SSR build runs in node.js
else
  await window.showOpenFilePicker() // Client build runs in the browser

Building in SSR mode

To create an SSR build:

bin/vite build --force -- --ssr

To run the SSR build:

node public/vite-ssr/ssr.js

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 the assets:precompile task.

@ElMassimo ElMassimo merged commit e98d725 into main May 4, 2022
@ElMassimo ElMassimo deleted the feat/ssr branch May 4, 2022 17:10
ElMassimo added a commit to ElMassimo/inertia-rails-ssr-template that referenced this pull request May 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant