fix: JS null error linked to newsletter form #962
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This fixes the poltergeist bug!
Initial problem
The newsletter form included a hidden label which was populated with JS when the website loaded. It was populated with the URL of the page the user was currently on. Since this hidden label was in the form, it was included in the POST request and the Django form then used the URL from the POST data to be able to redirect the user to the same page upon submission of the form. This was most likely because the newsletter form is in the footer (therefore, the base HTML template) and it wasn't straightforward to know how to redirect the user to the same page. More details can be found about how this was implemented by looking at PR #716.
The was the JS populated the hidden label sometimes caused a null error. We think that this happened sometimes depending on loading speed, or maybe overall traffic on the website. In general, it seemed like if or when the webpage loaded slowly, the HTML element hadn't loaded properly and the JS wasn't able to find it, causing a null error.
Because the JS raised an error at that point in the
{% scripts %}
block, none of the following JS could be executed. This included the other JS files such as the level selection logic (explains why the white squares would show next to the episode names), the scoreboard logic (explains why the scoreboard would load without any borders, background colours, etc...) and the level moderation logic (explains why the student dropdown list would not load, populate or work properly).Solution
This PR makes the newsletter form redirect automatically to the home page, instead of the current page the user was on. This means the faulty JS and the hidden label could be removed.
This is also a fix that could later be further improved by actually figuring out how to properly redirect to the same page in Django, however this proved to be more challenging than expected, and the urgency of fixing this production bug pushed me towards this solution.