Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
3127ac91
Commit
3127ac91
authored
Jan 22, 2016
by
muhammad-ammar
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bookmark button js caching
TNL-3954
parent
97cf6dd4
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
119 additions
and
1 deletions
+119
-1
common/djangoapps/pipeline_mako/__init__.py
+63
-0
common/djangoapps/pipeline_mako/tests/test_render.py
+33
-0
lms/envs/common.py
+14
-1
lms/templates/courseware/courseware.html
+6
-0
lms/templates/main.html
+3
-0
No files found.
common/djangoapps/pipeline_mako/__init__.py
View file @
3127ac91
...
...
@@ -5,6 +5,9 @@ from pipeline.packager import Packager
from
pipeline.utils
import
guess_type
from
static_replace
import
try_staticfiles_lookup
from
django.conf
import
settings
as
django_settings
from
django.contrib.staticfiles.storage
import
staticfiles_storage
def
compressed_css
(
package_name
,
raw
=
False
):
package
=
settings
.
PIPELINE_CSS
.
get
(
package_name
,
{})
...
...
@@ -79,3 +82,63 @@ def render_individual_js(package, paths, templates=None):
if
templates
:
tags
.
append
(
render_inline_js
(
package
,
templates
))
return
'
\n
'
.
join
(
tags
)
def
render_require_js_path_overrides
(
path_overrides
):
# pylint: disable=invalid-name
"""Render JavaScript to override default RequireJS paths.
The Django pipeline appends a hash to JavaScript files,
so if the JS asset isn't included in the bundle for the page,
we need to tell RequireJS where to look.
For example:
"js/vendor/jquery.min.js" --> "js/vendor/jquery.min.abcd1234"
To achive this we will add overrided paths in requirejs config at runtime.
So that any reference to 'jquery' in a JavaScript module
will cause RequireJS to load '/static/js/vendor/jquery.min.abcd1234.js'
If running in DEBUG mode (as in devstack), the resolved JavaScript URLs
won't contain hashes, so the new paths will match the original paths.
Arguments:
path_overrides (dict): Mapping of RequireJS module names to
filesystem paths.
Returns:
unicode: The HTML of the <script> tag with the path overrides.
"""
# Render the <script> tag that overrides the paths
# Note: We don't use a Mako template to render this because Mako apparently
# acquires a lock when loading templates, which can lead to a deadlock if
# this function is called from within another template.
# The rendered <script> tag with overrides should be included *after*
# the application's RequireJS config, which defines a `require` object.
html
=
'''<script type="text/javascript">
(function (require) {{
require.config({{
paths: {{
{overrides}
}}
}});
}}).call(this, require || RequireJS.require);
</script>'''
new_paths
=
[]
for
url_path
in
path_overrides
:
# Calculate the full URL, including any hashes added to the filename by the pipeline.
# This will also include the base static URL (for example, "/static/") and the
# ".js" extension.
actual_url
=
staticfiles_storage
.
url
(
url_path
)
# RequireJS assumes that every file it tries to load has a ".js" extension, so
# we need to remove ".js" from the module path.
# RequireJS also already has a base URL set to the base static URL, so we can remove that.
path
=
actual_url
.
replace
(
'.js'
,
''
)
.
replace
(
django_settings
.
STATIC_URL
,
''
)
new_paths
.
append
(
"'{module}': '{path}'"
.
format
(
module
=
url_path
.
replace
(
'.js'
,
''
),
path
=
path
))
return
html
.
format
(
overrides
=
',
\n
'
.
join
(
new_paths
))
common/djangoapps/pipeline_mako/tests/test_render.py
0 → 100644
View file @
3127ac91
""" Tests for rendering functions in the mako pipeline. """
from
django.test
import
TestCase
from
pipeline_mako
import
render_require_js_path_overrides
class
RequireJSPathOverridesTest
(
TestCase
):
"""Test RequireJS path overrides. """
OVERRIDES
=
[
'js/vendor/jquery.min.js'
,
'js/vendor/backbone-min.js'
,
'js/vendor/text.js'
]
OVERRIDES_JS
=
[
"<script type=
\"
text/javascript
\"
>"
,
"(function (require) {"
,
"require.config({"
,
"paths: {"
,
"'js/vendor/jquery.min': 'js/vendor/jquery.min',"
,
"'js/vendor/backbone-min': 'js/vendor/backbone-min',"
,
"'js/vendor/text': 'js/vendor/text'"
,
"}"
,
"});"
,
"}).call(this, require || RequireJS.require);"
,
"</script>"
]
def
test_requirejs_path_overrides
(
self
):
result
=
render_require_js_path_overrides
(
self
.
OVERRIDES
)
# To make the string comparision easy remove the whitespaces
self
.
assertEqual
(
map
(
str
.
strip
,
result
.
splitlines
()),
self
.
OVERRIDES_JS
)
lms/envs/common.py
View file @
3127ac91
...
...
@@ -1610,7 +1610,20 @@ REQUIRE_EXCLUDE = ("build.txt",)
# and defines some "args" function that returns a list with the command arguments to execute.
REQUIRE_ENVIRONMENT
=
"node"
# In production, the Django pipeline appends a file hash to JavaScript file names.
# This makes it difficult for RequireJS to load its requirements, since module names
# specified in JavaScript code do not include the hash.
# For this reason, we calculate the actual path including the hash on the server
# when rendering the page. We then override the default paths provided to RequireJS
# so it can resolve the module name to the correct URL.
#
# If you want to load JavaScript dependencies using RequireJS
# but you don't want to include those dependencies in the JS bundle for the page,
# then you need to add the js urls in this list.
REQUIRE_JS_PATH_OVERRIDES
=
[
'js/bookmarks/views/bookmark_button.js'
,
'js/views/message_banner.js'
]
################################# CELERY ######################################
# Message configuration
...
...
lms/templates/courseware/courseware.html
View file @
3127ac91
...
...
@@ -5,6 +5,7 @@ from django.utils.translation import ugettext as _
from
django
.
template
.
defaultfilters
import
escapejs
from
django
.
conf
import
settings
from
edxnotes
.
helpers
import
is_feature_enabled
as
is_edxnotes_enabled
from
pipeline_mako
import
render_require_js_path_overrides
%
>
<
%
include_special_exams =
settings.FEATURES.get('ENABLE_SPECIAL_EXAMS',
False
)
and
(
course
.
enable_proctored_exams
or
course
.
enable_timed_exams
)
...
...
@@ -14,6 +15,11 @@ from edxnotes.helpers import is_feature_enabled as is_edxnotes_enabled
</
%
def>
<
%
block
name=
"bodyclass"
>
view-in-course view-courseware courseware ${course.css_class or ''}
</
%
block>
<
%
block
name=
"js_overrides"
>
${render_require_js_path_overrides(settings.REQUIRE_JS_PATH_OVERRIDES)}
</
%
block>
<
%
block
name=
"title"
><title>
% if section_title:
${static.get_page_title_breadcrumbs(section_title, course_name())}
...
...
lms/templates/main.html
View file @
3127ac91
...
...
@@ -86,6 +86,9 @@ from branding import api as branding_api
</script>
<script
type=
"text/javascript"
src=
"${static.url("
lms
/
js
/
require-config
.
js
")}"
></script>
<
%
block
name=
"js_overrides"
>
</
%
block>
% if not disable_courseware_js:
<
%
static:js
group=
'module-js'
/>
% endif
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment