-
Notifications
You must be signed in to change notification settings - Fork 2
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
serve react app on 40x errors #911
Conversation
352901a
to
deeafc5
Compare
main/views.py
Outdated
while preserving the erroring URL in the browser. | ||
""" | ||
html_content = Path(f"{settings.STATIC_ROOT}/mit-open/index.html").read_text() | ||
return HttpResponse(html_content) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a better way to serve a literally static HTML file? (I.e., not a template file). I could not find one.
Serving a purely static HTML file via django seems strange—we could just redirect—but I don't see any other way to preserve the URL (like http://localhost:8063/api/v1/cats-and-dogs).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any issue adding its path location to the Nginx config (as you suggested in the comments)? What's the content of the html file (to help me understand why we need to serve it at all)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HTML files are valid django templates - they just don't have and django template directives in them.
Since the files is under the static root directory, you'll probably need to add that directory to the templates directory configuration: https://docs.djangoproject.com/en/3.2/ref/settings/#dirs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given your other comment below, you probably want to put this directory first in the list and leave main/templates/index.html
as the failover for tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ended up keeping the touch ./frontends/mit-open/build/index.html
for tests. If the file is missing in prod, we probably want an error not a fallback.
The nginx error_page
approach discussed in slack seems appealing, but I couldn't get it to work. It just always showed the django error_page.
deeafc5
to
d92a0b9
Compare
d92a0b9
to
9864337
Compare
9864337
to
9849020
Compare
9849020
to
fb251ce
Compare
.github/workflows/ci.yml
Outdated
@@ -71,6 +71,8 @@ jobs: | |||
run: ./scripts/test/stub-data.sh | |||
- name: Tests | |||
run: | | |||
touch frontends/mit-open/build/index.html |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added this touch
for discussion.
One issue I ran into when serving the webpack build in handle_40x
handlers without using a Django template is that the file won't exist without running the webpack build. This causes one of our tests to fail:
test_initials_avatar_fake_user
callsname_initials_avatar_view
...- which calls
redirect("static/images/avatar_default.png")
... - which for some reason triggers
handle_404
(I don't understand whyredirect
triggers that). - handle_404 wants the webpack built html file to exist, which then errors with 500 because it does not exist.
If you want to see this locally:
docker compose exec web bash
rm -rf staticfiles
pytest profiles/views_test.py --no-cov -k test_initials_avatar_fake_user -s
I added this touch
to illustrate the issue. Doesn't seem like a good solution.
fb251ce
to
8f0b7d0
Compare
8f0b7d0
to
df44b05
Compare
df44b05
to
c1da300
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rhysyngsun I think you just need to rebuild the frontend. |
c1da300
to
05506dc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM once tests pass
What are the relevant tickets?
Description (What does it do?)
This PR fixes and issue with our 40x handling that was introduced as part of #678:
handle_40x
handlers were still serving the oldindex.html
template, but this template file would error because of missing webpack stats.handle_40x
got triggered.handle_40x
itself would error, raising 500.How can this be tested?
DEBUG=False
docker compose up
http://localhost:8063/api/cats-and-dogs
. This triggershandle_404
, which should send you to the frontend "Not Found" page.