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.
Long overdue, we're finally readying for Inertia Rails SSR!
First off, a huge thanks to @zealot128 for their work on #73 . I had already started this version so I wanted to push forward with it, but their code was a great help in ensuring we were all thinking about things in a similar way.
Part of the hold up here ha been that Rail's Webpacker is not quite as fluid as Laravel Mix so I wanted to wait until I felt like we had a solid way of recommending the JS setup. After a lot of tinkering, I'm happy to say that I think we do!
First off, the InertiaRails portion of this is very simple, InertiaRails gets 2 new config values that can be set (the values shown are the defaults):
Backend
Frontend
Now comes the harder part, the javascript:
First things first, let's add a new dependency.
More information can be found in the Inertia SSR docs: https://inertiajs.com/server-side-rendering
Unfortunately, Laravel Mix doesn't exist for rails, so the docs can't help us much further (though they do give us the contents of one more file).
I created/modified the following files:
Create
app/javascript/ssr/ssr.jsx
This comes directly from the Inertia docs and serves as the entry point for Inertia SSR.
Create
config/webpack/ssr.js
This file is unique to rails but is similar in spirit to Inertia's
ssr.mix.js
file. The basic idea is that the SSR node process is distinct from our usual JS, so we want to configure it a bit differently.Edit
config/webpacker.yml
and add the following entry:This entry will allow us to keep our ssr js separate since it's not really a pack in the rails sense.
Edit
babel.config.js
Change the individual lines that look like
to
This let's us specify our new ssr node env, but otherwise treat it like production.
Edit
app/views/layouts/application.html.erb
and add the following line inside of the head tagsThis allows Inertia SSR to add to the document head
And finally
Edit
package.json
and add (or modify if you already have one) to add a scripts sectionThis will allow you to run the SSR server locally with the command
yarn run ssr
(but keep in mind that it does NOT hot reload, you will need to kill and re-run this command when you make JS changes!)And that should be that! Now kickback and disable your javascript!
If these changes work well for most users, we'll get them moved into the templates.
Outstanding questions
For those of you who need SSR, do you prefer to use SSR in development? As of right now, the best workflow from an ease of use standpoint would be to use traditional Inertia in development and SSR only in production. Obviously, this runs of the risk of accidentally using client only javascript or otherwise breaking the production build despite working fine in development. For those of you using this, is that an acceptable tradeoff or should we invest some time in making SSR in development a better experience?