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

fix: scope locale context to current query and not reuse globally #211

Merged
merged 2 commits into from
Jan 11, 2023

Conversation

pieh
Copy link
Contributor

@pieh pieh commented Jan 9, 2023

#209 describe how sometimes nested fields don't honor locale settings. This happens because the context.sourceDatocms object is actually reused between all the queries that run in given process and state there is scope to just field paths within the query. Gatsby can execute multiple queries in parallel (especially for the builds as it does batching of all queries), so this shared state object is a problem as one query for one page might change the context that query for other page rely on.

Proposed fix provide fresh locale and localeFallback for each query seperately - I implemented this in shortest way possible, there is opportunity for some optimisation as right now there is a lot of repeated .get lookups which I kept for visibilty and easier to follow proposed changes

Possibly fixes #209

@matjack1
Copy link
Member

matjack1 commented Jan 9, 2023

Hey @pieh thank you very much for this! We are going to check it and get back to you later during the week as we cannot sooner.

Comment on lines -38 to -43
if (context.sourceDatocms.localeState) {
context.sourceDatocms.localeState.clear(info);
}
if (context.sourceDatocms.fallbackLocales) {
context.sourceDatocms.fallbackLocales.clear(info);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a code smell that something is not right - if something has to clear data for current info.path which should be first time we visit it - the setup seemed problematic overall.

Now with fresh "locale context" it should be just not possible for those things to have to be cleared as we should enter this code path for given info.path for the first time every time

Comment on lines 10 to +32
if (args.locale) {
context.sourceDatocms.localeState.set(info, args.locale);
context.sourceDatocms
.getQueryContext(context)
.localeState.set(info, args.locale);
}

if (args.fallbackLocales) {
context.sourceDatocms.fallbackLocalesState.set(info, args.fallbackLocales);
context.sourceDatocms
.getQueryContext(context)
.fallbackLocalesState.set(info, args.fallbackLocales);
}

const locale = context.sourceDatocms.localeState.get(info) || mainLocale;
const locale =
context.sourceDatocms.getQueryContext(context).localeState.get(info) ||
mainLocale;

return {
locale,
fallbacks: {
[locale]: context.sourceDatocms.fallbackLocalesState.get(info) || [],
[locale]:
context.sourceDatocms
.getQueryContext(context)
.fallbackLocalesState.get(info) || [],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has 4 repeated context.sourceDatocms.getQueryContext(context) calls, which could be deduped

@pieh pieh force-pushed the parallel-queries-overwrites branch from 4315471 to 2462344 Compare January 9, 2023 11:43
@stefanoverna stefanoverna merged commit 2f73b0f into datocms:master Jan 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Locale at query level returns wrong fields
3 participants