Config and Global Settings
The Config object in @serenity-is/corelib holds global runtime configuration for the client framework. Values are typically set once during application startup (e.g. in ScriptInit.ts) and read throughout the app.
Config Properties
| Property | Description |
|---|---|
applicationPath |
Root path of the application (e.g. "/mysite/"). Initialized from <link id="ApplicationPath">. |
cspNonce |
Content Security Policy nonce for dynamically created <script>/<style> tags. |
rootNamespaces |
Root namespaces probed when resolving short type names. Defaults to ["Serenity"]. |
defaultReturnUrl |
Fallback URL to redirect to when no explicit return URL is provided. |
emailAllowOnlyAscii |
Whether e-mail validation allows only ASCII characters (default true). |
lazyTypeLoader |
Callback to lazily resolve a type not yet in the type registry (for code-splitting). |
notLoggedInHandler |
Handler invoked when a service call returns NotAuthorized/session expired. |
Setting Values in ScriptInit.ts
The most common configuration is adding your application namespace to rootNamespaces so short type names resolve:
import { Config } from "@serenity-is/corelib";
Config.rootNamespaces.push("MyApp");
Other examples:
import { Config, getWindowLocation } from "@serenity-is/corelib";
Config.emailAllowOnlyAscii = false;
Config.defaultReturnUrl = (purpose) => purpose === "logout" ? "/Goodbye" : Config.applicationPath;
Config.notLoggedInHandler = () => getWindowLocation({ top: true }).href = "/Account/Login";
applicationPath
Config.applicationPath is the root path of the application, always starting and ending with /. It is initialized from <link id="ApplicationPath" href="/mysite/"> in _LayoutHead.cshtml, falling back to "/".
It is used by resolveUrl to resolve ~/-prefixed URLs:
import { resolveUrl } from "@serenity-is/corelib";
resolveUrl("~/upload/xyz.png"); // "/mysite/upload/xyz.png"
If you change the link element at runtime, call resetApplicationPath() to re-read it:
document.getElementById("ApplicationPath").href = "/newPath/";
resetApplicationPath();
cspNonce
Config.cspNonce is the Content Security Policy nonce applied to dynamically created <script>/<style> tags. It is initialized from <meta name="csp-nonce"> or the nonce attribute of existing script/style elements. Helpers that inject markup copy this value to the nonce attribute.
lazyTypeLoader
Config.lazyTypeLoader lets you lazily load types (e.g. with code-splitting) when they are requested but not yet registered:
Config.lazyTypeLoader = async (typeKey) => await import(`./editors/${typeKey}`);
The default loader handles the types marked with the LazyLoadInclude marker, using the JSON block rendered by
@Html.LazyTypes() in the layout head (see Type Registration). It is called
with the requested type key and a kind hint ("dialog", "editor", "enum", "formatter", "filtering", ...), and
can also be asked to only check availability (third argument) without importing a module — which the registries use to
decide whether a type exists before requesting it.
If you replace it, wrap the existing value, since the default loader is what resolves the types code generation declared:
const defaultLoader = Config.lazyTypeLoader;
Config.lazyTypeLoader = (key, kind, checkOnly) => defaultLoader(key, kind, checkOnly) ?? myOwnLoader(key, kind);
getWindowLocation
getWindowLocation() returns the location object the framework should navigate. Use it instead of touching window.location directly:
import { getWindowLocation } from "@serenity-is/corelib";
getWindowLocation().href = "/Home"; // current window
await getWindowLocation().reload(); // current window
getWindowLocation({ top: true }).href = "/Account/Login"; // break out of an iframe
Why it exists:
- Tests (jsdom) do not implement navigation, so directly assigning
window.locationlogs "Not implemented" warnings.getWindowLocation()gives tests a single seam to replace the returned object. - When the application runs in an iframe, a redirect may need to replace the top window rather than the frame. Pass
{ top: true }for that, as the service call 403 redirect does.
The built-in pages (login, sign-up, password actions, idle timeout, two-factor, external logins) all use it.
getWindowHistory
getWindowHistory() is the history counterpart of getWindowLocation(), for code that reads or changes the history of
the page, e.g. a page that keeps its state in the hash:
import { getWindowHistory, getWindowLocation } from "@serenity-is/corelib";
const history = getWindowHistory();
history.replaceState(history.state, "", getWindowLocation().pathname + "?tab=2");
It exists for the same reason: a test can replace it, e.g. with a history whose back() takes effect at once, as jsdom
runs a traversal in a later task, which would change the address after the test has ended. Pass { top: true } for the
history of the top window, as with the location. The router and the other parts of the framework read the location and
the history through these two functions.
windowHooks
windowHooks.location and windowHooks.history replace what getWindowLocation() and getWindowHistory() return, for
every caller, including the code inside corelib, e.g. its router, which a spy on the functions doesn't reach. A hook
returns the object to use, or undefined to fall back to the window's own:
import { windowHooks } from "@serenity-is/corelib";
windowHooks.history = () => myHistory;
// ...
windowHooks.history = void 0;
See Also
- Config (API reference) — the full
Configobject. - windowHooks (API reference) — the hooks behind the two functions.
- resetApplicationPath (API reference) — re-read the application path.
- resetCspNonce (API reference) — re-read the CSP nonce.
- resolveUrl (API reference) — resolve
~/URLs. - Type Registration — how
rootNamespacesis used. - Frontend Framework Overview — the three client-side packages and how they fit together.