Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MM-57839: Rewrite static assets only if needed #27076

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

agarciamontoro
Copy link
Member

@agarciamontoro agarciamontoro commented May 20, 2024

Summary

Refactor UpdateAssetsSubpathInDir so that the actual rewriting of files happen in two different functions: one for root.html, another for manifest.json and *.css files.

I would have wanted to simply do an if pathToReplace == newPath { return nil } when those two are defined, but the logic for root.html is not that simple*, and that may miss some edge cases, so I opted out for simply moving the actual rewriting to individual functions, which check for their corresponding conditions to perform the update:

  1. for root.html, check that the edited file is indeed different than the original one
  2. for manifest.json and *.css files, rewrite them only if pathToReplace != newPath, which in this case is clear that that's the only modification we do.

The logic is exactly the same as before except for skipping the rewrite when those conditions are not met.

*I don't really understand this block:

// Determine if a previous subpath had already been rewritten into the assets.
reWebpackPublicPathScript := regexp.MustCompile("window.publicPath='([^']+/)static/'")
alreadyRewritten := false
if matches := reWebpackPublicPathScript.FindStringSubmatch(string(oldRootHTML)); matches != nil {
	oldSubpath = matches[1]
	alreadyRewritten = true
}

When can that be rewritten if not here?

Ticket Link

https://mattermost.atlassian.net/browse/MM-57839

Screenshots

--

Release Note

NONE

Fix #26769

Refactor UpdateAssetsSubpathInDir so that the actual rewriting of files
happen in two different functions: one for root.html, another for
manifest.json and *.css files.

I would have wanted to simply do a

  if pathToReplace == newPath { return nil }

when those two variables are defined, but the logic for root.html is not
that simple, and that may miss some edge cases, so I opted out for
simply moving the actual rewriting to individual functions, which check
for their corresponding conditions to perform the update:
  1. for root.html, check that the edited file is indeed different than
     the original one
  2. for manifest.json and *.css files, rewrite them only if
     pathToReplace != newPath, which in this case is clear that that's the
     only modification we do
@agarciamontoro agarciamontoro added the 2: Dev Review Requires review by a developer label May 20, 2024
@mm-cloud-bot mm-cloud-bot added the release-note-none Denotes a PR that doesn't merit a release note. label May 20, 2024
@agarciamontoro
Copy link
Member Author

@hmhealey, adding you here in case you're familiar with this client static assets rewriting.

@agnivade agnivade requested review from lieut-data and removed request for agnivade May 21, 2024 04:21
@agnivade
Copy link
Member

Swapping me for Jesse, who should have more context on this area.

Copy link
Member

@lieut-data lieut-data left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@@ -110,13 +124,29 @@ func UpdateAssetsSubpathInDir(subpath, directory string) error {
newRootHTML = strings.Replace(newRootHTML, "</style>", fmt.Sprintf("</style><script>%s</script>", script), 1)
}

if newRootHTML == oldRootHTML {
mlog.Debug("No need to rewrite unmodified root.html", mlog.String("from_subpath", pathToReplace), mlog.String("to_subpath", newPath))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to just log nothing in this case? (Unix philosophy and all?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I didn't know that was part of the Unix philosophy 👀 But given this is a debug-level log, I'm in favour of keeping it. I don't think there's such thing as a lot of debug logging! If you have a stronger opinion, I can change it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm only 0/5, but if we DEBUG logged everything that /didn't/ happen, I would wager that would be a /lot/ of debug logging ;P. My hope whenever scrubbing logs is to look for positive evidence of something, and in that context, fewer logs that speak to something that actually happened are strictly more helpful. But this is a one-off, so I'm really only -1/5 :D

@hmhealey hmhealey added 4: Reviews Complete All reviewers have approved the pull request and removed 2: Dev Review Requires review by a developer labels May 23, 2024
@agarciamontoro agarciamontoro added 3: QA Review Requires review by a QA tester. May occur at the same time as Dev Review and removed 4: Reviews Complete All reviewers have approved the pull request labels May 24, 2024
@unified-ci-app
Copy link
Contributor

E2E test triggered successfully for PR #27076. The corresponding commit's status check will be available shortly.

Copy link

E2E test run is starting for commit 7ce20bcee6599533c597432973860e83c44bc385.
You can check its progress by either:

@agarciamontoro
Copy link
Member Author

@DHaussermann, this PR modifies the code that updates the static assets served by MM when changing the subpath where the application is served. Could you run a smoke test to ensure that everything is working as expected? If something is broken, it should be quite obvious, as in nothing works. We should ideally test that changing the subpath also works, but I don't know how easy that is (if it's even possible) in a cloud test server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3: QA Review Requires review by a QA tester. May occur at the same time as Dev Review release-note-none Denotes a PR that doesn't merit a release note.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

root.html updated even when not needed
5 participants