Issue
- What are the differences in the usage of
javascript.barebone.filesandjavascript.everything.filesacross Liferay versions?
Environment
- Liferay DXP 7.0+
Resolution
-
Importing Javascript files
in Liferay 7.0
On Liferay 7.0 there are thejavascript.barebone.filesandjavascript.everything.filesportal properties for listing the JavaScript files that will be loaded automatically in/html/common/themes/top_js.jsp
The custom JS files that can be added to these two lists must be located in the folder “/html/js” of a webapp.
The two lists of files exist for performance reasons because unauthenticated users usually do not utilize all the JavaScript that is available. As the name suggests, thebarebonelist is the minimum list of JavaScript files required for most cases. Theeverythinglist includes everything else not listed in the barebone list.
The list of files are also merged and packed intoeverything.jsporbarebone.jspfor further performance improvements, this is controlled by thejavascript.fast.loadportal property.Let's see how it is implemented in the
top_js.jsp:<c:choose> <c:when test="<%= themeDisplay.isThemeJsFastLoad() %>"> <c:choose> <c:when test="<%= themeDisplay.isThemeJsBarebone() %>"> <script src="<%= HtmlUtil.escape(PortalUtil.getStaticResourceURL(request, themeDisplay.getCDNDynamicResourcesHost() + themeDisplay.getPathJavaScript() + "/barebone.jsp", "minifierBundleId=javascript.barebone.files", jsLastModified)) %>" type="text/javascript"></script> </c:when> <c:otherwise> <script src="<%= HtmlUtil.escape(PortalUtil.getStaticResourceURL(request, themeDisplay.getCDNDynamicResourcesHost() + themeDisplay.getPathJavaScript() + "/everything.jsp", "minifierBundleId=javascript.everything.files", jsLastModified)) %>" type="text/javascript"></script> </c:otherwise> </c:choose> </c:when> <c:otherwise> <% String path = themeDisplay.getCDNHost().concat(themeDisplay.getPathJavaScript()); String[] javaScriptFiles = null; if (themeDisplay.isThemeJsBarebone()) { javaScriptFiles = JavaScriptBundleUtil.getFileNames(PropsKeys.JAVASCRIPT_BAREBONE_FILES); } else { javaScriptFiles = JavaScriptBundleUtil.getFileNames(PropsKeys.JAVASCRIPT_EVERYTHING_FILES); } for (String javaScriptFile : javaScriptFiles) { %> <script data-senna-track="permanent" src="<%= HtmlUtil.escape(PortalUtil.getStaticResourceURL(request, path + "/" + javaScriptFile, "minifierType=", jsLastModified)) %>" type="text/javascript"></script> <% } %> </c:otherwise> </c:choose> -
If
themeDisplay.isThemeJsFastLoad()then thebarebone.jsporeverything.jspare loaded, otherwise the JS files listed atjavascript.barebone.filesorjavascript.everything.filesare loaded instead -
If
themeDisplay.isThemeJsBarebone()then thebarebone.jspor the JS files listed atjavascript.barebone.filesare loaded, otherwise theeverything.jspor the JS files listed atjavascript.everything.fileswill be loaded instead -
The
themeDisplay.isThemeJsFastLoad()value can be controlled by thejavascript.fast.loadportal property which is true by default, but let's check thethemeDisplay.isThemeJsBarebone():
ThethemeJsBarebonevalue is set in theServicePreActionclass:boolean themeJsBarebone = PropsValues.JAVASCRIPT_BAREBONE_ENABLED;
if (themeJsBarebone && (signedIn || PropsValues.JAVASCRIPT_SINGLE_PAGE_APPLICATION_ENABLED)) {
themeJsBarebone = false;
}The themeJsBarebone value is set by the
javascript.barebone.enabledportal property which is true by default.
ThePropsValues.JAVASCRIPT_SINGLE_PAGE_APPLICATION_ENABLEDis set by thejavascript.single.page.application.enabledportal property which is again true by default.
Based on the logic, if thePropsValues.JAVASCRIPT_SINGLE_PAGE_APPLICATION_ENABLED, then barebone is never loaded, otherwise if not signed in the barebone is loaded.So by default (
javascript.single.page.application.enabled=true,javascript.fast.load=true) theeverything.jspis loaded which is merging and packing the files listed at bothjavascript.barebone.filesandjavascript.everything.files, which means that the performance improvement cannot be leveraged by distributing the JS files between barebone and everything. -
in Liferay 7.1+
From Liferay 7.1, the
javascript.barebone.filesandjavascript.everything.filesproperties are deprecated because they can be configured via OSGi using BND headers. These properties will be removed in a future release.
From 7.1, this functionality can be handled with theLiferay-JS-Resources-Top-Head-AuthenticatedandLiferay-JS-Resources-Top-Headbnd properties similar to the portal properties.
If the user is not authenticated, then the files listed atLiferay-JS-Resources-Top-Headare loaded, and, if the user is authenticated, then the files are listed at bothLiferay-JS-Resources-Top-HeadandLiferay-JS-Resources-Top-Head-Authenticatedare loaded.In addition, similar to 7.0, if the portal is used with
javascript.single.page.application.enabled=true, this won't make any difference between authenticated user and non-authenticated, in every case all js files will be loaded.So, by default there is no need to differentiate between
Liferay-JS-Resources-Top-Head-AuthenticatedandLiferay-JS-Resources-Top-Headfiles because all the JS files are loaded. -
Attached the my-top-head-extender.zip and com.liferay.my.top.head.extender.jar files.
In the portlet, the js files have been added and theLiferay-JS-Resources-Top-Head-AuthenticatedandLiferay-JS-Resources-Top-Headhave been set in the bnd.bnd file.
Dynamic including Javascript files
There is another way to load Javascript files via Top JS Dynamic Include: with this solution we are able to include additional JavaScript files to the theme’s head dynamically, exactly on top oftop_js.jspf.As an example, attached the com.liferay.dynamic.include.jar and the my-custom-dynamic-include.zip of the source of the portlet.