Vue No SSR
Vue No SSR was a component designed for Vue.js applications utilizing Server-Side Rendering (SSR) to wrap components that should only be rendered on the client side. Its primary function was to prevent hydration mismatches and allow browser-specific code or libraries to function correctly without causing errors during the SSR build process. The package's last version under this name was 1.1.1, released in early 2019. It was succeeded by `vue-client-only` in its 2.0.0 release, which is actively maintained. This component provided a simple API, allowing for fallback content (placeholders) to be displayed during SSR until the client-side component was mounted. It solved a common problem in isomorphic Vue applications where certain elements or third-party libraries are not compatible with Node.js environments.
Common errors
-
Failed to mount component: template or render function not defined.
cause The `<no-ssr>` component was imported but not registered in the Vue component's `components` option.fixEnsure `NoSSR` is correctly imported and registered: `import NoSSR from 'vue-no-ssr'; export default { components: { 'no-ssr': NoSSR } }`. -
[Vue warn]: Unknown custom element: <no-ssr> - did you register the component correctly?
cause The Vue instance cannot find the `<no-ssr>` component, often due to incorrect registration or a typo in the tag name.fixVerify that `NoSSR` is correctly imported and registered in the `components` option, and that the tag used in the template matches the registered name (e.g., `'no-ssr'`).
Warnings
- breaking The `vue-no-ssr` package and its component `<no-ssr>` were officially renamed to `vue-client-only` and `<client-only>` respectively in `vue-client-only@2.0.0`.
- breaking The default CSS class name for the placeholder element changed from `no-ssr-placeholder` to `client-only-placeholder` in `vue-client-only@2.0.0`.
- gotcha This package (`vue-no-ssr`) does not provide official TypeScript types. While it can be used in TypeScript projects, you may need to declare types manually or rely on `any`.
Install
-
npm install vue-no-ssr -
yarn add vue-no-ssr -
pnpm add vue-no-ssr
Imports
- NoSSR
import { NoSSR } from 'vue-no-ssr'import NoSSR from 'vue-no-ssr'
- NoSSR Component Registration
<script> import { NoSSR } from 'vue-no-ssr'; export default { components: { NoSSR } } </script><script> import NoSSR from 'vue-no-ssr'; export default { components: { 'no-ssr': NoSSR } } </script> - Usage in template
<template> <no-ssr> <MyClientOnlyComponent /> </no-ssr> </template>
Quickstart
<template>
<div id="app">
<h1>My SSR-enabled Website</h1>
<no-ssr placeholder="Loading client-only content...">
<!-- This component might use browser-specific APIs or be incompatible with Node.js -->
<MyClientOnlyComponent />
<template v-slot:placeholder>
<p>This content is client-only: Loading...</p>
</template>
</no-ssr>
<p>This content is always visible (SSR and client).</p>
</div>
</template>
<script>
import Vue from 'vue';
import NoSSR from 'vue-no-ssr';
// A dummy component that would typically rely on client-side APIs
const MyClientOnlyComponent = Vue.component('MyClientOnlyComponent', {
template: '<div>Hello from the client! (Window width: {{ windowWidth }})</div>',
data() {
return { windowWidth: 0 };
},
mounted() {
if (typeof window !== 'undefined') {
this.windowWidth = window.innerWidth;
window.addEventListener('resize', this.updateWidth);
}
},
beforeDestroy() {
if (typeof window !== 'undefined') {
window.removeEventListener('resize', this.updateWidth);
}
},
methods: {
updateWidth() {
this.windowWidth = window.innerWidth;
}
}
});
export default {
name: 'App',
components: {
'no-ssr': NoSSR,
MyClientOnlyComponent
}
};
</script>
<style>
body { font-family: sans-serif; }
</style>