-
Notifications
You must be signed in to change notification settings - Fork 939
fix(inertia): set serverRendered dynamically to prevent SSR crash #5396
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
Conversation
commit: |
|
Nice! I'll have a look. |
|
Even with this fix, how do you manage the FOUC? (unhead does not inject the styles and I'm not understanding why, could be related to the head management of Inertia) ui-inertia-ssr-fouc.mp4 |
|
Hi, The issue is that Inertia handles its own head management, so it doesn't pick up the tags generated by Nuxt UI's internal The fix is to create a dedicated Here's my createServer(
(page) => {
const head = createHead();
return createInertiaApp({
page,
render: renderToString,
resolve: (name) =>
resolvePageComponent(
`./pages/${name}.vue`,
import.meta.glob<DefineComponent>("./pages/**/*.vue")
),
setup: ({ App, props, plugin }) =>
createSSRApp({ render: () => h(App, props) })
.use(plugin)
.use(head)
.use(ui),
}).then(async (app) => {
const payload = await renderSSRHead(head);
app.head.push(payload.headTags);
return app;
});
},
{ cluster: true }
);To prevent the theme from flashing on hydration, i use a small script in <script>
try {
const theme = localStorage.getItem('vueuse-color-scheme');
if (theme === 'dark' || (theme === null && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
document.documentElement.classList.add('dark');
} else {
document.documentElement.classList.remove('dark');
}
} catch (e) { /* ignore */ }
</script>On a related note, I saw the Laravel starter kit still uses Ziggy, while the official Laravel kits have moved on to Wayfinder(laravel/vue-starter-kit#178). I'd be happy to open a separate PR to update it and include these SSR fixes. I've already implemented this in my own starter kit if you want to take a look: https://github.com/y-l-g/saasterkit. By the way https://saasterkit.com is server rendered. Let me know if this approach works for you. |
|
Thanks @y-l-g π A PR on https://github.com/nuxt-ui-templates/starter-laravel would be greatly appreciated, I made this template based on the Laravel Kit back then but in all honesty I know nothing about Laravel π¬ |
π Linked issue
Resolves #5254
β Type of change
π Description
The Inertia stub currently hardcodes the payload.serverRendered value to false. This causes the application to crash during Server-Side Rendering (SSR). This change replaces the hardcoded value with a dynamic check (typeof window === "undefined") to accurately determine if the code is running on the server.
This solve SSR crashing with
ReferenceError: document is not definedπ Checklist