简体   繁体   中英

How to load js files during web application startup through nashorn

I am working on an application which loads multiple js files on client side during first hit in browser. It takes quiet a bit of time to load the first page of our application which can be improved using server side rendering.

We are using java and spring in our backend application. So I am looking for a way to load all the js files on server side during application load(server startup) instead of client side. So I removed js file reference from the jsp and I am trying to load the js files using nashorn as below:

@Configuration
public class ConfigureScript {

  @Bean
  ScriptTemplateConfigurer configurer() {
    ScriptTemplateConfigurer configurer = new ScriptTemplateConfigurer();
    configurer.setEngineName("nashorn");
    configurer.setScripts("/js/common/common.js", "/js/utils/utils.js");
    configurer.setRenderFunction("render");
    configurer.setSharedEngine(false);
    return configurer;
  }
}

But somehow this doesn't seems to work for me since application just keeps loading. Can you guys please suggest the problem with above code or some other way to achieve the server side loading of js files?

Also, if you can suggest is there a way to debug whether js files are loaded properly or not?

  1. for debugging I use postman chrome plugin. When server side rendering takes place - hit to the basic url (eg "/" or any other supposed) returns the content rather than jsp template.
  2. removing js files from jsp - not sure this is correct. js should handle anyway some users interactions in browser after the rendering on server side, except probably that would be fine if content returned from server is static.
  3. in snipped above - two files are loaded. the way how nashorn works - it builds the entire hierarchy of js objects, required for rendering. Are those files enough? Implementation of "render" function should take the job to render.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM