If you're running Facebook Ads and using Django with caching (like Memcached or Redis), you may notice that your pages are loading slowly when traffic comes from Facebook.
😕 The Problem: Facebook Adds Unique Parameters
Facebook automatically adds a unique query string to all outbound links:
https://yourdomain.com/page/?fbclid=XYZ123456789
This breaks caching because Django sees each URL as unique — so it doesn’t hit the cache, and loads the page from scratch every time.
✅ The Solution: Strip Tracking Parameters
You can write a custom Django middleware that removes fbclid
and other tracking parameters
before the request reaches your views or caching layer.
🧠Python 2 / Django 1.8 Middleware Example
For older projects using Python 2.7 and Django 1.8:
# realty/middleware/strip_tracking_params.py
from django.http import HttpResponseRedirect
from urlparse import urlparse, urlunparse, parse_qs
from urllib import urlencode
TRACKING_PARAMS = ['fbclid', 'utm_source', 'utm_medium', 'utm_campaign']
class StripTrackingParamsMiddleware(object):
def process_request(self, request):
parsed = urlparse(request.get_full_path())
query = parse_qs(parsed.query, keep_blank_values=True)
cleaned = {k: v for k, v in query.iteritems() if k not in TRACKING_PARAMS}
if len(cleaned) < len(query):
new_query = urlencode(cleaned, doseq=True)
new_url = urlunparse((
parsed.scheme,
parsed.netloc,
parsed.path,
parsed.params,
new_query,
parsed.fragment
))
return HttpResponseRedirect(new_url)
return None
settings.py:
MIDDLEWARE_CLASSES = [
'realty.middleware.strip_tracking_params.StripTrackingParamsMiddleware',
# other middleware...
]
🧠Python 3 / Modern Django Middleware Example
For Django 2.x or 3.x+ with Python 3:
# realty/middleware/strip_tracking_params.py
from django.http import HttpResponseRedirect
from urllib.parse import urlparse, urlunparse, parse_qs, urlencode
TRACKING_PARAMS = ['fbclid', 'utm_source', 'utm_medium', 'utm_campaign']
class StripTrackingParamsMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
parsed = urlparse(request.get_full_path())
query = parse_qs(parsed.query, keep_blank_values=True)
cleaned = {k: v for k, v in query.items() if k not in TRACKING_PARAMS}
if len(cleaned) < len(query):
new_query = urlencode(cleaned, doseq=True)
new_url = urlunparse(parsed._replace(query=new_query))
return HttpResponseRedirect(new_url)
return self.get_response(request)
settings.py:
MIDDLEWARE = [
'realty.middleware.strip_tracking_params.StripTrackingParamsMiddleware',
# other middleware...
]
🛠️ Don't Forget!
- Ensure
__init__.py
exists inrealty/
andrealty/middleware/
. - Restart your server after making changes.
- Make sure your app (e.g.
realty
) is listed inINSTALLED_APPS
.
📈 The Result
After stripping the tracking parameters, your app will properly serve cached pages to users coming from Facebook ads. This leads to faster loading times, less server load, and happier visitors.
Happy caching! 🚀
Quick Fix: Add a Loading Indicator for Old, Slow Websites Using jQuery
If you're maintaining an old website — maybe 10 years old, not earning anything, running on minimal resources, and still getting some traffic from Google — chances are you don’t have the time, money, or motivation to rebuild it from scratch. But if it’s still working, even slowly, it's worth keeping alive.
Sometimes the first page load can be slow (5+ seconds), especially without proper caching or optimization. A fast and cheap way to improve user experience is by simply showing a loading message or overlay when a user clicks something. It doesn’t speed things up, but it helps users understand that something is happening.
Add a Loading Overlay
How to Show a Loading Indicator on Click Using jQuery
If your website has a slow initial load due to caching or minimal resources, it's helpful to display a loading indicator when a user clicks a button or link. This can improve user experience and let them know that something is happening in the background.
Step 1: Add a Loading Indicator to Your HTML
Add this HTML snippet somewhere inside your <body>
tag. It will be used as a full-page overlay:
<div id="loading-indicator">Loading...</div>
Step 2: Add Some jQuery to Show/Hide It
This script shows the loading indicator whenever a link or button is clicked. You can place this script at the bottom of your HTML file, just before the closing </body>
tag:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function () {
$('a, button').on('click', function () {
$('#loading-indicator').show();
});
// Optional: show loading during any AJAX request
$(document).ajaxStart(function () {
$('#loading-indicator').show();
}).ajaxStop(function () {
$('#loading-indicator').hide();
});
});
</script>
Customization Tips
- You can replace the text
Loading...
with a spinner image or CSS animation. - If you only want this for certain elements, add a class like
.show-loading
and update your jQuery selector accordingly. - To prevent it from showing on external links, you can check the link’s
hostname
.
That's it! Now users will see a clear loading message when they click something, making your site feel faster and more responsive — especially during the first load.
I applied these fixes to the old website, and I think it's slowly starting to make a comeback. With a new idea behind it, it's been receiving more and more traffic and interest lately. It might even get a second life — there are people interested in sponsoring our work.
https://be.propenda.com/
https://propenda.be/
https://immo.estate/
Comments
Post a Comment