-
Notifications
You must be signed in to change notification settings - Fork 809
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
SSR Client Side Hydration Issue on new Install #1320
Comments
I think you misread. You don't need to touch The --ssr flag also already adds all of this for you: https://jetstream.laravel.com/3.x/installation.html#or-install-jetstream-with-inertia |
@driesvints This is a valid issue, I was referring to Client Side Hydration, which instructs as stated in my issue, to change the the app.js to createSSRApp instead of createApp, so rather than being re-rendered it hydrates the static html so it is reactive/etc. https://inertiajs.com/server-side-rendering#client-side-hydration
When createApp is change to createSSRApp and you make a fresh visit to /dashboard, it causes a hydration node mismatch causing the AppLayout.Vue to not render. Meaning there is an issue with something in AppLayout.Vue that causes client side hydration issues when using SSR. Unless I’m missing something & am completely wrong (wouldn’t be the first time) I would think that it’s important that Jetstream is completely compatible with client side hydration that Vue/Inertia offers, given that SSR is an install option. |
Don't have time right now but will re-open this for now. Thanks |
@CamKem thank you for logging this. Having the same issue myself. |
@driesvints @CamKem one hypothesis that @RobertBoes had: this could be caused by the HTML comments in |
I am not sure what others think should be or can be done about this? Currently I'm working on a solution that uses a flag in the NPM script to use point to different defineConfig instances in config.vite.js, that sets a node ENV variable, which we then use as a conditional in Outside of this, I think it would be beneficial if both the Jetstream & Inertia docs were updated to be a little bit more clear on SSR, specifically client side hydration. Because even if we set up run script flags to allow testing of client side hydration when also running |
@CamKem thanks so much for doing that. Not only do we need a solution for this, but this also needs to be clearly documented in both the Laravel / Jetstream / Breeze and Inertia docs. |
I've been able to replicate the issue - thanks for the detailed steps! I don't know a whole lot about SSR and hydration, but it does strike me as problematic to build and serve the SSR bundle ( If you need to modify code while running SSR and hydration, I'd imagine you'd need to use Vite in watch mode rather than HMR mode so your SSR bundle is always recompiled. Even then, you might need to stop and start the SSR server each time manually. As for the immediate mismatch - I'm wondering whether it's just a difference between what you get from The Vue docs also mention a scenario where invalid nesting (like a I'm unsure what we could document other than to discourage running a compiled SSR bundle alongside the Vite dev server. Even without hydration, I think that's going to cause unexpected results. I'm happy to reconsider if I've got it wrong, though! |
@jessarcher You are correct in your interpretation of the issue. I have done a lot of research on vite, rollupjs & terser to try and create a custom config that allows you to either allow or deny client-side hydration running & have the comments either preserved or stripped. It's possible to achieve this however I don't believe it is a good solution. For me this really comes back to needing a statement about this as a warning in both the Inertia docs & the Jetstream docs to help devs both understand and use client-side hydration & more generally SSR. Lately I have seen many people frustrated using the starter kits with Inertia due to bad SEO & they didn't feel comfortable undertaking learning about & implementing SSR in Vite/Inertia. So I feel that there should be a page in the docs that assist in this process. Possibly something like the following: If you execute If you run The reason for this is that the elements in your rebuilt assets do not match the server-rendered assets. When the assets are rebuilt, the presence of HTML comments in the HMR (hot module replacement) built assets can confuse the SSR server, as it relies on these comments for proper placement. As a result, the ability to hydrate and render the app can be affected. To avoid these issues, it's important to ensure that the So overall, when developing an Inertia web app with the goal of achieving server-side rendering (SSR), which is vital for SEO purposes, it is important to regularly rebuild the assets, restart the SSR server, and ensure that your code does not contain any problematic issues that would interfere with client-side hydration. |
Thank you @CamKem for that thorough investigation. Feel free to attempt a PR at the docs to see if Taylor would accept it 👍 |
@driesvints I'll do a PR request when I get a minute over the weekend. Well at least if it gets rejected, I have clearly label the thread & people with similar issues can find all of the data they need to understand whats going on in the comments. |
@CamKem developing SSR with constant building/rebuilding both client and server is not really a good option, as it clearly takes away all the benefits HMR has to offer. So the actual solution would be to have SSR "dev mode" available, that can be used for ironing out the SSR bugs and mismatches. Since for me the SSR was the main selling point on Inertia, I have determined to get it working, but let's see how that goes. |
Jetstream Version
3.2.2
Jetstream Stack
Inertia
Laravel Version
10.13.2
PHP Version
8.2.1
Database Driver & Version
MySQL
Description
Client side hydration is not working as expected on a fresh install of Laravel & Jetstream. With just the app installed, setup ready to go & no edits (except createSSRApp and not createApp as per the Inertia docs), when accessing
/dashboard
on first visit, it causes aHydration node mismatch
& the entire AppLayout component renders then disappears leaving just the slot component rendered. When I then look at the elements tab in devtools, there is just a couple of empty divs with display:none set on them & not representative of what rendered at all. There is no errors displayed in the terminal running the SSR server.Steps To Reproduce
The text was updated successfully, but these errors were encountered: