Skip to Content
Technical Articles
Author's profile photo Reuven Kozel

How to create a Component-preload.js file in a custom site template

To improve the performance of a custom site template and speed up it’s initial loading, it is recommended to create a Component-preload.js file.

In the following procedure, we explain how to create such a file for your project.

 

Steps:

1. In SAP Web IDE, create a new site template, based on the Basic Layout template.

Project name: anewtemplate

Site template name: mynewtemplate

For more information about how to create a site template, see Create a Site Template.

You should get the following project structure:

2. In your project folder, create the following files:

– package.json

– Gruntfile.js

 

3. Open the package.json file and add the following code:

{
  "name": "anewtemplate",
  "version": "0.0.1",
  "description": "",
  "private": true,
  "devDependencies": {
    "grunt": "1.0.3",
    "grunt-openui5": "^0.14.0"
  },
  "scripts": {}
}

NOTE: The name attribute must be the name of the project.

 

4. Open the Gruntfile.js file and add the following code:

module.exports = function (grunt) {
    grunt.initConfig({
		  "openui5_preload": {
		  }
    });
	grunt.loadNpmTasks('grunt-openui5');
    grunt.registerTask('default', ['openui5_preload']);
}

 

5. Inside the “openui5_preload” attribute, add a script that will create the Component-preload.

In the following example, a Component-preload is added to the AnchorNavigation page which is located under the pageTemplates folder:

module.exports = function (grunt) {
    grunt.initConfig({
	"openui5_preload": {
		"AnchorNavigation": {
                options: {
                  resources: {
                    cwd: 'mynewtemplate/pageTemplates/AnchorNavigation',
                    prefix: 'cpv2/templates/AnchorNavigationV2',
                    src:[
                        '**/*.js',
                        '**/*.fragment.json',
                        '**/*.fragment.xml',
                        '**/*.view.xml',
                        '**/*.properties'
                     ]
                    },
                    dest: "mynewtemplate/pageTemplates/AnchorNavigation"
                },
                components: '**'
            }
		}
    });
	grunt.loadNpmTasks('grunt-openui5');
	grunt.registerTask('default', ['openui5_preload']);
};

 

Note:

  • The string that appears next to cwd and dest is a relative path to the site template that you’ve created. The path consists of “<your-template-name>/pageTemplates/<the-page-name>”.
  • The string that appears next to prefix can be found in the Component.js file of the chosen page. For example, this is the prefix for the AnchorNavigation page, taken from the AnchorNavigation folder -> Component.js file: When you copy this prefix from the Component.js file, make sure to replace the dots with slashes and remove the word “Component”.
  • The rest of the code in the Component-preload example should be copied as-is.

6. To add a Component-preload script to another page in the site template (in this example, the BlankPage), add another script block under the “openui5_preload” section, separated by a comma.

module.exports = function (grunt) {
    grunt.initConfig({
        "openui5_preload": {
            "AnchorNavigation": {
	      // Here goes the previous code
            },
            "BlankPage": {
                options :{
                  resources: {
                    cwd: 'mynewtemplate/pageTemplates/BlankPage',
                    prefix: 'cpv2/templates/Blank',
                    src:[
                        '**/*.js',
                        '**/*.fragment.json',
                        '**/*.fragment.xml',
                        '**/*.view.xml',
                        '**/*.properties'
                     ]
                    },
                    dest:"mynewtemplate/pageTemplates/BlankPage"
                } ,
                components: '**'
            }
        }
    });

 

7. Right-click the project -> Build -> Build Project.

The build might take a few minutes to complete.

 

After the build finishes successfully, you can find the Component-preload.js file under the pageTemplate folder.

 

8. Before deploying the project, you must delete the package.json file (and package-lock.json file, if exists).

 

9. Right-click the project-> Deploy -> Deploy to SAP Cloud Platform.

 

10. After the deployment finishes successfully, you can test your flow.

Open the Site Directory and create a site using the site template that you’ve created.

The new site template will load the Component-preload.js instead of Component.js and you should notice a faster loading!

 

In conclusion, we know how important performance is to our sites. The lack of Component-preload files in our custom site templates can harm the performance and slow things down. In this blog post, we’ve seen how easy it is to add Component-preload files to our custom site templates and rapidly increase the performance.

Hope this will help you make better sites with better performance!

Assigned Tags

      3 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Sven Kannengiesser
      Sven Kannengiesser

      Hi, Reuven!

      That is exactly what I was trying to do for some time now. You nailed it! In my previous attempts I worked with a local version where I set up some Gulp tasks to produce a Component-preload.js, additionally embedding even CSS files via a trick. I synced this local version via Git with the project residing in the WebIDE (for deployment). That was two years ago.

      I think I’ll rewrite some of it to work with WebIDE’s build process.

      Thanks again for this post,

      Sven

      Author's profile photo Karel Verbanck
      Karel Verbanck

      Thank you for the great post.

      It works like a charm!

       

      Quick question: is this something that SAP will provide out of the box in the future? (seems like a basic functionality that i was missing for a while in the site templates)

       

      Kr,

      Karel

      Author's profile photo Zayidu Ansari
      Zayidu Ansari

      Hello Reuven,

      Thanks for the post. Nice article. Can we do generate a Component-preload file using ui5-tooling? if so what will be the build command?

       

      My current build command, I anyways don't build my project as it is downloading all the Ui5 libraries and dependencies instead I use "https://sapui5.netweaver.ondemand.com/resources/sap-ui-core.js" to load the ui5 library.

      "build": "ui5 build --clean-dest"

      Hence is it possible to generate the Component-preload file in webapp or dev folder instead in the dist folder.

      Thanks