Vize

vize:croquis/cf/browser-api-ssr

サーバーでも描画され得る場所で、ブラウザ専用 API を使っています。

既定の重大度 自動修正 オプション
warning なし。関連ファイルを確認して修正してください コード固有のオプションはありません。対応済みの CLI の診断は、重大度を変更できます

適用範囲: 解析対象のコンポーネント構成と、下記に示す対応済みの情報

共通のプロジェクト ファイル

以下のファイルは、悪い例・良い例の両方で変更せずに使います。プロジェクトには Vue をインストールし、例で import している場合は vue-router や Pinia もインストールしてください。バージョン固有の対応条件がある場合は、その注意に従ってください。エントリーファイルによって、コンポーネント間の関係を明示しています。

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>

悪い例

setup 中に window.innerWidth を読み、ブラウザの window がない SSR 環境でも実行されます。

App.vue

<script setup lang="ts">
const width = window.innerWidth;
</script>
<template><p>Content</p></template>

良い例

サーバーでも安全に使える初期値で ref を作り、クライアントでのマウント後に実行される onMounted 内で window を読みます。

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>

設定(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

関連ページ

公開の説明

生成元

ファイル間ルール一覧