การจัดลำดับความสำคัญของเนื้อหาเพื่อความเร็ว
ใช้เทคนิคต่าง ๆ เช่น `async`, `defer` และคำแนะนำทรัพยากร เพื่อจัดลำดับความสำคัญของเนื้อหาสำคัญและปรับปรุงประสิทธิภาพที่ผู้ใช้รับรู้
การจัดลำดับความสำคัญของเนื้อหาเพื่อความเร็ว เป็นบทเรียน Web Performance Optimization & Lighthouse ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Web Performance Optimization & Lighthouse และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Web Performance Optimization & Lighthouse มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Prioritize Content?
When a browser loads a webpage, it follows a sequence of steps to render content. This is often called the Critical Rendering Path (CRP).
Optimizing the CRP means delivering the most important content to the user as quickly as possible. This improves perceived performance – how fast a user *feels* the page is loading.
Blocking Scripts Slow Down
By default, when the browser encounters a <script> tag, it pauses parsing the HTML, fetches the script, executes it, and then resumes parsing. This is called a render-blocking script.
This can significantly delay how quickly users see content, especially if the script is large or on a slow network.
<html>
<head>
<title>Blocking Script</title>
<script>
// This script blocks HTML parsing
console.log("Blocking script started.");
// Simulate a long task
let start = performance.now();
while (performance.now() - start < 700) { /* busy wait */ }
console.log("Blocking script finished.");
</script>
</head>
<body>
<h1>Page Content</h1>
<p>This content appears <b>after</b> the script finishes.</p>
</body>
</html>Introducing `defer`
The defer attribute tells the browser to download the script in the background without blocking HTML parsing. Once the HTML is fully parsed, the deferred scripts execute in the order they appear in the document.
- Non-blocking: HTML parsing continues.
- Order preserved: Scripts execute in source order.
- Execution time: After HTML parsing, before
DOMContentLoaded.
`defer` in Action
Using defer is ideal for scripts that depend on the DOM being fully available, like analytics or UI initialization logic.
See how the 'Page Content' appears before the deferred script executes in the console.
<html>
<head>
<title>Deferred Script Example</title>
<script defer>
console.log("Deferred script 1 started.");
</script>
<script defer>
// This script will run after script 1
console.log("Deferred script 2 started.");
</script>
</head>
<body>
<h1>Page Content</h1>
<p>This content appears immediately while scripts are downloading.</p>
<script>
console.log("HTML parsing complete.");
</script>
</body>
</html>Introducing `async`
The async attribute also tells the browser to download the script in the background. However, async scripts execute as soon as they are downloaded, potentially before HTML parsing is complete.
- Non-blocking: HTML parsing continues.
- Order NOT preserved: Scripts execute as soon as they're ready.
- Execution time: Asynchronously, as soon as possible.
`async` vs. `defer`
Choosing between async and defer depends on your script's needs:
- Use
asyncfor independent scripts (e.g., third-party ads, tracking) where execution order doesn't matter. - Use
deferfor scripts that need the DOM or depend on other deferred scripts, ensuring order is maintained.
<html>
<head>
<title>Async vs Defer</title>
<script async>
console.log("Async script executed!");
</script>
<script defer>
console.log("Defer script executed!");
</script>
<script>
console.log("HTML parsing continues, then inline script runs.");
</script>
</head>
<body>
<h1>Async & Defer Demo</h1>
<p>Check the console to see the execution order.</p>
</body>
</html>Resource Hint: `preconnect`
Resource hints give the browser clues about resources it will need soon, allowing it to start fetching them earlier.
<link rel="preconnect"> tells the browser to establish a connection to another domain early. This includes DNS lookup, TCP handshake, and TLS negotiation, saving precious milliseconds for critical third-party resources.
Resource Hint: `preload`
<link rel="preload"> tells the browser to fetch a resource that is critically important for the current navigation as soon as possible. This is useful for resources like fonts, critical CSS, or images that are discovered late by the parser.
<html>
<head>
<title>Preload Example</title>
<link rel="preload" href="data:text/css;base64,Ym9keSB7IGJhY2tncm91bmQtY29sb3I6IGxpZ2h0Ymx1ZTsgfQ==" as="style">
<style>
/* This is a simple inline style */
body { margin: 20px; }
</style>
<style>
/* The preloaded style will apply */
@import url("data:text/css;base64,Ym9keSB7IGJhY2tncm91bmQtY29sb3I6IGxpZ2h0Ymx1ZTsgfQ==");
</style>
</head>
<body>
<h1>Preloaded Style</h1>
<p>The CSS for the light blue background is preloaded, ensuring it's available early.</p>
</body>
</html>Resource Hint: `prefetch`
<link rel="prefetch"> tells the browser to fetch a resource that might be needed for a future navigation. It's a lower priority hint than preload and is great for speeding up subsequent page loads.
For example, if you know a user is likely to click a specific link, you can prefetch the resources for that page.
Prioritization Quiz
Which of the following statements about script loading and resource hints are TRUE?
Recap: Faster Page Loads
We've learned powerful techniques to prioritize content and speed up your web pages:
- `defer` for non-blocking script loading, preserving execution order.
- `async` for non-blocking, independent script execution.
- `preconnect` to warm up connections to other domains.
- `preload` to fetch critical resources early for the current page.
- `prefetch` to fetch resources for future navigations.
By strategically using these, you can significantly improve your website's perceived performance and user experience!
คำถามที่พบบ่อย
บทเรียน “การจัดลำดับความสำคัญของเนื้อหาเพื่อความเร็ว” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การจัดลำดับความสำคัญของเนื้อหาเพื่อความเร็ว” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web Performance Optimization & Lighthouse ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web Performance Optimization & Lighthouse มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การจัดลำดับความสำคัญของเนื้อหาเพื่อความเร็ว”
ใช้เทคนิคต่าง ๆ เช่น `async`, `defer` และคำแนะนำทรัพยากร เพื่อจัดลำดับความสำคัญของเนื้อหาสำคัญและปรับปรุงประสิทธิภาพที่ผู้ใช้รับรู้ คุณปฏิบัติ Web Performance Optimization & Lighthouse ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web Performance Optimization & Lighthouse หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web Performance Optimization & Lighthouse บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การจัดลำดับความสำคัญของเนื้อหาเพื่อความเร็ว” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Web Performance Optimization & Lighthouse นี้ได้ไหม
ได้ บทเรียน Web Performance Optimization & Lighthouse ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ทำความเข้าใจเส้นทางวิกฤติ
- ทรัพยากรที่บล็อกการแสดงผล
- การจัดลำดับความสำคัญของเนื้อหาเพื่อความเร็ว
- การโหลดล่วงหน้าและคำแนะนำทรัพยากร