Skip to content

Instantly share code, notes, and snippets.

@EduardoAC
Created February 11, 2024 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EduardoAC/29065b731cd7bddd4cc1694b013049c9 to your computer and use it in GitHub Desktop.
Save EduardoAC/29065b731cd7bddd4cc1694b013049c9 to your computer and use it in GitHub Desktop.
Persist Language in a client-side cookie using js-cookie library
import Cookies from "js-cookie"
const languageCookieKey = "{your-webApp-name}-language"
const cookieOptions = {
// ...
// Only set secure if we are running on an https environment.
// secure: true => force cookie to be only readable by https requests.
secure: window?.location?.protocol === "https:",
}
export function setLanguageCookie(language) {
// Save language in cookie on the client side - only single browser support required
Cookies.set(languageCookieKey, language, cookieOptions)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment