You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Locale on req overwrites locale set on findById #4409
I've implemented an afterRead hook that fetches all urls of the same document with different locales. However if I pass the req and locale in the same request. It chooses the locale on req over the one provided on locale. I expect this to be the other way around.
My afterRead hook:
// findById requestconstdoc=awaitreq.payload.findByID({collection: req.collection?.config?.slugasany,id: data.id,
locale,// `en`
req,// req.locale is set on `nl`})// Expect this to be in `en`
To Reproduce
Create a findById call, and set it on a different locale that req.locale is
You'll see that the document is fetched in the 'wrong' locale.
This is the line that sets the locale to use in the local findByID function: req.locale = locale ?? req?.locale ?? defaultLocale;
I'm guessing because you're passing the same req through many calls to payload.findByID and payload isn't making a copy of the req, it has all the same variables and so the locale may not be correct for each instance of it being called.
The best thing to do would be to isolate the req.locale property. You could add a function that dereferences the property on the req which you would call before the findByID function is called.
exportdefaultfunctionisolateProperty(req: PayloadRequest,property: string): PayloadRequest{constdelegate={}consthandler: ProxyHandler<PayloadRequest>={deleteProperty(target,p): boolean{returnReflect.deleteProperty(p===property ? delegate : target,p)},get(target,p,receiver){returnReflect.get(p===property ? delegate : target,p,receiver)},has(target,p){returnReflect.has(p===property ? delegate : target,p)},set(target,p,newValue,receiver){if(p===property){// in case of transactionID we must ignore any receiver, because// "If provided and target does not have a setter for propertyKey, the property will be set on receiver instead."returnReflect.set(delegate,p,newValue)}else{returnReflect.set(target,p,newValue,receiver)}},}returnnewProxy(req,handler)}
Called with: isolateProperty(req, 'locale')
I'm pretty sure this will solve your issue and it is something we should consider a fix for the current version. Before we do anything we need to know this is the actual cause and include a reproduction so we can properly diagnose.
@RickGeersing can you provide a reproduction? I threw together this example collection and it worked as I expected, prioritizing en over es which was defined on the request
{slug: 'test',access: {read: ()=>true,},hooks: {afterRead: [async({ req, doc })=>{if(req?.context?.test)returnconsttest=awaitreq.payload.findByID({collection: req.collection?.config?.slugasany,id: doc.id,locale: 'en',// `en`
req,// req.locale is set on `es`context: {test: 'test',},depth: 0,})console.log(test)},],},fields: [{name: 'title',type: 'text',localized: true,},],},
Activity
RickGeersing commentedon Dec 7, 2023
Haven't had the time to test this on 2.0, so could be fixed already. Not sure
[-]Dataloader replaces request with different locale[/-][+]Locale on `req` overwrites `locale` set on `findById`[/+]RickGeersing commentedon Dec 7, 2023
Problem was something completely else, changed the topic and contents.
DanRibbens commentedon Dec 7, 2023
This is the line that sets the locale to use in the local
findByIDfunction:req.locale = locale ?? req?.locale ?? defaultLocale;I'm guessing because you're passing the same
reqthrough many calls topayload.findByIDand payload isn't making a copy of the req, it has all the same variables and so thelocalemay not be correct for each instance of it being called.The best thing to do would be to isolate the req.locale property. You could add a function that dereferences the property on the req which you would call before the findByID function is called.
Called with:
isolateProperty(req, 'locale')I'm pretty sure this will solve your issue and it is something we should consider a fix for the current version. Before we do anything we need to know this is the actual cause and include a reproduction so we can properly diagnose.
JarrodMFlesch commentedon Dec 14, 2023
@RickGeersing can you provide a reproduction? I threw together this example collection and it worked as I expected, prioritizing
enovereswhich was defined on the requestThe request url looked like this:
The response consistently returns the english version of the document.
RickGeersing commentedon Dec 15, 2023
In the meantime we've upgraded to 2.0, that seems to have fixed it.
JarrodMFlesch commentedon Dec 18, 2023
Ok marking as resolved since it is fixed on latest. Thank you for confirming.
github-actions commentedon Sep 7, 2024
This issue has been automatically locked.
Please open a new issue if this issue persists with any additional detail.