SSR Compatibility #
KawaPress performs server-side rendering (SSR) for initial requests during development and again during production builds. Custom code in pages, components, and Runtime Plugins must work in both Node.js and browser environments.
Read the Vue Server-Side Rendering guide for a broader introduction to SSR.
Access Browser APIs #
Browser APIs such as window, document, navigator, and localStorage do not exist on the server. Do not access them directly while setup runs.
Move browser-only work into onMounted():
<script setup lang="ts">
import { onMounted, shallowRef } from 'vue'
const viewportWidth = shallowRef<number>()
onMounted(() => {
viewportWidth.value = window.innerWidth
})
</script>
<template>
<span v-if="viewportWidth">Viewport width: {{ viewportWidth }}px</span>
</template>onMounted() does not run during SSR, so window is only accessed in the browser.
Handle Dependencies That Access Browser APIs on Import #
Some libraries access window as soon as their module loads. Calling the library only from onMounted() is not enough if a static import at the top of the file still runs during SSR.
Move the import itself into onMounted():
<script setup lang="ts">
import { onMounted } from 'vue'
onMounted(async () => {
const { mountWidget } = await import('browser-only-widget')
mountWidget()
})
</script>Regular modules can also branch on import.meta.env.SSR:
if (!import.meta.env.SSR) {
const { startTracking } = await import('browser-only-library')
startTracking()
}Keep the import dynamic inside the branch. Static imports always run when the module is evaluated.
Render a Component Only on the Client #
Wrap a component that needs the DOM while rendering in Core's globally registered <ClientOnly>. The server and the browser's first render both produce an empty node; the component appears after mounting, without a hydration mismatch:
<script setup lang="ts">
import { defineAsyncComponent } from 'vue'
const ClientWidget = defineAsyncComponent(
() => import('./ClientWidget.vue'),
)
</script>
<template>
<ClientOnly>
<ClientWidget />
</ClientOnly>
</template>ClientOnly can also be imported from kawapress/client for use in a regular Vue component.
Use useAppearance() when a component needs to follow the light or dark appearance:
import { useAppearance } from 'kawapress/client'
const { isDark } = useAppearance()It follows the dark / light class on <html>, or the operating system preference when neither class is present. isDark updates when the theme toggles that class.
Use a Browser Plugin from a Runtime Plugin #
The same Runtime Plugin source enters both the SSR and client module graphs. Dynamically import a Vue plugin in the non-SSR branch when it accesses browser APIs during module evaluation:
import { defineRuntimePlugin } from 'kawapress'
export default defineRuntimePlugin({
name: 'browser-only-plugin',
setup(api) {
api.vueApp(async (app) => {
if (!import.meta.env.SSR) {
const { default: browserPlugin } = await import('browser-plugin')
app.use(browserPlugin)
}
})
},
})Use this pattern only for browser behavior that does not change the initial HTML. If a plugin changes the component tree, use the client-only component boundary from the previous section to keep server and browser output aligned.
Load the Same Dependency on Both Sides #
The server and the browser must run the same component code. A common trap is export conditions configured for the client only: since Vite 6, the top-level resolve.conditions applies to the browser build, while the server build reads ssr.resolve.conditions. With the config below the browser bundles a component library's src/ while the server still bundles its prebuilt dist/, and any difference between the two causes hydration mismatches:
import { defineConfig } from 'kawapress'
export default defineConfig({
vite: {
resolve: { conditions: ['source', 'module', 'browser', 'development|production'] },
},
})Give the server the same condition:
export default defineConfig({
vite: {
resolve: { conditions: ['source', 'module', 'browser', 'development|production'] },
ssr: { resolve: { conditions: ['source'] } },
},
})kawapress build checks the dependencies both sides bundle. When one import loads different files on the two sides, the build warns, lists the file each side loaded, and names the conditions to add where the export conditions explain the difference. A browser build that a package ships on purpose, through the browser export condition or the browser field, does not trigger the warning.
Keep the First Render Stable #
Hydration requires the server HTML and the browser's first render to produce the same node structure and text. Avoid using these values directly during the first render:
Date.now()or other request-dependent timestamps;Math.random();- browser viewport dimensions;
- user settings that exist only in
localStorage.
Start with a stable value, then read browser state from onMounted(). Data needed by both environments should come from the same pageData, site configuration, or route information.
KawaPress also performs real SSR in development. Compatibility errors usually appear while developing instead of waiting until a production build.
Debug Hydration Mismatches #
The dev server prints every mismatching node and what each side rendered in the browser console. A production build drops those details to stay small and prints a single line:
Hydration completed but contains mismatches.For a mismatch that only shows up in a production build, build once with --debug-hydration and preview it. The flag turns on Vue's __VUE_PROD_HYDRATION_MISMATCH_DETAILS__ for that build, and the console lists the mismatching nodes again:
kawapress build --debug-hydration
kawapress previewThe flag makes the client code larger. Once you have found the problem, rebuild without it before deploying.
Mismatches that only appear in a production build and cluster around one dependency's components usually mean the two sides loaded different builds of that dependency, for example because resolve.conditions was configured for the client only; see Load the Same Dependency on Both Sides.


Comments