Include path prefix in location of js asset bundle (#959)

Problem
If you navigate directly to (or do a hard refresh on) a path with more than one segment, 
e.g. http://localhost:8084/namespaces/conduit, the dashboard js is not served. 
Pages with two paths have to be accessed by loading the dashboard on a different 
path and then clicking through.

When accessing the dashboard via conduit dashboard we append a path prefix so that 
we can connect using the k8s proxy. This means that moving the dashboard to serve 
images off relative paths won't work, because we need to serve images whether the 
dashboard is loaded from http://localhost:8084/namespaces/conduit or 
from http://localhost:8084/namespaces. 

Solution
Check whether we're serving the dashboard with the proxy url, and if we are, adjust
the url at which we serve the index bundle from.
I've also added a very manual override if the conduit logo can't be found at the usual url.
This commit is contained in:
Risha Mars 2018-05-16 15:41:50 -07:00 committed by GitHub
parent fbba6bf4c8
commit 1464470487
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 8 deletions

View File

@ -83,7 +83,14 @@ export default class Sidebar extends React.Component {
<div className="sidebar">
<div className={`sidebar-menu-header ${this.state.collapsed ? "collapsed" : ""}`}>
<ConduitLink to="/servicemesh"><img src={this.state.collapsed ? logo : wordLogo} /></ConduitLink>
<ConduitLink to="/servicemesh">
<img
src={this.state.collapsed ? logo : wordLogo}
onError={e => {
// awful hack to deal with the fact that we don't serve assets off absolute paths
e.target.src = e.target.src.replace(/(.*)(\/[a-zA-Z]*)(\/dist)(.*)/, "$1$3$4");
}} />
</ConduitLink>
</div>
<Menu

View File

@ -2,12 +2,15 @@ package srv
import (
"net/http"
"regexp"
"github.com/julienschmidt/httprouter"
pb "github.com/runconduit/conduit/controller/gen/public"
log "github.com/sirupsen/logrus"
)
var proxyPathRegexp = regexp.MustCompile("/api/v1/namespaces/.*/proxy/")
type (
renderTemplate func(http.ResponseWriter, string, string, interface{}) error
serveFile func(http.ResponseWriter, string, string, interface{}) error
@ -22,7 +25,17 @@ type (
)
func (h *handler) handleIndex(w http.ResponseWriter, req *http.Request, p httprouter.Params) {
params := appParams{UUID: h.uuid, ControllerNamespace: h.controllerNamespace}
// when running the dashboard via `conduit dashboard`, serve the index bundle at the right path
pathPfx := proxyPathRegexp.FindString(req.URL.Path)
if pathPfx == "" {
pathPfx = "/"
}
params := appParams{
UUID: h.uuid,
ControllerNamespace: h.controllerNamespace,
PathPrefix: pathPfx,
}
version, err := h.apiClient.Version(req.Context(), &pb.Empty{}) // TODO: remove and call /api/version from web app
if err != nil {

View File

@ -42,6 +42,7 @@ type (
ControllerNamespace string
Error bool
ErrorMessage string
PathPrefix string
}
)

View File

@ -11,11 +11,9 @@
{{ end }}
{{ define "script-tags" }}
{{with .Context}}
{{ if .WebpackDevServer}}
<script type="text/javascript" src="{{.WebpackDevServer}}/dist/index_bundle.js" async></script>
{{else}}
<script type="text/javascript" src="dist/index_bundle.js" async></script>
{{end}}
{{ if .Context.WebpackDevServer }}
<script type="text/javascript" src="{{.Context.WebpackDevServer}}/dist/index_bundle.js" async></script>
{{else}}
<script type="text/javascript" src="{{.Contents.PathPrefix}}dist/index_bundle.js" async></script>
{{end}}
{{end}}