Experimental brew load function#4069
Conversation
|
Also, it reduces the initial page load to just the app code, and consequently very large brews would at least present the HB UI framing earlier. Con: it would increase the number of requests hitting the server. Not quite double, but certainly more (app+brew+css, instead of appbrew+css). Once this is working well though we could get the app.js to be cached (since it only really changes when we push code updates to prod). |
|
I agree with the idea in general. It makes sense to separate the brew contents from the appstart contents. Given that we already have API paths for retrieving brews and things in homebrewapi.js, we should try to centralize any new brew fetching paths in there instead of in app.js. |
|
I've shifted the API get route to HomebrewAPI; to make it consistent with the other API routes I have added the |
|
Thinking over this some more, I have two thoughts:
|
I was initially hoping that it would be as simple as putting a "brew fetch" function in the common-to-everything React Router component, and then manipulating the data in each page as needed for that page's purpose.
We'll need to catch any error and throw to the error page; even SharePage should throw if the brew is locked. |
More specifically, I'm thinking about how we will need to add some mechanism to differentiate between when to redirect to an error page vs display a popup. For example, HBError 04 occurs when the user is not signed and attempts to edit an owned brew. If the user already had the edit page open, and somehow became signed out (cleared cookies, etc.), they will be presented with a popup when they try to save:
But if they are trying to open the edit page, they will instead be directed to an error page:
Both of these result from the same root error (attempt editor access of a brew without being signed in), but the result displayed depends on the error context (saving a brew you are already editing, vs opening the edit page of a brew). If all brew retrievals now happen after the page is loaded, we lose the context. We can probably bridge this by handling the error differently if we are fetching the brew from within |
|
A side thought on routing: we don't necessarily need new/additional routes beyond |
|
The latest commits add the ErrorPage component to the SharePage. If an error occurs while fetching the brew data, the error information is presented in the ErrorPage component instead of the normal SharePage content. As best I have been able to determine, this is identical to the current ErrorPage presented for the same error. |
|
Weird issue I haven't been able to hammer out yet - the first page is always rendered using the Legacy renderer, even if the document is set to V3. |
server/homebrew.api.js
Outdated
| // Remove Edit ID | ||
| req.brew.editId = undefined; | ||
| // Increase view count and lastViewed property | ||
| await HomebrewModel.increaseView({ shareId: req.params.id }); |
There was a problem hiding this comment.
Just thinking out loud:
This no longer handles the case to increase view count for unstubbed Google-only brews. Was this intentional? I suppose this makes sense; view count/lastViewed only really matters in the context of the HB environment itself, and is not required to "rebuild" the document if HB goes down. Now that we have stubs, Google files do not need to contain their own viewing statistics.
If GoogleActions.increaseView() is no longer being used anywhere, let's remove it or put a note somewhere to remove it later.
There was a problem hiding this comment.
Good point - I thought that this had already been changed, but on checking now I see that GoogleActions.increaseView is called in the current version of the SharePage. However, is the view count something that should be stored outside of the brew stub, now that they exist?
I'm tempted to just replicate the functionality and leave the philosophical questions for another day.
server/homebrew.api.js
Outdated
| // If logged in user is not an author: | ||
| if(!req.brew.authors.includes(req?.account?.username)){ | ||
| // Remove Edit ID | ||
| req.brew.editId = undefined; |
There was a problem hiding this comment.
This appears to be performing part of the sanitizeBrew() function (removing the editId), but not the other parts (removing _id and __v). Do we need to handle that here by bringing over sanitizeBrew(), or is it already covered somewhere in the fetching process?
There was a problem hiding this comment.
I've updated the function in the latest commit to also remove the _id and __v properties.
| return res.status(200).send(brew.style); | ||
| }, | ||
|
|
||
| getMeta : async (req, res, next)=>{ |
There was a problem hiding this comment.
I see what getMeta is doing, but I am concerned this basically means we have to request the brew twice when opening a page (once for the metadata and again inside the sharePage.jsx).
Not sure if there is a way to apply the metadata at the same time of the initial brew fetch from the sharePage, which would at least save a Mongo call.
But at a minimum, I think we can use await api.getBrew('share', true) so we only fetch the stub.
There was a problem hiding this comment.
I've kept this separate at this stage because I'm not sure whether the metadata information needs to be returned in the initial response in order for the meta tags to be picked up correctly for social media previews.
I think that you are likely correct about getBrew('share', true).
|
Let's block this until the unification of edit and new page is completed: #1800 |
|
While testing this today using this 584 page brew: https://homebrewery-pr-4069.herokuapp.com/share/R9uVUjwBDxeV
Further investigation will be required to determine the cause. |
This experimental PR demonstrates a process for loading brew data from
homebrew.jsxrather than fromapp.js. This prevents user data (like</script>) from appearing in the emitted HTML source (and potentially crashing the web app).As an example, the following is a brew that contains the title
TITLE, text content ofTEXT, and style content ofSTYLE- none of which appear in thestart_appfunction call:If this approach was applied to all routes, it would resolve #546.