question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nuxt 3 fails to run fetch or render any components on client side – only works on full page refresh on server

See original GitHub issue

Environment

  • Operating System: Darwin
  • Node Version: v14.19.0
  • Nuxt Version: 3.0.0-rc.13
  • Nitro Version: 0.6.1
  • Package Manager: npm@6.14.16
  • Builder: vite
  • User Config: modules, sanity, privateRuntimeConfig, publicRuntimeConfig, target, image, css, vite, alias
  • Runtime Modules: @nuxtjs/sanity@1.4.1, nuxt-jsonld@2.0.6, @pinia/nuxt@0.4.3, @nuxt/image-edge@1.0.0-27769790.4b27db3
  • Build Modules: -

Reproduction

As the fetch requires a token to Sanity API, which I am unable to make public, I’m not sure how I can provide a reproduction of this? Happy to give access privately in some way? Please advise

Describe the bug

content renders fine and fetch queries all work on server side, on full page refresh. But on client side page changes, nothing loads and there is no errors in the console or terminal.

Here is a screen recording.

https://user-images.githubusercontent.com/36191462/201318983-2c372f2e-8b09-434c-9264-8afa7e2dd753.mp4

Possibly related to this bug which multiple users have reported, and which does have a reproduction available.

Additional context

Here is an example of one of my queries:

<template>
  <div>
    <loading v-if="pending"/>
    <template v-else-if="entry">
     <hero-builder :entry="entry"/>
     <builder-landing-page :entry="entry"/>
    </template>
  </div>
</template>


<script setup>
  import {entryMeta} from '~/helpers';
  import groqQuery from '~/queries/groq/home-page';
  const config = useRuntimeConfig();
  const route = useRoute();
  const query = groqQuery();

  const { pending, data:entry, error } = useSanityQuery(query);
  console.log({pending: pending.value, error:error.value, entry:entry.value});
  if (error && error.value) {
    throw createError({statusCode:500})
  } else if (entry) {
    const meta = entryMeta(entry.value);
    useHead({
      title:entry.value?.meta?.title,
      meta: meta,
      link: [
        {rel:'canonical',href:'/'}
      ]
    })
   } else {
      console.error('Unknown error occured')
    }
</script>
  

and here is my nuxt.config:


export default defineNuxtConfig({
	modules: ["@nuxtjs/sanity",'nuxt-jsonld','@pinia/nuxt','@nuxt/image-edge'],
	sanity: {
		projectId: "say5yn59",
		apiVersion: '2022-10-14',
		useCdn:false,
		dataset:process.env.SANITY_DATASET
	},
	privateRuntimeConfig: {
		sanity: {
			token: process.env.SANITY_TOKEN,
		},
	},
	publicRuntimeConfig:{
		siteName:'Cool Planet',
		baseUrl:process.env.BASE_URL
	},
	target: 'static',
	image: {
  		// Options
  		sanity: {
			projectId: 'say5yn59',
			dataset: 'production'
		},
  		screens:{
  			xxs:320,
			xs: 359,
			sm: 640,
			md:768,
			lg: 1024,
			xl: 1280,
			xxl:1536,
			xxxl:1920,
			quadHd:2400,
			'4k':3700,
  		}
	},
	css:[
		"@/assets/scss/main.scss"
	],
	vite: {
        css: {
            preprocessorOptions: {
                scss: {
                    additionalData: '@use "@/assets/scss/tools.scss" as *;'
                },
            },
        },
    },
    alias: {
        "@app": "/@app"
    }
})

Logs

No logs are available.

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
danielroecommented, Nov 11, 2022

Excellent - glad that is resolved 👍

CORS is only relevant for browsers. Node/server ignores CORS headers entirely.

0reactions
toddpadwickcommented, Nov 11, 2022

Hi @danielroe , thank you for your help. I realised what I have done wrong. I had added the origins to Sanity CORS, however, what I hadn’t realised was that I had added **https://**localhost:0000 rather than **HTTP://**localhost:0000. Silly mistake so this can be closed. although, what is confusing, is why it was working fine on server side? with the wrong cors it should have failed entirely I would have expected.

Read more comments on GitHub >

github_iconTop Results From Across the Web

nuxt.js - Client Side Component not working on Server after ...
I solve it on the server by using Static site generation (aka SSG). For that, you need to change the target from 'server'...
Read more >
Data Fetching
This allows your page to render with all of its required data present. ... fetch is a hook called during server-side rendering after...
Read more >
Common Problems With The Nuxt Client-Only Component
Yes, it skips rendering your component on the server side, but it still gets executed! Here is a simple example. Let's say the...
Read more >
Nuxt Local Server Gets Stuck · Issue #6442
If for some reason Nuxt. js is unable to render your page at all (ie renderRoute returns a falsy value), then the Nuxt...
Read more >
Creating Server-side Rendered Vue.js Apps Using Nuxt.js
In a new page called /products/view-async , let's change the created method to fetch ; that should work, right? export default { fetch...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found