nuxt-graphql-client icon indicating copy to clipboard operation
nuxt-graphql-client copied to clipboard

useGqlHeaders and useGqlHost immutable after first call on SSR

Open unluckynelson opened this issue 2 years ago • 1 comments

Environment

Describe the bug

I need to set some headers based on the user request i first received on the server side.

Common use case to attach the requesting IP address to x-forwarded-for header to enable correct IP locale detection server side. I also attempted using useGqlHost to add query parameters but they also seem immutable after the first change.

The code snippet I use to update the headers on each request:

plugins/gql-addheaders.ts

export default defineNuxtPlugin({
    async setup(nuxtApp) {
        const router = useRouter();
        const headers = {
            'x-no-cache': router.currentRoute.value.query?.test ? 'true' : 'false',
            'x-forwarded-for': '',
        };
        if (nuxtApp.ssrContext) {
            const ipaddress = nuxtApp.ssrContext?.event.node.req.headers['x-forwarded-for'] as string || '';
            headers['x-forwarded-for'] = ipaddress.split(',')[0];
        }
        console.log(headers);
        // Headers are only set on first request
        useGqlHeaders(headers);
        // Params are only set on first request
        useGqlHost(`?x-no-cache=${headers['x-no-cache']}&x-forwarded-for=${headers['x-forwarded-for']}`);
    },
});

Expected behaviour

The headers should be updated on each request. The server side logs show that the headers remain the same even after subsequent requests to useGqlHeaders

Reproduction

No response

Additional context

No response

Logs

No response

unluckynelson avatar Oct 11 '23 07:10 unluckynelson

Hello, man.

I've found a workaround for now. Try setting your headers like this:

useGqlHeaders({headers, client: YOUR_CLIENT, respectDefaults: false})

AngeloSchulerPiletti avatar Dec 01 '23 11:12 AngeloSchulerPiletti