NuxtN
Nuxtβ€’15mo ago
Pisandelli

Best way to run code once on nuxt ready

Hello guys! I'm wondering how to achieve a thing. Hope you can help me.
I'd like to generate a CSS variable with the user's font size configured on the browser.
I'll use it to calculate some clamp() functions based on Root font size. I can't assume it's 16px by default, so I need to run a script like this:
const HTMLDocument = document.querySelector('html');
if (!HTMLDocument) return;
return +getComputedStyle(HTMLDocument).fontSize.slice(0, -2);


My question is: "What is the best approach to run this just once when page's loading or as soon as I have access to this browser information? Should I use a Plugin? Should I use a Hook in nuxt.config? Should I use a middleware?" It's important to run this just once... I don't need to get this info every time I change a route.

Thanks in advance for any tip!
Was this page helpful?