webetc/history-api-fallback


Fallback to index.html for applications that are using the HTML 5 history API

Download


Step 1. Add the JitPack repository to your build file

Add it in your root settings.gradle at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url 'https://jitpack.io' }
		}
	}

Add it in your settings.gradle.kts at the end of repositories:

	dependencyResolutionManagement {
		repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
		repositories {
			mavenCentral()
			maven { url = uri("https://jitpack.io") }
		}
	}

Add to pom.xml

	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>

Add it in your build.sbt at the end of resolvers:

 
    resolvers += "jitpack" at "https://jitpack.io"
        
    

Add it in your project.clj at the end of repositories:

 
    :repositories [["jitpack" "https://jitpack.io"]]
        
    

Step 2. Add the dependency

	dependencies {
		implementation 'com.github.webetc:history-api-fallback:v1.0'
	}
	dependencies {
		implementation("com.github.webetc:history-api-fallback:v1.0")
	}
	<dependency>
	    <groupId>com.github.webetc</groupId>
	    <artifactId>history-api-fallback</artifactId>
	    <version>v1.0</version>
	</dependency>

                            
    libraryDependencies += "com.github.webetc" % "history-api-fallback" % "v1.0"
        
        

                            
    :dependencies [[com.github.webetc/history-api-fallback "v1.0"]]
        
        

Readme


history-api-fallback

Introduction

Single Page Applications (SPA) typically only utilise one index file that is accessible by web browsers: usually index.html. Navigation in the application is then commonly handled using JavaScript with the help of the HTML5 History API. This results in issues when the user hits the refresh button or is directly accessing a page other than the landing page, e.g. /help or /help/online as the web server bypasses the index file to locate the file at this location. As your application is a SPA, the web server will fail trying to retrieve the file and return a 404 - Not Found message to the user.

This tiny servlet filter addresses some of the issues. Specifically, it will change the requested location to the index you specify (default being index.html) whenever there is a request which fulfils the following criteria:

  1. The request is a GET request
  2. which accepts text/html and is not application/json
  3. is not a direct file request, i.e. the requested path does not contain a . (DOT) character

This project borrows heavily from the connect-history-api-fallback project.