Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Enable Angular Universal with a Spring Boot backend #631

@leon

Description

@leon

Hi!
I'm submitting a feature request to get angular universal working with spring boot

I've been playing with Spring Boot and Nashorn to render javascript views which works great.
Now I would like to get NG2 working with Nashorn.

I've could help out with implementing it but I need some guidance.

To render .ejs files with nashorn you create a spring bean as follows

@Bean
public ScriptTemplateConfigurer reactConfigurer() {
	ScriptTemplateConfigurer configurer = new ScriptTemplateConfigurer();
	configurer.setEngineName("nashorn");
	configurer.setScripts(
		"templates/polyfill.js",
		"templates/ejs.min.js",
		"templates/render.js"
	);
	configurer.setRenderFunction("render");
	configurer.setSharedEngine(false);
	return configurer;
}

// polyfill.js

var window = this;
var global = this;
var console = {};
console.debug = print;
console.warn = print;
console.log = print;
console.error = print;

// render.js

// Create a real JSON object from the model Map

function toJsonObject(model) {
  var o = {};
  for (var k in model) {
    // Convert Iterable like List to real JSON array
    if (model[k] instanceof Java.type("java.lang.Iterable")) {
      o[k] = Java.from(model[k]);
    }
    else {
      o[k] = model[k];
    }
  }
  return o;
}

function render(template, model) {
  return ejs.render(template, toJsonObject(model));
}

Have any of you done any tests with nashorn?
Could you point me in the right direction to get started?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions