JS stacks

Description

A common optimization is create a stack for reduce the number of request to the server. Tapestry has a mechanism to do it.

In this example we define a own stack independent of the Tapestry core stack (contribute to core stack with so much libraries can produce a delay on initializing page).

Example

We have to include de code below in our AppModule:

public static void contributeJavaScriptStackSource(final MappedConfiguration<String, JavaScriptStack> configuration) {
	configuration.addInstance("libraries-stack", MyJavascriptStack.class);
}}

We can import the stack or some of the individual JS and the stack will import automatically.

Source codes

package es.carlosmontero.webapp.t5devutil.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.tapestry5.Asset;
import org.apache.tapestry5.ioc.services.ThreadLocale;
import org.apache.tapestry5.services.AssetSource;
import org.apache.tapestry5.services.javascript.JavaScriptAggregationStrategy;
import org.apache.tapestry5.services.javascript.JavaScriptStack;
import org.apache.tapestry5.services.javascript.StylesheetLink;

public class MyJavascriptStack implements JavaScriptStack {

   private final List<Asset> js;
   private final List<String> modules;

   public MyJavascriptStack(final AssetSource assetSource, final ThreadLocale threadLocale) {

      js = new ArrayList<Asset>();

      /**
       * Project libraries
       */
      js.add(assetSource.getClasspathAsset("/META-INF/assets/js/js-stack-1.js"));
      js.add(assetSource.getClasspathAsset("/META-INF/assets/js/js-stack-2.js"));

      modules = new ArrayList<String>();

      /**
       * Project modules
       */
      modules.add("module-stack-1");

   }

   @Override
   public List<String> getStacks() {
      return Collections.EMPTY_LIST;
   }

   @Override
   public List<Asset> getJavaScriptLibraries() {
      return js;
   }

   @Override
   public List<StylesheetLink> getStylesheets() {
      return Collections.EMPTY_LIST;
   }

   @Override
   public List<String> getModules() {
      return modules;
   }

   @Override
   public JavaScriptAggregationStrategy getJavaScriptAggregationStrategy() {
      return JavaScriptAggregationStrategy.COMBINE_AND_MINIMIZE;
   }

   @Override
   public String getInitialization() {
      return "";
   }

}