Loading proofofbrain-blog...

Language Experiment for ProofOfBrain.Blog

Things Were being Displayed in the Wrong Language

Several months ago, I added the ability for ProofofBrain to look at the headers to determine which language you would prefer. Headers by the way are sent automatically by the browser and should be used to determine your language.

I started noticing some elements of ProofofBrain.blog being displayed in what I now think is Ukrainian. And until now, I had not had an explanation on why.

Proof of Brain has both server side rendering. Here is what the code looked like:

export const makePreloadedState = async (
  req: express.Request
): Promise<AppState> => {
  /* 
  
  req.headers['accept-language'] is the accept-language header which looks like
  the following:
  
  'en,en-US;q=0.9,es-AR;q=0.8,es;q=0.7'
  
  Although Brave sorts this in decending order, this is not a standard behavior.
  So it must be sorted here.
  
  */
  
  const headers = (req && req.headers) || {};
  const rawAcceptLanguage = headers["accept-language"] || "";
  ....

The headers here are queried for your favorite languages and the one you prefer the best (out of what Proof of Brain supports) will be served to you. I don't modify this so it should be deterministic.

Now that the headers member will be different for each call but I think that somehow these values are being overwritten by subsequent calls.

The Race

It seems to me the routine can be called concurrently but the req passed in are not independent and the headers member in both cases actually are the same memory. So when there is a new call to this routine, the instance of req for the English preference is the same as the one for the Ukrainian user!

The Change

I am changing the first line of the routine to read:

  const headers = (req && {...req.headers}) || {};

After this change headers and req.headers still get the same value initially but later changing req.headers wont impact ``headers```. I cannot imagine how they are modifying the strings rather than setting new ones but I am cloning the accept-language header also just in case.

H2
H3
H4
3 columns
2 columns
1 column
2 Comments