Skip to content

Commit

Permalink
defering possible js and css files (#2072)
Browse files Browse the repository at this point in the history
  • Loading branch information
HamzaIbnFarooq authored Jan 21, 2021
1 parent 8412a03 commit 3aaef79
Show file tree
Hide file tree
Showing 17 changed files with 231 additions and 202 deletions.
2 changes: 1 addition & 1 deletion cms/templates/home_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

{% block extrahead %}
{{ block.super }}
<link href="{% static 'css/vendor/jquery.fancybox.min.css'%}" rel="stylesheet">
<link href="{% static 'css/vendor/jquery.fancybox.min.css'%}" rel="stylesheet" media="none" onload="if(media!='all')media='all'">
{% endblock %}

{% block content %}
Expand Down
18 changes: 10 additions & 8 deletions courses/templates/subscription.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ <h1>WANT TO LEARN ABOUT NEW COURSES FROM {{ site_name }}?</h1>
<!--[if lte IE 8]>
<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2-legacy.js"></script>
<![endif]-->
<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2.js"></script>
<script>
if (typeof hbspt !== 'undefined') {
hbspt.forms.create({
portalId: "{{ hubspot_portal_id | safe }}",
formId: "{{ hubspot_new_courses_form_guid | safe }}"
});
}
<script defer charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2.js"></script>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function() {
if (typeof hbspt !== 'undefined') {
hbspt.forms.create({
portalId: "{{ hubspot_portal_id | safe }}",
formId: "{{ hubspot_new_courses_form_guid | safe }}"
});
}
});
</script>
</div>
</div>
1 change: 0 additions & 1 deletion mail/templates/email_debugger.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
error.innerHTML = res.error;
}
});

});
});
});
Expand Down
26 changes: 13 additions & 13 deletions mitxpro/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
{% endblock %}
{% endspaceless %}
{% if zendesk_config.help_widget_enabled and not request.path|startswith:'/certificate/' %}
<script id="ze-snippet" src="https://static.zdassets.com/ekr/snippet.js?key={{zendesk_config.help_widget_key}}"> </script>
<script defer id="ze-snippet" src="https://static.zdassets.com/ekr/snippet.js?key={{zendesk_config.help_widget_key}}"> </script>
{% endif %}
</head>
<body class="{% block bodyclass %}{% endblock %}">
Expand All @@ -58,22 +58,22 @@
{% block footer %}
{% include "footer.html" %}
{% endblock %}
{% render_bundle 'third_party' %}
{% render_bundle 'django' %}
{% render_bundle 'third_party' added_attrs='defer' %}
{% render_bundle 'django' added_attrs='defer' %}
{% block scripts %}
{% endblock %}
{% if zendesk_config.help_widget_enabled and not request.path|startswith:'/certificate/' %}
<script type="text/javascript">
$(function() {
if (typeof zE !== 'undefined') {
zE('webWidget', 'prefill', {
name: {
value: '{{user.name}}',
},
email: {
value: '{{user.email}}',
}
});
document.addEventListener('DOMContentLoaded', function() {
if (typeof zE !== 'undefined') {
zE('webWidget', 'prefill', {
name: {
value: '{{user.name}}',
},
email: {
value: '{{user.email}}',
}
});
}
});
</script>
Expand Down
16 changes: 10 additions & 6 deletions mitxpro/templates/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,26 @@ <h2>Contact Us</h2>
<li><a href="/about-us/">About Us</a></li>
</ul>
<script
defer
charset="utf-8"
type="text/javascript"
src="//js.hsforms.net/forms/v2-legacy.js"
></script>
<script
defer
charset="utf-8"
type="text/javascript"
src="//js.hsforms.net/forms/v2.js"
></script>
<script>
if (typeof hbspt !== "undefined") {
hbspt.forms.create({
portalId: "{{ hubspot_portal_id }}",
formId: "{{ hubspot_footer_form_guid }}",
});
}
document.addEventListener('DOMContentLoaded', function() {
if (typeof hbspt !== "undefined") {
hbspt.forms.create({
portalId: "{{ hubspot_portal_id }}",
formId: "{{ hubspot_footer_form_guid }}",
});
}
});
</script>
</div>
</div>
Expand Down
16 changes: 9 additions & 7 deletions mitxpro/templatetags/render_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def _get_bundle(request, bundle_name):


@register.simple_tag(takes_context=True)
def render_bundle(context, bundle_name):
def render_bundle(context, bundle_name, added_attrs=""):
"""
Render the script tags for a Webpack bundle
Expand All @@ -62,25 +62,27 @@ def render_bundle(context, bundle_name):
Args:
context (dict): The context for rendering the template (includes request)
bundle_name (str): The name of the bundle to render
added_attrs (str): Optional string of HTML attributes to add to the script/link tag
Returns:
django.utils.safestring.SafeText:
"""
try:
bundle = _get_bundle(context["request"], bundle_name)
return _render_tags(bundle)
return _render_tags(bundle, added_attrs)
except OSError:
# webpack-stats.json doesn't exist
return mark_safe("")


def _render_tags(bundle):
def _render_tags(bundle, added_attrs=""):
"""
Outputs tags for template rendering.
Adapted from webpack_loader.utils.get_as_tags and webpack_loader.templatetags.webpack_loader.
Args:
bundle (iterable of dict): The information about a webpack bundle
added_attrs (str): Optional string of HTML attributes to add to the script/link tag
Returns:
django.utils.safestring.SafeText: HTML for rendering bundles
Expand All @@ -90,14 +92,14 @@ def _render_tags(bundle):
for chunk in bundle:
if chunk["name"].endswith((".js", ".js.gz")):
tags.append(
('<script type="text/javascript" src="{}" ></script>').format(
chunk["url"]
('<script type="text/javascript" src="{}" {} ></script>').format(
chunk["url"], added_attrs
)
)
elif chunk["name"].endswith((".css", ".css.gz")):
tags.append(
('<link type="text/css" href="{}" rel="stylesheet" />').format(
chunk["url"]
('<link type="text/css" href="{}" rel="stylesheet" {} />').format(
chunk["url"], added_attrs
)
)
return mark_safe("\n".join(tags))
4 changes: 2 additions & 2 deletions mitxpro/tests/render_bundle_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_debug(settings, mocker):
"mitxpro.templatetags.render_bundle.get_loader", return_value=loader
)
assert render_bundle(context, bundle_name) == (
'<script type="text/javascript" src="{base}/{filename}" >'
'<script type="text/javascript" src="{base}/{filename}" >'
"</script>".format(
base=webpack_dev_server_url(request), filename=FAKE_COMMON_BUNDLE[0]["name"]
)
Expand Down Expand Up @@ -69,7 +69,7 @@ def test_production(settings, mocker):
"mitxpro.templatetags.render_bundle.get_loader", return_value=loader
)
assert render_bundle(context, bundle_name) == (
'<script type="text/javascript" src="{base}/{filename}" >'
'<script type="text/javascript" src="{base}/{filename}" >'
"</script>".format(
base="/static/bundles", filename=FAKE_COMMON_BUNDLE[0]["name"]
)
Expand Down
71 changes: 37 additions & 34 deletions static/js/courseware_carousel.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
/*eslint-env jquery*/
/*eslint semi: ["error", "always"]*/
$(".course-slider").slick({
rows: 0,
slidesToShow: 3,
slidesToScroll: 1,
dots: false,
infinite: false,
autoplay: false,
responsive: [
{
breakpoint: 1024,
settings: {
slidesToShow: 3,
slidesToScroll: 3
}
},
{
breakpoint: 992,
settings: {
slidesToShow: 2,
slidesToScroll: 1
}
},
{
breakpoint: 767,
settings: {
slidesToShow: 1,
slidesToScroll: 1

export default function coursewareCarousel() {
$(".course-slider").slick({
rows: 0,
slidesToShow: 3,
slidesToScroll: 1,
dots: false,
infinite: false,
autoplay: false,
responsive: [
{
breakpoint: 1024,
settings: {
slidesToShow: 3,
slidesToScroll: 3
}
},
{
breakpoint: 992,
settings: {
slidesToShow: 2,
slidesToScroll: 1
}
},
{
breakpoint: 767,
settings: {
slidesToShow: 1,
slidesToScroll: 1
}
}
}
]
});
]
});

$(".course-slider .slide").on("click", function() {
const targetUrl = $(this).data("url");
window.location.href = targetUrl;
});
$(".course-slider .slide").on("click", function() {
const targetUrl = $(this).data("url");
window.location.href = targetUrl;
});
}
27 changes: 19 additions & 8 deletions static/js/entry/django.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import "../notifications.js"
import "../tooltip.js"
import "../hero.js"
import "../testimonials_carousel.js"
import "../courseware_carousel.js"
import "../text_video_section.js"
import "../image_carousel.js"
import "../faculty_carousel.js"
import notifications from "../notifications.js"
import tooltip from "../tooltip.js"
import hero from "../hero.js"
import testimonialsCarousel from "../testimonials_carousel.js"
import coursewareCarousel from "../courseware_carousel.js"
import textVideoSection from "../text_video_section.js"
import imageCarousel from "../image_carousel.js"
import facultyCarousel from "../faculty_carousel.js"

document.addEventListener("DOMContentLoaded", function() {
notifications()
tooltip()
hero()
testimonialsCarousel()
coursewareCarousel()
textVideoSection()
imageCarousel()
facultyCarousel()
})
2 changes: 1 addition & 1 deletion static/js/entry/third-party.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "jquery"
import "popper.js"
import "bootstrap"
import "popper.js"
import "slick-carousel"
import "hls.js"
import "@fancyapps/fancybox"
71 changes: 37 additions & 34 deletions static/js/faculty_carousel.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
/*eslint-env jquery*/
/*eslint semi: ["error", "always"]*/
const numFacultySlides = $(".faculty-slider .slide").length;

$(".faculty-slider").slick({
rows: 0,
slidesToShow: 3,
slidesToScroll: 1,
dots: numFacultySlides > 3,
infinite: false,
autoplay: false,
responsive: [
{
breakpoint: 1024,
settings: {
slidesToShow: 3,
slidesToScroll: 3,
dots: numFacultySlides > 3
}
},
{
breakpoint: 992,
settings: {
slidesToShow: 2,
slidesToScroll: 1,
dots: numFacultySlides > 2
}
},
{
breakpoint: 767,
settings: {
slidesToShow: 1,
slidesToScroll: 1,
dots: numFacultySlides > 1
export default function facultyCarousel() {
const numFacultySlides = $(".faculty-slider .slide").length;

$(".faculty-slider").slick({
rows: 0,
slidesToShow: 3,
slidesToScroll: 1,
dots: numFacultySlides > 3,
infinite: false,
autoplay: false,
responsive: [
{
breakpoint: 1024,
settings: {
slidesToShow: 3,
slidesToScroll: 3,
dots: numFacultySlides > 3
}
},
{
breakpoint: 992,
settings: {
slidesToShow: 2,
slidesToScroll: 1,
dots: numFacultySlides > 2
}
},
{
breakpoint: 767,
settings: {
slidesToShow: 1,
slidesToScroll: 1,
dots: numFacultySlides > 1
}
}
}
]
});
]
});
}
Loading

0 comments on commit 3aaef79

Please sign in to comment.