大网数据广告活动
网站运营与故障问答

Putting CSS/JS in header for faster load times

· 52CE 我爱测

I've been reading many articles lately about SEO on load times. Trying to get the perfect load time can be quite time-consuming but also a good feeling when you see your site loading twice as quick. Now a lot of these tutorials a…

问题原文

原帖链接 · Matthew Smart · CC BY-SA 3.0

I've been reading many articles lately about SEO on load times. Trying to get the perfect load time can be quite time-consuming but also a good feeling when you see your site loading twice as quick.

Now a lot of these tutorials and blogs are saying to put chunks of CSS/JS in you head tag to help above-the-fold content to load faster.

"external resources take longer to load", each request back to the server for a file takes more time. So would it not make sense to actually just embed all CSS/JS in your head tag from the beginning.

Sure this could look messy at first, but the only reason why it takes long for external scripts to download in the browser is because the output has already been sent to the browser.

What if we simply do:

<head>
    ... Usual things here ...
    <?php require('css/bootstrap'); ?>
    <?php require('css/mystylesheet'); ?>
</head>
<body>
    ... Usual things here ...
    <?php require('js/bootstrap.js'); ?>
    <?php require('js/myscript.js'); ?>
</body>

Now this will just be served as a simple HTML file with embedded CSS and JavaScript as soon as it hits the users browser. And it is just as clean looking in our text editor as before because we require the file in PHP.

Would this improve overall speed performance or should i stay clear of this method?

已采纳解答

原帖链接 · Rob · CC BY-SA 3.0

This question might belong in Stackoverflow and not here but you are including the complete stylesheets and scripts in each of your pages. That increases the download and computation time for each page significantly while the recommendation you speak of is only talking about less significant amounts of styling and code. Doing what you show can increase your page download size perhaps 10x or more.

Imagine trying to load all your markup, styling and code while on a phone with a bad connection. Lots of sites can barely function serving up HTML and basic CSS, much less all that.

What the suggestion is trying to do is get you to provide a minimal amount of markup to make the initial user experience usable as quickly as possible so you can add all the other fluff later.

Now, is this time consuming and difficult? Yep. Depending on the site.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2016-05-31)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。