Improving page speed score in Google Page Score test- PART2

In our previous blog post,  we got an idea of what are the things that Google's Page Speed Insights will take into the count to give page score, now in the present  blog post we'll get to know what are the techniques we can use to improve the page score.

1. Reducing the Server response time:

As a programmer, one of the reasons of the high server response time is due to redundancy in the  queries. The following the cases where we use redundancy of queries and how can we reduce them.

  1. We use .exists() method, while querying to check if the DB object exists or not. Suppose we are having a model called 'Assessment'. The following is a code snippet with the wrong usage of  .exists().
    if Assessment.objects.filter(id=assess.assessment_id, edu_class_id=role.edu_class_id, subject_id=role.subject_id).exists():
         assessments_list = assessments_list + list(Assessment.objects.filter(id=assess.assessment_id,edu_class_id=role.edu_class_id,subject_id=role.subject_id))
    In the above code the query 'Assessment.objects.filter(id=assess.assessment_id, edu_class_id=role.edu_class_id, subject_id=role.subject_id)' is repeated twice. Using the same query second time is of no use. The method .exists(), is to be used in such cases where the query set result is not used anywhere else. We can rewrite the above code as below.
    assessments = Assessment.objects.filter(id=assess.assessment_id, edu_class_id=role.edu_class_id, subject_id=role.subject_id)
    if assessments:
         assessments_list = assessments_list + list(assessments)
    
  2. Similarly, we can use two querying methods prefectch_related() and select_related() to reduce redundancy of queries. For more about prefectch_related() and select_related(), check https://docs.djangoproject.com/en/1.8/ref/models/querysets/
  3. We can use caching the query set data, to not query for the DB again and again if there is a large amount of data. For caching we can use Django-Memcached. For further details of Django-Memcached check https://docs.djangoproject.com/en/1.8/topics/cache/
  4. For further compression, we can enable gzip compression in the  servers.

2. Enable compression and minifying  CSS and JS:

Many of the projects which are developed in Django use a plugin called 'Django-Compressor' to compress the CSS and JS resources. For more information about Django-Compressor check http://django-compressor.readthedocs.org/en/latest/. By default, django compressor won't minify the CSS and JS files completely. To minify CSS and  JS resources we have to keep the following settings in settings.py

COMPRESS_CSS_FILTERS = ['compressor.filters.css_default.CssAbsoluteFilter','compressor.filters.cssmin.CSSMinFilter']
COMPRESS_JS_FILTERS = ['compressor.filters.jsmin.JSMinFilter']

3. Minify HTML:

Django supports multiple HTML minification techniques, two of them are Django-htmlmin and Django-hmin. Both will minify the HTML data that is going  to be loaded on the page. For more about Django-htmlmin check https://pypi.python.org/pypi/django-htmlmin and for django-hmin check https://pypi.python.org/pypi/django-hmin/0.3.2

4. Leverage browser caching:

Setting an expiry date or a maximum age in the HTTP headers for static resources instructs the browser to load previously downloaded resources from local disk rather than over the network. If we are landing the static files using Nginx the need the configure the expires value in the Nginx configuration file. This will look as following.

expires 365d;

If we use Django-storages we have to keep the following settings in your settings.py.

AWS_HEADERS = {
    'Expires': 'Thu, 15 Apr 2010 20:00:00 GMT',
    'Cache-Control': 'max-age=86400',
}

This setting sets an expiry date for the  resources those are being served from AmazonS3.

By following the above techniques we can render our page more quickly, such that it will increase the page score in the google page speed insights.

Posted On 30 July 2015 By MicroPyramid


Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Django Template language Intro

We can write the required logics based on programming[python] syntax in models and views but, when we want to write simple logics we should follow ...

Continue Reading...
Introduction to Django's Class based views - Understanding how a class based view works

Django has MVT architecture. A view in django is just a callable that takes a request and returns a response. But this can be more ...

Continue Reading...
Integration Of GitHub API with python django

Using Github integration, we can get the user verified email id, general information, git hub URL, id, disk usage, public, private repo's, gists and followers, ...

Continue Reading...
open source packages

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties