简体   繁体   English

PageSpeed Insights 和 php 包括

[英]PageSpeed Insights and php include

I'm cleaning up the web page (HTML and PHP) code to improve the PageSpeed Insights reports and I noticed a bad result using PHP include (or include_once ).我正在清理网页(HTML 和 PHP)代码以改进 PageSpeed Insights 报告,我注意到使用 PHP include (或include_once )的结果不好。

To share common code between pages I use to copy the code to external files (ie inc_header.php ) and include them in more pages.为了在页面之间共享公共代码,我使用将代码复制到外部文件(即 inc_header.php )并将它们包含在更多页面中。 It is a consolidated practice.这是一种综合实践。

Today I noticed relevant different results in PageSpeed if I test a static page or the same page using a couple of included files.今天,如果我使用几个包含的文件测试静态页面或同一页面,我在 PageSpeed 中注意到相关的不同结果。

Is it true that PHP includes influence the PageSpeed Insight score? PHP 包括影响 PageSpeed Insight 分数是真的吗? What can I do to prevent this?我能做些什么来防止这种情况?

The include can do a lot of invisible requests (database requests for example) to generate the code. include 可以做很多不可见的请求(例如数据库请求)来生成代码。 If you serve a page without server side includes, you will get a lower TTFB.如果您在没有服务器端包含的情况下提供页面,您将获得较低的 TTFB。 In other words: the results you are seeing can be exlained and are as expected.换句话说:您看到的结果可以解释并且符合预期。

A common trick to tackle this delay is to use a Static Site Generator.解决此延迟的常见技巧是使用静态站点生成器。 SSG's generate static pages that contain the output of these includes (preventing the extra requests). SSG 生成静态页面,其中包含这些包含的输出(防止额外请求)。

Another trick to tackle this is to use a cache layer.解决这个问题的另一个技巧是使用缓存层。 A cache layer does (almost) the same as a Static Site Generator.缓存层(几乎)与静态站点生成器的功能相同。 When you cache (the output of) your request you prevent these invisible requests too.当您缓存(输出)您的请求时,您也会阻止这些不可见的请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM