-
Notifications
You must be signed in to change notification settings - Fork 50
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
fix: scope locale context to current query and not reuse globally #211
Conversation
7e7b64b
to
4315471
Compare
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. |
if (context.sourceDatocms.localeState) { | ||
context.sourceDatocms.localeState.clear(info); | ||
} | ||
if (context.sourceDatocms.fallbackLocales) { | ||
context.sourceDatocms.fallbackLocales.clear(info); | ||
} |
There was a problem hiding this comment.
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
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) || [], |
There was a problem hiding this comment.
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
4315471
to
2462344
Compare
#209 describe how sometimes nested fields don't honor
locale
settings. This happens because thecontext.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
andlocaleFallback
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 changesPossibly fixes #209