gocacheallrouter

package module
v0.0.0-...-82d1513 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 21, 2020 License: MIT Imports: 3 Imported by: 0

README

Cache-all routing for Javascript SPA

React.js, Angular.js, Vue.js all support window.history as a way to have dynamic front-end URL's served from the same base index.html file (plus assets).

How should standard Go routers be setup to make use of a "cache-all" design to serve the index.html for every URL path except the static CSS/Javascript/Media assets (and possible Go API paths)?

Background

Serving static assets is easy with Go. Assuming your react/angular/vue project is built/bundled into the "build" directory you could use the following to have Go serve them.

http.FileServer(http.Dir(("build/"))

However, since most single page apps (SPA) use routing (window.history), you can't only serve assets with the same URL path as filesystem path. You also have to serve a "cache-all" index.html file to every possible made up URL path since the SPA will handle the rendering for that path.

This project shows how to create cache-all routes while serving other specific routes.

Documentation

Index

Constants

View Source
const APIResponse = "api"

APIResponse body

View Source
const IndexResponse = "<script src='/js/app.js'></script>"

IndexResponse body

View Source
const JavascriptResponse = "document.write('JS loaded')"

JavascriptResponse body

Variables

View Source
var APIHandler = func(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte(APIResponse))
}

APIHandler would represent a response from the Go API

View Source
var MemoryHTTPFilesystem http.FileSystem

MemoryHTTPFilesystem to pretend we are reading from a SPA directory

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL