nuxt / nuxt

The Intuitive Vue Framework.
https://nuxt.com
MIT License
54.8k stars 5.02k forks source link

Network requests duplicating (Repository pattern) #29575

Closed UlugbekNG closed 2 weeks ago

UlugbekNG commented 2 weeks ago

Environment

Reproduction

https://stackblitz.com/edit/nuxt-starter-ka88sa?file=README.md

Describe the bug

https://medium.com/@luizzappa/nuxt-3-repository-pattern-organising-and-managing-your-calls-to-apis-with-typescript-acd563a4e046

using method from this Article and I have duplicated requests in network. You can check everything in reproduction link how i wrote this. But duplicating only when using baseUrl in fetch options. As you can see I'm using @nuxt-alt/proxy (settings you can check in Reproduction link). Wrapping call to useAsyncData not working.

If I add baseUrl in api.ts composable, there is no duplicates

Some Screenshots Image-1 image-2

Additional context

I will be very happy if someone can help me

Logs

No response

stackblitz[bot] commented 2 weeks ago

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

UlugbekNG commented 2 weeks ago

Fixed with using server proxy

import { joinURL } from "ufo";

export default defineEventHandler(async (event): Promise<string> => {
    const { devProxy, prodProxy } = useRuntimeConfig();
    const proxyUrl: string = devProxy;
    console.log('proxyUrl', proxyUrl);

    const path: string = event.path.replace(/^\/api\//, '');
    const url: string = joinURL(proxyUrl, path);

    return proxyRequest(event, url);
})