vize:croquis/cf/browser-api-ssr
A browser-only API is used where the component can render on the server.
| Default severity | Automatic fix | Options |
|---|---|---|
| warning | None; review related files and apply the repair | No per-code options; supported CLI findings accept severity overrides |
Applies to: Analyzed component graph and the supported facts described below
Shared project files
Use these unchanged files in both Bad and Good. Install the imported packages in the project: Vue, plus vue-router or Pinia where shown. Follow any version-specific support note. The entry root makes the component relationship explicit.
main.ts
import { createApp } from "vue";
import Root from "./App.vue";
createApp(Root).mount("#app");
index.html
<div id="app"></div>
<script type="module" src="/main.ts"></script>
Bad
window.innerWidth runs during setup, where an SSR environment has no browser window.
App.vue
<script setup lang="ts">
const width = window.innerWidth;
</script>
<template><p>Content</p></template>
Good
Initialize a ref to a server-safe value and read window inside onMounted, which runs after client mounting.
App.vue
<script setup lang="ts">
import { onMounted, ref } from "vue";
const width = ref(0);
onMounted(() => { width.value = window.innerWidth; });
</script>
<template><p>Content</p></template>
Configuration (Vite+)
import { defineConfig } from "@vizejs/vite-plugin/vite-plus";
export default defineConfig({}).withVue({
linter: { preset: "incremental", crossFile: true,
rules: { "croquis/cf/browser-api-ssr": "warn" },
},
});
vp run lint