@duannx/vue-client-only

中文文档
1.0.3发布于 2022/06/21MITTypeScriptESM + CJS零依赖近 30 天下载 3559

npm license

原始使用文档
文档目录10 节
  1. What is vue-client-only?
  2. Installation
  3. Usage
  4. Placeholder
  5. Props
  6. When to use this component?
  7. DEMO
  8. Contributing
  9. Versioning
  10. License

vue-client-only

npm license

What is vue-client-only?

This component helps you to render a component on the client side only. Only support Vue3

Installation

npm install --save @duannx/vue-client-only
or
yarn add @duannx/vue-client-only

Usage

import ClientOnly from '@duannx/vue-client-only'

<client-only>
    <some-component></some-component>
    This will be render only in the client
    <template #placeholder>
      This is the placeholder that will be render on SSR
    </template>
</client-only>

Placeholder

Use the placeholder slot to render something on SSR

<client-only>
    This will be render only in the client
    <template #placeholder>
      This is the placeholder that will be render on SSR
    </template>
</client-only>

Props

  • is-show: Boolean. Use this prop to control exactly when the component is rendered on the client. If you don't set this prop, the component will be rendered on the client only when the component is mounted.
<!-- The default slot will be shown only when isShow is set to true -->
<client-only :is-show="isShow">
    This will be render only in the client
    <template #placeholder>
      This is the placeholder that will be render on SSR
    </template>
</client-only>

When to use this component?

  • When a component can not run on the server side. Not really. You can just move the logic that can only run on the browser to the mounted hook. No need to use this component.
  • When you face the error: Hydration mismatch. NO. Please. This error is caused by the state of the component in the client is not matching with the state in the server or some incorrect HTML nesting tags. You need to find the root cause and fix it. You need to find the root cause and fix it. Overuse of this component will eliminate the benefit of SSR.
  • When you want to reduce the server workload. This component will defer the rendering of the component on the server side. So your server will run faster, have better server response time, and reduce document size that transfer to the client.
  • When you want want to defer the rendering of the component on the client side. The is-show prop will help you. Almost the time, the under-the-fold content is not useful when the page loads. So you can defer the rendering of all under-the-fold content till the page is loaded or the user interacts with the page. Example, this code delay the rendering of the content till the user click or scroll the page:
<script setup>
  import ClientOnly from '@duannx/vue-client-only'
  import { onMounted } from '@vue/runtime-core'
  const isShow = ref(false)
  onMounted(() => {
    ['keydown', 'click', 'touchstart', 'scroll'].forEach((type) => {
      addEventListener(type, () => {
        isShow.value = true
      }, { once: true, capture: true });
    })
  })
</script>
<template>
Some above the fold content ...
<ClientOnly :is-show="isShow">
  <p>This content is loaded after the user input</p>
  <template #placeholder>
    <p>Content placeholder. Click anywhere to show the main content...</p>
  </template>
</ClientOnly>
</template>

DEMO

This DEMO only has the client side. It shows how the component looks like in the client. Demo code

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

vue-client-only use SemVer for versioning.

License

This project is licensed under the MIT License - see the LICENSE file for details.

版本与发布记录

@duannx/vue-client-only 共发布 4 个版本,首次发布于 2022/06/20;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.3latest2022/06/214 年前
1.0.22022/06/204 年前
1.0.12022/06/204 年前
1.0.02022/06/204 年前

关于 @duannx/vue-client-only 的常见问题

如何安装 @duannx/vue-client-only?

在项目目录运行 npm install @duannx/vue-client-only,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:vue(^3.x)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@duannx/vue-client-only 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@duannx/vue-client-only 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@duannx/vue-client-only 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=8.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@duannx/vue-client-only 可以商用吗?

@duannx/vue-client-only 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@duannx/vue-client-only 的下载量有多少?

根据 npm 官方下载统计,@duannx/vue-client-only 在 2026/10/13 – 2026/11/11 这 30 天内被下载 3,559 次,日均约 119 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@duannx/vue-client-only 还在维护吗?

最新版本 1.0.3 发布于 2022/06/21(4 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/06/20。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。