การบีบอัดและการเข้ารหัสการส่งข้อมูล
ลดขนาดข้อมูลที่ส่งผ่านเครือข่ายด้วยการบีบอัด gzip และ Brotli ประเภทเนื้อหาที่เหมาะสม และการบีบอัดล่วงหน้า เพื่อให้การตอบสนองมาถึงเร็วขึ้นบนการเชื่อมต่อทุกประเภท
การบีบอัดและการเข้ารหัสการส่งข้อมูล เป็นบทเรียน Web Performance Optimization & Lighthouse ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Web Performance Optimization & Lighthouse และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Web Performance Optimization & Lighthouse มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Compress?
Text assets like HTML, CSS, JS, JSON, and SVG are highly compressible. Compressing them before transfer can cut payloads by 70-90%, directly speeding up load on every network.
How Negotiation Works
The browser advertises supported algorithms with the Accept-Encoding header; the server responds with the chosen one in Content-Encoding.
Accept-Encoding: br, gzip
Content-Encoding: brgzip
gzip is universally supported and a safe baseline. It offers good compression with low CPU cost and is enabled on virtually every server and CDN.
Brotli
Brotli (br) typically compresses text 15-25% smaller than gzip. It is supported in all modern browsers over HTTPS, making it the preferred choice today.
Compress the Right Types
Compress text-based MIME types only. Already-compressed binaries like JPEG, PNG, WebP, MP4, and woff2 fonts gain nothing and waste CPU if recompressed.
Static Precompression
For static assets, precompress at build time at the highest level. The server then serves the .br or .gz file directly, avoiding per-request CPU.
# nginx serving precompressed files
brotli_static on;
gzip_static on;Dynamic Compression Levels
For dynamic responses, use a lower compression level so CPU cost does not delay the response. High levels are best reserved for static, build-time compression.
Express Example
In Node, the compression middleware adds gzip/Brotli to dynamic responses with sensible defaults.
const compression = require('compression');
app.use(compression());Verifying Compression
Inspect a response in the Network panel: check Content-Encoding: br or gzip and compare transferred size to resource size. Lighthouse flags uncompressed text assets.
Compression and HTTPS
Modern compression for sensitive content is served over HTTPS, which also avoids historic attacks. Brotli in browsers is only offered on secure connections.
Strategy Summary
- Prefer Brotli, fall back to gzip.
- Compress text types only.
- Precompress static assets at max level.
- Use lower levels for dynamic responses.
Quick Check
You enable maximum-level compression on dynamically generated API responses and notice slower responses under load. What is the better approach?
Recap
You learned to shrink transfers with gzip and Brotli, negotiated via Accept-Encoding. Compress text types only, precompress static assets at max level, use lower levels for dynamic responses, and verify with the Network panel.
คำถามที่พบบ่อย
บทเรียน “การบีบอัดและการเข้ารหัสการส่งข้อมูล” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การบีบอัดและการเข้ารหัสการส่งข้อมูล” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web Performance Optimization & Lighthouse ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web Performance Optimization & Lighthouse มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การบีบอัดและการเข้ารหัสการส่งข้อมูล”
ลดขนาดข้อมูลที่ส่งผ่านเครือข่ายด้วยการบีบอัด gzip และ Brotli ประเภทเนื้อหาที่เหมาะสม และการบีบอัดล่วงหน้า เพื่อให้การตอบสนองมาถึงเร็วขึ้นบนการเชื่อมต่อทุกประเภท คุณปฏิบัติ Web Performance Optimization & Lighthouse ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web Performance Optimization & Lighthouse หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web Performance Optimization & Lighthouse บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การบีบอัดและการเข้ารหัสการส่งข้อมูล” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Web Performance Optimization & Lighthouse นี้ได้ไหม
ได้ บทเรียน Web Performance Optimization & Lighthouse ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- พื้นฐาน HTTP/2 และ HTTP/3
- การแคชบนเบราว์เซอร์และเซิร์ฟเวอร์
- เครือข่ายส่งมอบเนื้อหา (CDN)
- การบีบอัดและการเข้ารหัสการส่งข้อมูล