The "Browser Settings" section from the "Site Settings" page of the launchpad now allows enabling "Asynchronous Module Loading".
But once it's enabled, some of the SAPUI5 applications or FLP plugins fail to start. The browser reports in the console:
Failed to execute '<JavaScript module>.js': Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive:
"script-scr * data: blob:".
How is the CSP topic related to the launchpad's "Asynchronous Module Loading" setting in this case and what can we do to avoid evaluating "string as JavaScript" in UI5?

Cause
If "Asynchronous Module Loading" is activated, not only does SAP Fiori launchpad (FLP) bootstrap SAPUI5 with
data-sap-ui-async="true"but serves also its HTML document with thecontent-security-policy(CSP) response header that contains a set of CSP directives omittingunsafe-evalandunsafe-inlinefromscript-src. UI5 contents, that initiate callingevalor contain inline scripts, violate the CSP and thus won't be processed by the browser.In the legacy UI5 code,
evalis called typically due to the application synchronously fetching JS modules via deprecated APIs. For other causes, see the table below.Resolution
It is highly recommended to keep the "Asynchronous Module Loading" setting enabled which improves the overall UI5 performance in FLP (unblocked main thread) and security (stricter CSP).
UI5 has already deprecated legacy/synchronous APIs and — with the 1.96 release — largely improved the support for strict CSP. UI5 content owners should adjust their code accordingly:
data-sap-ui-async="true"or with the debug mode activated.data-sap-ui-async="true"and ensure that no debug mode is activated unnecessarily. C.f. "Is Your Application Ready for Asynchronous Loading?"<script>...</script>) or inline styles (<style>...</style>/style="...").script-srcandstyle-srcdirectives that excludeunsafe-inlineby using:<script src="..." ...></script>data-sap-ui-onInit="module:..."e.g.sap/ui/core/ComponentSupport..cssfiles.jQuery.sap.*,Core#loadLibrary,Core#createComponent,sap.ui.component,sap.ui.*view,sap.ui.controller,sap.ui.*fragment,sap.ui.extensionpoint,sap.ui.commons,sap.ca.scfld,sap.ca.ui,sap.ui.suite,sap.landvisz,sap.ui.vtm,sap.zen.*,sap.ui.ux3,sap.makit,sap.ui.model.odata.ODataModel,sap.ushell.Container.getService, etc..Refer to the API reference to learn about newer APIs that replace the deprecated ones.
When requiring JS modules, only either
sap.ui.defineorsap.ui.require, andsap.ui.loader.configadditionally if needed, should be used.Use the UI5 linter to detect deprecated APIs.
sap-ui-logLevel=...to see the relevant logs."sap.ui.core.IAsyncContentCreation"to theComponent.jsdefinition to enable creating the component content asynchronously and other benefits.UI5 bundles such as
This could be, for example, due to the bundle having been generated via an outdated Grunt build task. Result:Component-preload.jsorlibrary-preload.jscontaining JS modules that are in string.Even when using UI5 Tooling instead, some JS modules can be still in string especially if those modules have
var,let, orconstdeclared in the global scope above thesap.ui.definecall in the module definition. Result:UI5 Tooling would then log the following warning in the terminal:
Generate the bundle by leveraging UI5 Tooling e.g. with
ui5 build -a --clean-dest.When defining a UI5 module, avoid globals and use only
Result:sap.ui.defineat top-level of the JS file.If the module depends on
myGlobal:var myGlobalwithglobalThis.myGlobaland/or wrap the module definition in an IIFE if possible.requiresTopLevelScope="false"to the affectedraw-modulewithin.library. Keep in mind that this approach is not publicly documented.In case the static file
locate-reuse-lib.js, generated by SAP Fiori toos, is also included in the bundle: note thatlocate-reuse-lib.jsis relevant only at development time. It is not supposed to be deployed together with the app.Other resources that might help identifying CSP issues in UI5
UI5 Tooling middleware
cspui5 build --allorui5 build self-contained -a.distfolder (Refer to SAP/ui5-tooling issue #300).E.g. with
ui5 serve --config ui5-dist.yaml.index.html?sap-ui-xx-csp-policy=sap-target-level-<1 to 3>:report-onlysap-target-level-<n>, the stricter the CSP.:report-onlyif the browser should actually apply the CSP level.⚠️ Currently doesn't work in combination with the custom middleware
ui5-middleware-index.Chrome DevTools
F12 → F1 → Select the "Show CSP Violations view" checkbox from the Experiments section.
Q&As
evalcalls when addressing UI5 modules at runtime?Documentation
For more detailed information about the current state of CSP in UI5 and which restrictions there are, see the documentation topic Content Security Policy.
See also the topic Best Practices for Developers for other guidelines.