منع التخزين المؤقت بإصدارات الملفات
أجبر المتصفحات على جلب الأصول المحدّثة.
منع التخزين المؤقت بإصدارات الملفات درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flask Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flask Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Browsers Love to Cache
Browsers save assets to load pages faster. That is great until you ship a new style.css and visitors keep seeing the cached old one. 🗂️
The Stale Asset Problem
If the URL stays /static/style.css forever, the browser assumes nothing changed. Your fresh design simply never reaches the user.
Change the URL to Refetch
The fix is cache busting: change the asset URL whenever the file changes. A new URL forces the browser to download again.
A Version Query String
The simplest trick adds a version query parameter. /static/style.css?v=2 is treated as a new resource by the browser.
<link href="{{ url_for('static', filename='style.css') }}?v=2">Bump It on Each Release
Each time you deploy changed CSS, raise the v number. A central variable beats editing the value in many templates by hand.
Use the File's mtime
A smarter bust uses the file modification time. Read getmtime so the version changes only when the file actually does.
import os
v = int(os.path.getmtime("static/style.css"))Let url_for Carry Extras
Any unknown keyword you pass url_for becomes a query parameter, so you can attach a computed version cleanly.
url_for("static", filename="style.css", v=v)
# -> /static/style.css?v=1718000000Content Hashes Are Best
The strongest approach names files by a hash of their contents, like style.abc123.css. New contents mean a new name and a guaranteed refetch.
Hashes Enable Long Caching
Because a hashed name only changes when contents change, you can cache it almost forever. Speed and freshness stop fighting each other.
Build Tools Do the Work
Asset bundlers generate hashed filenames and a manifest mapping logical names to hashed ones, so your templates ask for the current build.
Pick the Right Tier
Start with a simple ?v= version for small projects. Move to content hashes once a build pipeline is worth the setup.
Quick Check
Why does adding ?v=2 to an asset URL make the browser fetch a fresh copy?
Recap: Cache Busting
You learned to change asset URLs so browsers refetch updates: a version query for small apps, content hashes for serious builds. 🚀
الأسئلة الشائعة
هل درس «منع التخزين المؤقت بإصدارات الملفات» مجاني؟
نعم — نص درس «منع التخزين المؤقت بإصدارات الملفات» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.
ماذا ستتعلم في «منع التخزين المؤقت بإصدارات الملفات»؟
أجبر المتصفحات على جلب الأصول المحدّثة. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟
لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «منع التخزين المؤقت بإصدارات الملفات»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟
نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- اصطلاح مجلد static
- ربط الأصول باستخدام url_for static
- منع التخزين المؤقت بإصدارات الملفات
- الملفات الثابتة في بيئة الإنتاج