useGqlHeaders and useGqlHost immutable after first call on SSR
Environment
- Operating System: Linux
- Node Version: v18.17.1
- Nuxt Version: 3.7.4
- CLI Version: 3.9.0
- Nitro Version: 2.6.3
- Package Manager: [email protected]
- Builder: -
- User Config: devtools, ssr, buildModules, modules, i18n, build, nitro, loading, app, content, server, runtimeConfig, delayHydration, image, css, tailwindcss, postcss, vite, apollo
- Runtime Modules: [email protected], @nuxtjs/[email protected], @nuxtjs/[email protected], [email protected], @nuxtjs/[email protected], @nuxt/[email protected], [email protected], @pinia/[email protected], @pinia-plugin-persistedstate/[email protected]
- Build Modules: nuxt-graphql-request
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
Hello, man.
I've found a workaround for now. Try setting your headers like this:
useGqlHeaders({headers, client: YOUR_CLIENT, respectDefaults: false})