diff --git a/polygerrit-ui/server.go b/polygerrit-ui/server.go index e849469286..faf28adf84 100644 --- a/polygerrit-ui/server.go +++ b/polygerrit-ui/server.go @@ -103,7 +103,7 @@ func resourceBasePath() (string, error) { func handleIndex(writer http.ResponseWriter, originalRequest *http.Request) { fakeRequest := &http.Request{ URL: &url.URL{ - Path: "/", + Path: "/", RawQuery: originalRequest.URL.RawQuery, }, } @@ -170,7 +170,7 @@ func setJsonPropByPath(json map[string]interface{}, path []string, value interfa func patchResponse(req *http.Request, res *http.Response) io.Reader { switch req.URL.EscapedPath() { case "/": - return replaceCdn(res.Body) + return rewriteHostPage(res.Body) case "/config/server/info": return injectLocalPlugins(res.Body) default: @@ -178,13 +178,39 @@ func patchResponse(req *http.Request, res *http.Response) io.Reader { } } -func replaceCdn(reader io.Reader) io.Reader { +func rewriteHostPage(reader io.Reader) io.Reader { buf := new(bytes.Buffer) buf.ReadFrom(reader) original := buf.String() + // Simply remove all CDN references, so files are loaded from the local file system or the proxy + // server instead. replaced := cdnPattern.ReplaceAllString(original, "") + // Modify window.INITIAL_DATA so that it has the same effect as injectLocalPlugins. To achieve + // this let's add JavaScript lines at the end of the snippet that also + // contains window.INITIAL_DATA=... + // Here we rely on the fact that the ") + builder := new(strings.Builder) + builder.WriteString( + "window.INITIAL_DATA['/config/server/info'].plugin.html_resource_paths = []; ") + builder.WriteString( + "window.INITIAL_DATA['/config/server/info'].plugin.js_resource_paths = []; ") + for _, p := range strings.Split(*plugins, ",") { + if filepath.Ext(p) == ".html" { + builder.WriteString( + "window.INITIAL_DATA['/config/server/info'].plugin.html_resource_paths.push('" + p + "'); ") + } + if filepath.Ext(p) == ".js" { + builder.WriteString( + "window.INITIAL_DATA['/config/server/info'].plugin.js_resource_paths.push('" + p + "'); ") + } + } + replaced = replaced[:insertionPoint] + builder.String() + replaced[insertionPoint:] + } + return strings.NewReader(replaced) } @@ -209,11 +235,11 @@ func injectLocalPlugins(reader io.Reader) io.Reader { jsResources := getJsonPropByPath(response, jsPluginsPath).([]interface{}) for _, p := range strings.Split(*plugins, ",") { - if strings.HasSuffix(p, ".html") { + if filepath.Ext(p) == ".html" { htmlResources = append(htmlResources, p) } - if strings.HasSuffix(p, ".js") { + if filepath.Ext(p) == ".js" { jsResources = append(jsResources, p) } }