[null,null,["最后更新时间 (UTC):2025-07-25。"],[[["\u003cp\u003eThis document is outdated; refer to Version 5 for the latest information on PageSpeed Insights.\u003c/p\u003e\n"],["\u003cp\u003eAvoid or minimize blocking JavaScript, especially external scripts, to improve page load times.\u003c/p\u003e\n"],["\u003cp\u003eInline small, essential JavaScript code and use \u003ccode\u003easync\u003c/code\u003e or \u003ccode\u003edefer\u003c/code\u003e attributes for non-critical scripts.\u003c/p\u003e\n"],["\u003cp\u003eFor JavaScript libraries and frameworks, explore asynchronous loading, deferred execution, or server-side rendering for optimal performance.\u003c/p\u003e\n"]]],["The key issue is blocking JavaScript, especially external scripts, which halt HTML parsing and delay page rendering. Recommendations include inlining small, critical scripts to avoid network requests, and making non-critical scripts asynchronous or deferred. Asynchronous scripts use the `async` attribute, while deferred scripts load after the initial render. JavaScript libraries and frameworks should be evaluated to see if they can be loaded asynchronously or inlined to avoid extra round trips.\n"],null,["# Remove Render-Blocking JavaScript\n\n| **Deprecated** . This page was written for version 4 of the PageSpeed Insights API, which is deprecated and will be shut down in May 2019. [Version 5](/speed/docs/insights/v5/get-started) is the latest and provides both real-world data from the Chrome User Experience Report and lab data from Lighthouse.\nThis rule triggers when PageSpeed Insights detects that your HTML references a blocking external JavaScript file in the above-the-fold portion of your page.\n\n### Overview\n\nBefore the browser can render a page it has to build the DOM tree by parsing the HTML markup. During this process, whenever the parser encounters a script it has to stop and execute it before it can continue parsing the HTML. In the case of an external script the parser is also forced to wait for the resource to download, which may incur one or more network roundtrips and delay the time to first render of the page. See [Adding Interactivity with JavaScript](/web/fundamentals/performance/critical-rendering-path/adding-interactivity-with-javascript) to learn more about how JavaScript affects the critical rendering path.\n\n### Recommendations\n\nYou should avoid and minimize the use of blocking JavaScript, especially external scripts that must be fetched before they can be executed. Scripts that are necessary to render page content can be inlined to avoid extra network requests, however the inlined content needs to be small and must execute quickly to deliver good performance. Scripts that are not critical to initial render should be made asynchronous or deferred until after the first render. Please keep in mind that for this to improve your loading time, you must also [optimize CSS delivery](/speed/docs/insights/OptimizeCSSDelivery).\n\nInline JavaScript\n-----------------\n\nExternal blocking scripts force the browser to wait for the JavaScript to be fetched, which may add one or more network roundtrips before the page can be rendered. If the external scripts are small, you can inline their contents directly into the HTML document and avoid the network request latency. For example, if the HTML document looks like this: \n\n```carbon\n\u003chtml\u003e\n \u003chead\u003e\n \u003cscript type=\"text/javascript\" src=\"small.js\"\u003e\u003c/script\u003e\n \u003c/head\u003e\n \u003cbody\u003e\n \u003cdiv\u003e\n Hello, world!\n \u003c/div\u003e\n \u003c/body\u003e\n\u003c/html\u003e\n```\nAnd the resource `small.js` is like this: \n\n```scilab\n /* contents of a small JavaScript file */\n```\nThen you can inline the script as follows: \n\n```carbon\n\u003chtml\u003e\n \u003chead\u003e\n \u003cscript type=\"text/javascript\"\u003e\n /* contents of a small JavaScript file */\n \u003c/script\u003e\n \u003c/head\u003e\n \u003cbody\u003e\n \u003cdiv\u003e\n Hello, world!\n \u003c/div\u003e\n \u003c/body\u003e\n\u003c/html\u003e\n```\nInlining the script contents eliminates the external request for `small.js` and allows the browser to deliver a faster time to first render. However, note that inlining also increases the size of the HTML document and that the same script contents may need to be inlined across multiple pages. As a result, you should only inline small scripts to deliver best performance.\n\nMake JavaScript Asynchronous\n----------------------------\n\nBy default JavaScript blocks DOM construction and thus delays the time to first render. To prevent JavaScript from blocking the parser we recommend using the HTML `async` attribute on external scripts. For example: \n\n```\n\u003cscript async src=\"my.js\"\u003e\n```\nSee [Parser Blocking vs. Asynchronous JavaScript](/web/fundamentals/performance/critical-rendering-path/adding-interactivity-with-javascript#parser_blocking_versus_asynchronous_javascript) to learn more about asynchronous scripts. Note that asynchronous scripts are not guaranteed to execute in specified order and should not use `document.write`. Scripts that depend on execution order or need to access or modify the DOM or CSSOM of the page may need to be rewritten to account for these constraints.\n\nDefer loading of JavaScript\n---------------------------\n\nThe loading and execution of scripts that are not necessary for the initial page render may be deferred until after the initial render or other critical parts of the page have finished loading. Doing so can help reduce resource contention and improve performance.\n\nFAQ\n---\n\nWhat if I am using a JavaScript library such as jQuery?\n: Many JavaScript libraries, such as JQuery, are used to enhance the page to add\n additional interactivity, animations, and other effects. However, many of these\n behaviors can be safely added after the above-the-fold content is rendered.\n Investigate making such JavaScript asynchronous or defer its loading.\n\nWhat if I'm using a JavaScript framework to construct the page?\n: If the content of the page is constructed by client-side JavaScript,\n then you should investigate inlining the relevant JavaScript modules to avoid extra\n network roundtrips. Similarly, leveraging server-side rendering can significantly\n improve first page load performance: render JavaScript templates on the server to deliver\n fast first render, and then use client-side templating once the page is loaded. For more\n information on server-side rendering, see \u003chttp://youtu.be/VKTWdaupft0?t=14m28s\u003e.\n\nFeedback\n--------\n\nWas this page helpful? \nYes Great! Thank you for the feedback. If you have a specific, answerable question about using PageSpeed Insights, ask the question in English on [Stack\n| Overflow](https://stackoverflow.com/questions/tagged/pagespeed-insights). For general questions, feedback, and discussion, start a thread in the [mailing list](https://groups.google.com/forum/#!forum/pagespeed-insights-discuss).\nNo Sorry to hear that. If you have a specific, answerable question about using PageSpeed Insights, ask the question in English on [Stack\n| Overflow](https://stackoverflow.com/questions/tagged/pagespeed-insights). For general questions, feedback, and discussion, start a thread in the [mailing list](https://groups.google.com/forum/#!forum/pagespeed-insights-discuss)."]]