Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

kayhadrin
Copy link

@kayhadrin kayhadrin commented May 18, 2020

This PR demonstrates how the continuation-local-storage npm module (actually, a fork of it) could be used to keep track of the initial locale of a web request, when used with the fbt runtime JS API.

Depends on a new (WIP) Fbt runtime API from this PR: facebook/fbt#142

Note that the vanilla continuation-local-storage module is not await friendly, so you might want to investigate how to make it work best for your purposes.

For now, I used cls-hooked instead of node-continuation-local-storage:
See othiym23/node-continuation-local-storage#98 (comment) for more info.

There's maybe hopes to find something more elegant with this other Express plugin: othiym23/node-continuation-local-storage#98 (comment)

"dependencies": {
"@babel/runtime": "^7.9.2",
"babel-plugin-fbt-runtime": "^0.9.4",
"cls-hooked": "^4.2.2",
Copy link
Author

Choose a reason for hiding this comment

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

Used cls-hooked instead of node-continuation-local-storage:
See othiym23/node-continuation-local-storage#98 (comment) for more info.

There's maybe hopes to find something more elegant with this other Express plugin: othiym23/node-continuation-local-storage#98 (comment)

Comment on lines 16 to 28
getViewerContext() {
if (!!isNode && continuationLocalStorage != null) {
const sessionLocale = continuationLocalStorage.getNamespace('session').get('locale');
if (!sessionLocale) {
throw new Error('Unknown sessionLocale');
}
locale = sessionLocale;
}
return {
GENDER: IntlVariations.GENDER_UNKNOWN,
locale: locale || DEFAULT_LOCALE
};
}
Copy link
Author

@kayhadrin kayhadrin May 18, 2020

Choose a reason for hiding this comment

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

Depends on a new (WIP) Fbt runtime API from this PR: facebook/fbt#142

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.

1 participant