Skip to content

Fix Inertia SSR errors #53

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

Merged
merged 13 commits into from
Mar 5, 2025

Conversation

JoeyMckenzie
Copy link
Contributor

There were a handful of errors when running SSR with Inertia, primarily around accessing the window and missing some middleware that's required for SSR contexts. This PR resolves those issues. I haven't checked the Vue starter kit yet, though Is suspect that same thing is probably happening there as well.

@Linux123123
Copy link

Using SSR will also cause a flash of light theme for users who have previously selected a dark theme because the theme is saved in localstorage. I would love to see persisting theme in session or some other way, so we can inject the theme class in app blade during SSR and not have to wait for javascript to change it.

@JoeyMckenzie
Copy link
Contributor Author

Yeah, cookies can also work and I've gotten by using that route as well. Not sure if it necessarily falls on the responsibility of the starter kit though, as it's fairly easy to setup in user land if that's desired in the scaffolded app.

@JoeyMckenzie
Copy link
Contributor Author

For those stumbling upon this wondering about the dark mode flash, I have a branch demoing how to prevent it using this starter kit based on the changes from this PR. TL;DR, use a cookie to keep the dark mode state.

// For SSR, we can't access the window location, so can only render the layout on the client
if (typeof window === 'undefined') {
return null;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is url in usePage hooks, why not just use that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We certainly could, both will accomplish the same thing I'm pretty sure. This is just a preference of being explicit about rendering on the server.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess in that context I prefer that way too, thanks for the answer.

@JoeyMckenzie JoeyMckenzie force-pushed the fix/inertia-ssr-errors branch from 90fcf59 to 011ee04 Compare March 2, 2025 22:09
Copy link
Contributor

@tnylea tnylea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really love the composer run dev:ssr command.

Thanks for the PR. I just tested it out and it works great.

@tnylea tnylea added the Approved Approved for Merge label Mar 3, 2025
"include": ["resources/js/**/*.ts", "resources/js/**/*.tsx"]
"include": [
"resources/js/**/*.ts",
"resources/js/**/*.d.ts",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would "resources/js/**/*.d.ts" not already be targeted with "resources/js/**/*.ts"?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is necessary, but I feel like it's not a bad thing to include because it helps with clarity. Some devs perfer to explicitly include the .d.ts 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RV7PR, correct. Technically not necessary, but I like to include them to be very explicit that the project include type declaration files. Similar to how the original Breeze templates worked as well.

composer.json Outdated
@@ -55,6 +55,11 @@
"dev": [
"Composer\\Config::disableProcessTimeout",
"npx concurrently -c \"#93c5fd,#c4b5fd,#fb7185,#fdba74\" \"php artisan serve\" \"php artisan queue:listen --tries=1\" \"php artisan pail --timeout=0\" \"npm run dev\" --names=server,queue,logs,vite"
],
"dev:ssr": [
"npm run build",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JoeyMckenzie This should run npm run build:ssr instead of npm run build. Go ahead and get that updated and we'll merge this in.

Really appreciate the help!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, updated.

@tnylea
Copy link
Contributor

tnylea commented Mar 4, 2025

@JoeyMckenzie Thanks for the PR. It's a good point what @Linux123123 brings up about the light mode flicker with SSR.

I really like your progress with the Cookie solution. I think we'll wait to merge this in until we get this resolved.

Also, I've created a PR for the Inertia SSR fixes to the Vue Starter Kit here, which I now see that you have left some comments in there as well. Thanks for that 😉

I was wondering if there is any advantage to use localstorage for non-ssr and cookies for SSR or if we should just use cookies for both. Let me know your thoughts.

@JoeyMckenzie
Copy link
Contributor Author

@tnylea Hacked on this a bit yesterday, coincidentally arriving at very similar to your last commit here (used Inertia middleware instead, both do the same thing). That fixes the SSR flashing, there's still the issue of CSR flashing though that should be fairly simple to just inline a <script> in the head and determine the style to set on the document.

Definitely should be considering using both local storage and cookies, since some folks can choose to disable cookies entirely. Relying on them solely would most likely break those user experiences, but it's definitely the solution for preventing the flash in SSR.

Nice work on that last commit, I like it!

@tnylea
Copy link
Contributor

tnylea commented Mar 5, 2025

@JoeyMckenzie, thanks!

I'm sorry. We should have collaborated a little more if we had both been working on this solution. I agree. Using both cookies and local storage has its benefits.

Who would have thought there would be so much involved with Light/Dark mode 🤣

But ultimately, this feels like a good solution. Let me know if you have any other feedback; otherwise, I'll sync up with the team and see if we can merge this soon.

Thanks again!

@eznix86
Copy link

eznix86 commented Mar 5, 2025

Thanks @JoeyMckenzie I cherry-picked those commits, works fine, almost in prod with those, hope this is merged !

@JoeyMckenzie
Copy link
Contributor Author

@tnylea No worries! I'm just happy to be here and help where I can. 🤠

The only last bit I think we'd need is on that last commit by me, just adding some inline styles that match the shadcn styles set app.css to prevent the flash for CSR. In that scenario, even though the dark class is set on the document, we still have to wait for vite assets to load in and apply styles correctly.

An inline style will prevent the flash, with the only nuisance being keeping the background colors in sync with whatever shadcn themes the user is running.

Totally open to changing that too, more of just a quick 'n dirty solution to fix the CSR flash. Let me know.

@taylorotwell taylorotwell merged commit 01805e5 into laravel:main Mar 5, 2025
2 checks passed
@taylorotwell
Copy link
Member

Thanks all. 👍

egxadev pushed a commit to egxadev/laravel-react-starter-kit that referenced this pull request Mar 6, 2025
* fix: check for window when running ssr

* chore: remove ssr files

* fix: add ziggy to inertia middleware for ssr

* chore: remove bootstrap ssr

* chore: add script for ssr

* chore: update composer script

* Adding solution for Dark Mode flicker with SSR

* Adding condition for system appearance and detecting via client-side immediately

* feat: add inline style to prevent flash during CSR

* formatting

* package update

---------

Co-authored-by: Tony Lea <[email protected]>
Co-authored-by: Taylor Otwell <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Approved Approved for Merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants