All WebApps and RESTApps use a shared area of the global session. We commonly refer to this as the "App session" and apps access it via the req.session
variable.
This is how it works when an app is executed:
req.session
variable.req.session
state. I.e. all potential local changes are made available for other apps that will be executed later on.This is efficient and works really well when apps are executed serially (i.e. the typical page rendering case). But when an app execution involve other apps, this can cause subtle App session state problems - if some or all of the apps mutate req.session
. Such typical problem case is when a WebApp executes a RESTApp via RestAppInvoker - and one or both of the apps mutates their req.session
.
App session state problems like these can be avoided by using the req.updateSession()
function that is introduced in Sitevision 2023.03.1. The function (re-) synchronizes local req.session
state with the shared App session state.
// index.js of WebApp
router.get('/doSomething', (req, res) => {
/* 1. LOCAL mutable copy of SHARED App session state is made available via req.session here */
...
req.session.beforeMess = 'xxx'; // LOCAL mutation of app session state
const restApp = restAppInvokerFactory.fromPath('/rest-api/my-restapp');
if (restApp) {
// A. Ensure RESTApp gets up-to-date app session state (propagate/push local mutations by this app)
req.updateSession();
const result = restApp.get("/some_route"); // Execute RESTApp
...
// B. Ensure this app gets up-to-date app session state (propagate/fetch App session mutations by RESTApp)
req.updateSession();
}
...
/* 2. SHARED App session state is updated with LOCAL state (req.session) here */
});
// index.js of RESTApp my-restapp
router.get('/some_route', (req, res) => {
/* 1. LOCAL mutable copy of SHARED App session state is made available via req.session here */
...
req.session.someId = 'abc_123'; // LOCAL mutation of app session state
...
/* 2. SHARED App session state is updated with LOCAL state (req.session) here */
});
Rule of thumb: If your app uses req.session
and executes another app - you should consider usage of req.updateSession
Do you want to subscribe to News from Sitevision Developer team? Subscribe here!