javascript added using useHead is being executed on the server
I have a javascript to be executed on the client to check the browser version and also if some functionalities are available and if not match the criteria we redirect the user to a dedicated page (informing the user that he needs to update the browser)
But I see that the code added to the script tag is being executed on the server and being changed
original:
then turns into:
How can I prevent/fix this?
Thanks
16 Replies
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
thanks @Igor Jacaúna
I'll gonna take a look and see if it feets on my needs
even so, I'm trying to understand why the js code inside the script tag that is injected into the markup is being executed on the server and also changed
It's something that I wasn't expecting to happen as it should "just" inject it into the markup
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
I am not sure if it has to do with the way I'm declaring the js function
I'm creating it as a "string"
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
the code is inside app.vue
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
it's hard as it's the config is very complex
The addBodyClass function is called inside the if (typeof window !== 'undefined') { base on other conditions
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
StackBlitz
Nuxt - Starter (forked) - StackBlitz
Create a new Nuxt project, module, layer or start from a theme with our collection of starters.
No problem
now I'm confused
In my example, I have the same way as I have on the project and also doesn't change the code :/
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
nope. I forgot to set the SSR
Now I'm also getting the js code changed
looks like someone already reported this behavior
https://github.com/nuxt/nuxt/issues/22559
GitHub
Unexpected substitutions in embedded scripts in useHead · Issue #22...
Environment Operating System: Linux Node Version: v16.20.0 Nuxt Version: 3.6.5 Nitro Version: 2.5.2 Package Manager: [email protected] Builder: vite User Config: - Runtime Modules: - Reproduction https://s...
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
maybe
The thing is that it wasn't me who implemented
We had it more or less as you described on our Vue 3 SSR project and now that we are migrating to Nuxt, who was working on this, initially, it has made it the same way as you, but then the dev that implemented the initial solution said that going with that approach, it could lead to some problems for slower connection and then it was refactored to this approach.
My proposal was to forget the check for browser functionalities and only use the browser version and do it directly on the server middleware, cause that way, instead of returning the page that was initially requested it would immediately return the outdated page...but the architects said it was ok the way it was and didn't want to change :/
this was my proposal
even so, with this approach, it would still be possible to cover the browser functionalities, by running that logic directly on the onMounted hook