Cannot read property of undefined on theme access in Vuetify 3 production SSR
0 reputation · 03 Feb 2024, 07:50 UTC
0 reputation · 03 Feb 2024, 07:50 UTC
A project using Vuetify 3 with Vite SSR exhibits inconsistent behavior between environments. In local development, the custom theme configuration loads as expected. However, after a production build, the application fails when attempting to access theme properties, resulting in a runtime error indicating the theme object is undefined.
The goal is to maintain a consistent theme state across both client-side and server-side rendering contexts. There is uncertainty regarding the specific requirements for the createVuetify() configuration and the vite.config.ts plugin settings necessary to ensure the theme provider is correctly registered during the production hydration process.
createVuetify() to prevent theme undefined errors during production SSR hydration?vite.config.ts plugin settings that differ between development and production builds to support theme injection?32275 reputation · 03 Feb 2024, 16:02 UTC
To eliminate the "Cannot read property of undefined" error when accessing the theme during production SSR hydration, configure Vuetify with a fully defined theme object and enable its SSR option, then ensure the Vite Vuetify plugin is present with ssr:true in both client and server builds.
The theme object is undefined on the server because Vuetify’s SSR plugin does not register the theme provider when ssr is false or when the theme is imported asynchronously. During hydration the client expects the same theme context that was rendered on the server, causing the runtime error.
ssr flag in createVuetify() that registers the theme provider on the server.{ ssr: true } so the server bundle includes Vuetify styles and theme context.Create or edit your Vuetify setup (e.g., src/plugins/vuetify.ts) and import the theme synchronously:
import { createVuetify } from 'vuetify'
import { aliases, mdi } from 'vuetify/iconsets/mdi'
import theme from '@/theme' // <-- synchronous import of your theme object
export default createVuetify({
theme: {
defaultTheme: 'default',
themes: { default: theme }
},
icons: { defaultSet: 'mdi', aliases, sets: { mdi } },
ssr: true // <-- essential for SSR
})
In vite.config.ts, ensure the Vuetify plugin is present with SSR enabled and not removed conditionally:
import vuetify from 'vite-plugin-vuetify'
import { defineConfig } from 'vite'
export default defineConfig({
plugins: [vuetify({ autoImport: true, ssr: true })],
build: {
ssr: 'src/entry-server.ts' // adjust to your server entry file
}
})
Do not place theme imports inside setup(), onMounted, or any async function; keep them at module level.
Rebuild and preview:
npm run build
npm run preview // or `vite preview`
After these steps, the server‑rendered HTML will contain Vuetify theme classes (e.g., v-application--theme-light) and the client will hydrate without the undefined theme error.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.