Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useGqlHeaders and useGqlHost immutable after first call on SSR #402

Open
unluckynelson opened this issue Oct 11, 2023 · 1 comment
Open
Labels
bug Something isn't working

Comments

@unluckynelson
Copy link

unluckynelson commented Oct 11, 2023

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 unluckynelson added the bug Something isn't working label Oct 11, 2023
@AngeloSchulerPiletti
Copy link

Hello, man.

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants