N
Nuxt2w ago
Stf

how to fix network error in homepage after we changed the code and make the build

how to fix network error in homepage after we changed the code and make the build with npm start build , later that dist folder extracted into the hosting and the whole website now dont works, it gives that error Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://127.0.0.1:8000/api/v1/common. (Reason: CORS request did not succeed). Status code: (null).
2 Replies
kapa.ai
kapa.ai2w ago
Hi @Stf. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the information provided in your question, it appears you're encountering a CORS (Cross-Origin Resource Sharing) error after deploying your Nuxt application. This issue is likely related to how your API requests are being handled in the production environment. Let's address this problem step by step: 1. CORS Configuration: The error message suggests that your frontend is trying to make a request to <http://127.0.0.1:8000/api/v1/common,> which is likely your local development API URL. In production, this URL should be pointing to your actual API server, not localhost. 2. Environment Variables: Make sure you're using the correct API URL for your production environment. In Nuxt 3, you can use runtime config to manage environment-specific variables. Here's how you can set it up: Update your nuxt.config.ts file:
export default defineNuxtConfig({
runtimeConfig: {
public: {
apiBase: process.env.NUXT_PUBLIC_API_BASE
},
},
})

export default defineNuxtConfig({
runtimeConfig: {
public: {
apiBase: process.env.NUXT_PUBLIC_API_BASE
},
},
})

Then, in your .env file for production, set the correct API URL:
NUXT_PUBLIC_API_BASE=<https://your-production-api-url.com>

NUXT_PUBLIC_API_BASE=<https://your-production-api-url.com>

This approach is documented in the Nuxt 3 Runtime Config guide (https://nuxt.com/docs/guide/going-further/runtime-config#environment-variables). 3. API Requests: In your components or pages, use the runtime config to make API requests:
const config = useRuntimeConfig()
const { data } = await useFetch(`${config.public.apiBase}/api/v1/common`)

const config = useRuntimeConfig()
const { data } = await useFetch(`${config.public.apiBase}/api/v1/common`)

4. Server-side CORS: Ensure that your API server is configured to accept requests from your frontend's domain. You'll need to set the appropriate CORS headers on your backend. 5. Proxy Configuration: If you're unable to modify the API server's CORS settings, you can use Nuxt's server middleware to proxy requests. Add this to your nuxt.config.ts:
import { defineNuxtConfig } from 'nuxt'

export default defineNuxtConfig({
nitro: {
devProxy: {
'/api': {
target: '<https://your-production-api-url.com',>
changeOrigin: true,
}
}
}
})

import { defineNuxtConfig } from 'nuxt'

export default defineNuxtConfig({
nitro: {
devProxy: {
'/api': {
target: '<https://your-production-api-url.com',>
changeOrigin: true,
}
}
}
})

This configuration is based on the Nuxt DevProxy documentation (https://nuxt.com/docs/api/nuxt-config#experimental-crossoriginprefetch). 6. Chunk Loading Errors: If you're experiencing issues with JS chunks not loading correctly after deployment, Nuxt provides built-in support for handling chunk loading errors. You can configure this behavior in your nuxt.config.ts:
export default defineNuxtConfig({
experimental: {
emitRouteChunkError: 'automatic' // or 'manual' if you want to handle it yourself
}
})

export default defineNuxtConfig({
experimental: {
emitRouteChunkError: 'automatic' // or 'manual' if you want to handle it yourself
}
})

This feature is explained in the Nuxt Error Handling documentation (https://nuxt.com/docs/getting-started/error-handling#errors-with-js-chunks). By implementing these steps, you should be able to resolve the CORS issue and ensure your Nuxt application works correctly in the production environment. Remember to rebuild your application after making these changes and redeploy the updated dist folder to your hosting service. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2w ago
<@963754097360662558> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Want results from more Discord servers?
Add your server