Improving page speed score in Google Page Score test- PART2

Reading Time : ~ .

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.

    By Posted On
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Using Django's built in signals and writing custom signals. Divya Sri

Django has a beautiful feature of signals which will record all the actions performed on particular model. In the current blog post we’ll learn how ...

Continue Reading...
Sendgrid Inbound Email Parsing with django Nikhila Mergu

Using the Inbound parse webhook, we can parse the contents, attachments of an incoming email.

Inbound Parse API follows 3 steps:
1. sending an ...

Continue Reading...
How to create Periodic Tasks in Celery? Nikhila Mergu

Periodic Task is a scheduling task which will run at regular intervals of time. Celery is a powerful, production-ready asynchronous job queue, which allows you ...

Continue Reading...

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties