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

When setting/changing variables in plugins, the (cache) key is not updated #199

Open
FreekVR opened this issue Jun 20, 2023 · 1 comment
Labels
✨ enhancement New feature or request

Comments

@FreekVR
Copy link

FreekVR commented Jun 20, 2023

We're building a multilingual site, and every query is getting a default variable with the current language site like so;

    /**
     * Sets the default variables for all requests.
     */
    const defaultVariables = definePlugin(({ operation }) => {
        // Only set the site if it hasn't been set yet.
        if (operation.variables && !('site' in operation.variables)) {
            operation.variables.site = nuxt?.$i18n?.localeProperties?.value?.craftSiteHandle;
        }

        // // Only set the uri if it hasn't been set yet.
        if (operation.variables && !('uri' in operation.variables)) {
            operation.variables.uri = plainUri;
        }
    });

This is causing a problem however with the cache plugin from the defaultPlugins list, as the cache key is getting set before the plugins run (?) at any rate, when the language changes, it uses cached data for a different set of query variables now.

I re-generate the key after setting the variables in the plugin above now, which feels like a bit of a workaround

operation.key = getQueryKey(operation);

This successfully fixed the caching issue for me.

I would suggest the key would get (re)calculated in the dedup/cache plugins when they run?

@logaretm
Copy link
Owner

logaretm commented Aug 20, 2023

Interesting, I didn't expect people to modify stuff other than the headers on the fly but that's a good way to do localization actually.

I will think of something, I think the cache plugin could recalculate the key itself.

@logaretm logaretm added the ✨ enhancement New feature or request label Aug 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants