diff --git a/utils/subpath.go b/utils/subpath.go index cddc90fa48b1caf08ef662ed1192dd8198d1d571..be06a73ef1d9df194be2280acc82028f65883a2d 100644 --- a/utils/subpath.go +++ b/utils/subpath.go @@ -89,14 +89,14 @@ func UpdateAssetsSubpath(subpath string) error { return errors.Wrapf(err, "failed to update root.html with subpath %s", subpath) } - // Rewrite the *.css references to `/static/*` (or a previously rewritten subpath). + // Rewrite the manifest.json and *.css references to `/static/*` (or a previously rewritten subpath). err = filepath.Walk(staticDir, func(walkPath string, info os.FileInfo, err error) error { - if filepath.Ext(walkPath) == ".css" { - if oldCss, err := ioutil.ReadFile(walkPath); err != nil { + if filepath.Base(walkPath) == "manifest.json" || filepath.Ext(walkPath) == ".css" { + if old, err := ioutil.ReadFile(walkPath); err != nil { return errors.Wrapf(err, "failed to open %s", walkPath) } else { - newCss := strings.Replace(string(oldCss), pathToReplace, newPath, -1) - if err = ioutil.WriteFile(walkPath, []byte(newCss), 0); err != nil { + new := strings.Replace(string(old), pathToReplace, newPath, -1) + if err = ioutil.WriteFile(walkPath, []byte(new), 0); err != nil { return errors.Wrapf(err, "failed to update %s with subpath %s", walkPath, subpath) } } diff --git a/utils/subpath_test.go b/utils/subpath_test.go index ee518d5f6a368c0c9d33c2bb63e0ecb8d7fb48ad..6e417e1c511cfdb5778bdb98ba7b8902b620e58d 100644 --- a/utils/subpath_test.go +++ b/utils/subpath_test.go @@ -33,52 +33,64 @@ func TestUpdateAssetsSubpath(t *testing.T) { require.NoError(t, err) testCases := []struct { - Description string - RootHTML string - MainCSS string - Subpath string - ExpectedRootHTML string - ExpectedMainCSS string + Description string + RootHTML string + MainCSS string + ManifestJSON string + Subpath string + ExpectedRootHTML string + ExpectedMainCSS string + ExpectedManifestJSON string }{ { "no changes required, empty subpath provided", baseRootHtml, baseCss, + baseManifestJson, "", baseRootHtml, baseCss, + baseManifestJson, }, { "no changes required", baseRootHtml, baseCss, + baseManifestJson, "/", baseRootHtml, baseCss, + baseManifestJson, }, { "subpath", baseRootHtml, baseCss, + baseManifestJson, "/subpath", subpathRootHtml, subpathCss, + subpathManifestJson, }, { "new subpath from old", subpathRootHtml, subpathCss, + subpathManifestJson, "/nested/subpath", newSubpathRootHtml, newSubpathCss, + newSubpathManifestJson, }, { "resetting to /", subpathRootHtml, subpathCss, + baseManifestJson, "/", resetRootHtml, baseCss, + baseManifestJson, }, } @@ -86,6 +98,7 @@ func TestUpdateAssetsSubpath(t *testing.T) { t.Run(testCase.Description, func(t *testing.T) { ioutil.WriteFile(filepath.Join(tempDir, model.CLIENT_DIR, "root.html"), []byte(testCase.RootHTML), 0700) ioutil.WriteFile(filepath.Join(tempDir, model.CLIENT_DIR, "main.css"), []byte(testCase.MainCSS), 0700) + ioutil.WriteFile(filepath.Join(tempDir, model.CLIENT_DIR, "manifest.json"), []byte(testCase.ManifestJSON), 0700) err := utils.UpdateAssetsSubpath(testCase.Subpath) require.NoError(t, err) @@ -97,6 +110,9 @@ func TestUpdateAssetsSubpath(t *testing.T) { require.NoError(t, err) require.Equal(t, testCase.ExpectedMainCSS, string(contents)) + contents, err = ioutil.ReadFile(filepath.Join(tempDir, model.CLIENT_DIR, "manifest.json")) + require.NoError(t, err) + require.Equal(t, testCase.ExpectedManifestJSON, string(contents)) }) } }) @@ -190,3 +206,207 @@ const newSubpathRootHtml = ` Mattermost

Cannot connect to Mattermost


We're having trouble connecting to Mattermost. If refreshing this page (Ctrl+R or Command+R) does not work, please verify that your computer is connected to the internet.


` + +const baseManifestJson = `{ + "icons": [ + { + "src": "/static/icon_96x96.png", + "sizes": "96x96", + "type": "image/png" + }, + { + "src": "/static/icon_32x32.png", + "sizes": "32x32", + "type": "image/png" + }, + { + "src": "/static/icon_16x16.png", + "sizes": "16x16", + "type": "image/png" + }, + { + "src": "/static/icon_76x76.png", + "sizes": "76x76", + "type": "image/png" + }, + { + "src": "/static/icon_72x72.png", + "sizes": "72x72", + "type": "image/png" + }, + { + "src": "/static/icon_60x60.png", + "sizes": "60x60", + "type": "image/png" + }, + { + "src": "/static/icon_57x57.png", + "sizes": "57x57", + "type": "image/png" + }, + { + "src": "/static/icon_152x152.png", + "sizes": "152x152", + "type": "image/png" + }, + { + "src": "/static/icon_144x144.png", + "sizes": "144x144", + "type": "image/png" + }, + { + "src": "/static/icon_120x120.png", + "sizes": "120x120", + "type": "image/png" + }, + { + "src": "/static/icon_192x192.png", + "sizes": "192x192", + "type": "image/png" + } + ], + "name": "Mattermost", + "short_name": "Mattermost", + "orientation": "any", + "display": "standalone", + "start_url": ".", + "description": "Mattermost is an open source, self-hosted Slack-alternative", + "background_color": "#ffffff" +} +` + +const subpathManifestJson = `{ + "icons": [ + { + "src": "/subpath/static/icon_96x96.png", + "sizes": "96x96", + "type": "image/png" + }, + { + "src": "/subpath/static/icon_32x32.png", + "sizes": "32x32", + "type": "image/png" + }, + { + "src": "/subpath/static/icon_16x16.png", + "sizes": "16x16", + "type": "image/png" + }, + { + "src": "/subpath/static/icon_76x76.png", + "sizes": "76x76", + "type": "image/png" + }, + { + "src": "/subpath/static/icon_72x72.png", + "sizes": "72x72", + "type": "image/png" + }, + { + "src": "/subpath/static/icon_60x60.png", + "sizes": "60x60", + "type": "image/png" + }, + { + "src": "/subpath/static/icon_57x57.png", + "sizes": "57x57", + "type": "image/png" + }, + { + "src": "/subpath/static/icon_152x152.png", + "sizes": "152x152", + "type": "image/png" + }, + { + "src": "/subpath/static/icon_144x144.png", + "sizes": "144x144", + "type": "image/png" + }, + { + "src": "/subpath/static/icon_120x120.png", + "sizes": "120x120", + "type": "image/png" + }, + { + "src": "/subpath/static/icon_192x192.png", + "sizes": "192x192", + "type": "image/png" + } + ], + "name": "Mattermost", + "short_name": "Mattermost", + "orientation": "any", + "display": "standalone", + "start_url": ".", + "description": "Mattermost is an open source, self-hosted Slack-alternative", + "background_color": "#ffffff" +} +` + +const newSubpathManifestJson = `{ + "icons": [ + { + "src": "/nested/subpath/static/icon_96x96.png", + "sizes": "96x96", + "type": "image/png" + }, + { + "src": "/nested/subpath/static/icon_32x32.png", + "sizes": "32x32", + "type": "image/png" + }, + { + "src": "/nested/subpath/static/icon_16x16.png", + "sizes": "16x16", + "type": "image/png" + }, + { + "src": "/nested/subpath/static/icon_76x76.png", + "sizes": "76x76", + "type": "image/png" + }, + { + "src": "/nested/subpath/static/icon_72x72.png", + "sizes": "72x72", + "type": "image/png" + }, + { + "src": "/nested/subpath/static/icon_60x60.png", + "sizes": "60x60", + "type": "image/png" + }, + { + "src": "/nested/subpath/static/icon_57x57.png", + "sizes": "57x57", + "type": "image/png" + }, + { + "src": "/nested/subpath/static/icon_152x152.png", + "sizes": "152x152", + "type": "image/png" + }, + { + "src": "/nested/subpath/static/icon_144x144.png", + "sizes": "144x144", + "type": "image/png" + }, + { + "src": "/nested/subpath/static/icon_120x120.png", + "sizes": "120x120", + "type": "image/png" + }, + { + "src": "/nested/subpath/static/icon_192x192.png", + "sizes": "192x192", + "type": "image/png" + } + ], + "name": "Mattermost", + "short_name": "Mattermost", + "orientation": "any", + "display": "standalone", + "start_url": ".", + "description": "Mattermost is an open source, self-hosted Slack-alternative", + "background_color": "#ffffff" +} +`