We are building a React app using Sitecore JSS and we want to store some data about the user. The data is either info we want to store while the user is logged in (e.g. cache the user profile retrieved from the database) and other data we want to persist (e.g. their language preference). Should we be storing this data in Sitecore context?

In a normal React/SPA app we'd store this data in local and session storage, but we can't do that in a JSS app since the application crashes in Sitecore integrated mode (e.g. when you try to navigate to Sitecore author, you just get an ASP.NET error page) because browser specific resources (e.g. local and session storage) do not exist when Sitecore does Server Side Rendering.

Thanks in advance!

  • You could store the user data on the contact: doc.sitecore.com/developers/100/sitecore-experience-platform/en/… (You will always know the contact id from the AnalyticsCookie - SC_ANALYTICS_GLOBAL_COOKIE) – gorhal Nov 14 '20 at 8:20
  • @gorhal Thanks for the suggestion! Unfortunately, the contact API isn't available in JSS or an SPA (since that data is stored server-side)...at least as far as I can tell from reading the documentation. :) – KingTwinkie Nov 16 '20 at 15:29
  • Yes, it's server-side. But you can use/create a rest api to send the user data(to server-side) and then add it to the contact. Then to get access to the userdata, you could use GetLayoutServiceContext processor(jss.sitecore.com/docs/techniques/extending-layout-service/…) and put the "contact data" in the "context" – gorhal Nov 17 '20 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.