#title Weekly Vue News #89 - Render Nuxt Component Only on Client-Side #preview Ready for your weekly Vue & Nuxt dose?
Render Nuxt Component Only on Client-Side
Hi π
I'm still busy creating my remote talk for Vue.js Live 2023, I have to upload the video this Friday π΅βπ«
I hope you enjoy this week's issue π
Have a lovely week βοΈ
To support me:
- π Recommend the newsletter to your friends: it really helps!
- πΈ Sponsor the newsletter
- π§΅ Retweet the latest Twitter thread
- π¨ Reply to this email: feedback is welcome
By default, Nuxt uses
It's a typical use case to access browser APIs like localStorage
in your Nuxt components:
1 <script setup>
2 localStorage.setItem('test', 'A test string')
3
4 const localStorageItem = ref('')
5
6 onMounted(() => {
7 localStorageItem.value = localStorage.getItem('test')
8 })
9 </script>
10
11 <template>
12 <div style="display: flex; flex-direction: column; gap: 10px">
13 <span>Hello from client</span>
14 <span>Local storage item: {{ localStorageItem }}</span>
15 </div>
16 </template>
If you try to run your app, Nuxt will throw an error:
localStorage is not defined
This error occurs because the code is first executed on the Node.js server which does not support localStorage
.
You can solve this problem by using Nuxt 3's
1 <template>
2 <div>
3 <ClientOnly fallback-tag="span" fallback="Loading on server...">
4 <MyComponent />
5 </ClientOnly>
6 </div>
7 </template>
The <ClientOnly>
component renders its slot only in client-side. To import a component only on the client, register the component in a client-side only plugin.
Try it yourself in the following StackBlitz project:
ππ» In the article you will come to understand what component noise is and how to reduce it with the Composition API in Vue.
logaretm.comππ» The migration from Nuxt 2 to Nuxt 3 is not easy. |
ππ» A course designed "to provide a one stop shop for all of the |
helpful snippets, techniques, etc. that we came across in the process" of the Nuxt 3 migration |
ππ» The NuxtLink component in Nuxt 3 may seem simple at first glance, but thereβs a lot going on beneath the surface. |
ππ» In this article find out how it works and how to use it. |
ππ» A guide on how to waste resources and make projects harder to complete.
taylor.townππ» Make it scannable |
ππ» Speak plainly |
ππ» Avoid adverbs |
ππ» Simplify your sentences |
ππ» Avoid a passive voice |
ππ» The new Outlook switches rendering engines from Microsoft Word to Edge
fullystacked.netππ» It lets users trigger functions, evaluate cron expressions, and obtain a list of upcoming run times without any dependencies.
github.comππ» Make TypeScript errors prettier and human-readable in VSCode.
marketplace.visualstudio.comππ» It lets you modify a JS/TS file and write back magically, just like JSON |
ππ» It could be helpful for tools to update your configs written in JS. |
ππ» A 100% free open-source UI Kit forΒ TailwindΒ CSS |
ππ» Supports importing only the key plugins & components they need, without bloating the JS code |
Until next week,
Holzapfelkreuther Str. 19, 81375 Munich, Germany