vite-plugin-vue-server-ref is a Vite plugin designed to facilitate the sharing of reactive state between multiple Vue clients and the Vite development server. It enables real-time synchronization of data across browser tabs or different client instances, utilizing Vue's reactivity system. The current stable version is v1.0.0, which notably transitioned to an ESM-only architecture, dropping CommonJS support. The package sees active development with regular updates addressing bug fixes and introducing minor features. Key differentiators include its tight integration with Vite's dev server, the use of virtual module imports (e.g., `server-ref:key`, `server-reactive:key`) for seamless state access, and features like granular synchronization control and incremental updates for reactive objects via diffing.
npm install vite-plugin-vue-server-refVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to configure `vite-plugin-vue-server-ref` in `vite.config.ts` with initial state and then use `server-ref` and `server-reactive` virtual imports within a Vue component to access and synchronize state across multiple clients and the Vite development server, including type-safety with `import type`.
Ensure your project is configured for ESM, and replace `require()` statements with `import` statements. If using `vite.config.js`, rename it to `vite.config.mjs` or ensure your `package.json` has `"type": "module"`.
Similar to v1.0.0, ensure your project's module resolution properly handles ESM packages. For Node.js environments, use `import` syntax or configure transpilation if necessary.
Always use `import type { ServerRef, ServerReactive } from 'vite-plugin-vue-server-ref/client'` and apply type assertions (e.g., `const foo = _foo as ServerRef<string>`) to ensure proper type inference and safety.For reactive objects where partial updates are desired, change your import from `import object from 'server-reactive:object'` to `import object from 'server-reactive:object?diff'`.
Understand that `foo.$syncUp = false` means changes made locally *will not* be sent to the server, making it download-only. `foo.$syncDown = false` means changes from the server *will not* be received, making it upload-only (if `$syncUp` is true). Set these flags carefully according to your desired sync behavior.
Change `const ServerRef = require('vite-plugin-vue-server-ref')` to `import ServerRef from 'vite-plugin-vue-server-ref'` and ensure your Node.js or Vite configuration supports ESM.Import the correct types and apply a type assertion: `import type { ServerRef } from 'vite-plugin-vue-server-ref/client'; const foo = _foo as ServerRef<string>;`Verify that `vite-plugin-vue-server-ref` is added to the `plugins` array in your `vite.config.ts`. Ensure your `vite.config.ts` is correctly named and located, and that Vite is running in development mode.
Ensure the `state` object in your `vite.config.ts` has the key you are trying to access (e.g., `object.count`). If it's an asynchronous component, ensure proper loading states or fallbacks while the ref is being populated.