Skip to content

Commit

Permalink
fix: fix hot reload serving of static resources (#1980)
Browse files Browse the repository at this point in the history
* fix: fixes loading of static files from dev server

* test: add test for load static base URL override

* chore: bump ORA version to 5.0.4
  • Loading branch information
nsprenkle authored Jun 21, 2023
1 parent 0f8b0f9 commit a397361
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion openassessment/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
Initialization Information for Open Assessment Module
"""
__version__ = '5.0.3'
__version__ = '5.0.4'
9 changes: 8 additions & 1 deletion openassessment/xblock/load_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
import json
import logging
from urllib.parse import urlparse

from pkg_resources import resource_string
from django.conf import settings
Expand Down Expand Up @@ -36,6 +37,8 @@ def reload_manifest():
Reload from manifest file
"""
root_url, base_url = '', '/static/dist/'
base_url_override = ''

if hasattr(settings, 'LMS_ROOT_URL'):
root_url = settings.LMS_ROOT_URL
else:
Expand All @@ -44,11 +47,15 @@ def reload_manifest():
try:
json_data = resource_string(__name__, 'static/dist/manifest.json').decode("utf8")
LoadStatic._manifest = json.loads(json_data)
base_url_override = LoadStatic._manifest.get('base_url', None)
LoadStatic._is_loaded = True
except OSError:
logger.error('Cannot find static/dist/manifest.json')
finally:
LoadStatic._base_url = urljoin(root_url, base_url)
if base_url_override and urlparse(base_url_override).scheme:
LoadStatic._base_url = base_url_override
else:
LoadStatic._base_url = urljoin(root_url, base_url)

@staticmethod
def get_url(key):
Expand Down
17 changes: 17 additions & 0 deletions openassessment/xblock/test/test_load_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ def test_get_url_default(self):
self.assertEqual(LoadStatic.get_url(key_url),
urljoin(self.default_base_url, key_url))

def test_get_url_absolute_base_url(self):
# Given my base_url is an absolute URL and is overridden (as in hot-reload
# local dev setup)
key_url = 'some_url.js'
absolute_base_url = 'foo://bar'
loaded_key_url = '/some_url.hashchunk.js'

# When I get a static URL
with patch('json.loads') as jsondata:
jsondata.return_value = {
'base_url': absolute_base_url,
'some_url.js': loaded_key_url,
}
# Then I use the base URL instead of default LMS URL
self.assertEqual(LoadStatic.get_url(key_url), urljoin(
absolute_base_url, loaded_key_url))

@patch('pkg_resources.resource_string')
def test_get_url_file_not_found(self, resource_string):
key_url = 'some_url.js'
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "edx-ora2",
"version": "5.0.3",
"version": "5.0.4",
"repository": "https://github.com/openedx/edx-ora2.git",
"dependencies": {
"@edx/frontend-build": "^6.1.1",
Expand Down

0 comments on commit a397361

Please sign in to comment.