Skip to content

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

Open
@FreekVR

Description

@FreekVR

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions