mardi 7 mai 2019

When packaging an SPA (and no Java code) into a WAR, how to serve the index.html for every URL except for those that match actual asset files?

I have a single-page webapplication, that when built must be installable into a java-ee application server, such as Glassfish. That is, I should package it into a WAR.

This single-page webapplication is strongly separated from its backend application, it should be buildable and deployable by itself. That is, the WAR only should and will include the single-page webapplication.

If possible, I would like to solve this without having any Java code.

In the case of a single-page application, the URL routing should function like this:

  1. If the requested URL matches a static asset file (html, js, css, etc.), then that file should be served.

  2. In any other case the index.html should be served.

When the second point does not apply, the application (by default) can only be accessed at its entry point (index.html). And not with URLs pointing directly to any subroutes.

There is a workaround, since In Ember.js it is possible to set an option to use hash based subroute URLs. But this has its limiting drawbacks, and I'd like to entirely avoid.

The solution I currently have however is working only by switching on said workaround. I'm using maven and the maven-assembly-plugin to achieve this.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>My App</name>
  <build>
    <finalName>finalName</finalName>
    <plugins>
      <plugin>
        <!-- assemble static content -->
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <descriptors>
            <descriptor>static.xml</descriptor>
          </descriptors>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

static.xml

(dist is the directory where ember-cli puts the compiled assets, that I need to include.)

<?xml version="1.0" encoding="UTF-8"?>
<assembly>
  <id>static</id>
  <formats>
    <format>war</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <fileSets>
    <fileSet>
      <directory>dist</directory>
      <includes>
        <include>**</include>
      </includes>
      <excludes>
        <exclude>WEB-INF/*</exclude>
      </excludes>
      <outputDirectory>/</outputDirectory>
    </fileSet>
    <fileSet>
      <directory>WEB-INF</directory>
      <includes>
        <include>**</include>
      </includes>
      <outputDirectory>/WEB-INF</outputDirectory>
    </fileSet>
  </fileSets>
</assembly>

I also have a web.xml inside the WEB-INF folder, but this is essentially empty.

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                             http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">

</web-app>

So to summarize the question: Is it possible to have said routing logic without writing any Java code, that is without any "gluing" or "middleman" server side module?




Aucun commentaire:

Enregistrer un commentaire