在Yahoo系的最佳实践里,建议把静态的内容尽早的输出,在head之后就调用flush,让浏览器可以尽早去加载静态资源,包括脚本、样式、图片(javascipt,css,image一般是外链的形式)等等,后台如果有多个数据源或者api需要调用,尽可能做到完成一个输出一个,通过js在前端拼装页面,进而达到优化用户体验的效果,用户等待的时间,是木桶最短的那快木板。
下面是引用:
Flush the Buffer Early
tag: server
When users request a page, it can take anywhere from 200 to 500ms for the backend server to stitch together the HTML page. During this time, the browser is idle as it waits for the data to arrive. In PHP you have the function flush(). It allows you to send your partially ready HTML response to the browser so that the browser can start fetching components while your backend is busy with the rest of the HTML page. The benefit is mainly seen on busy backends or light frontends.
A good place to consider flushing is right after the HEAD because the HTML for the head is usually easier to produce and it allows you to include any CSS and JavaScript files for the browser to start fetching in parallel while the backend is still processing.
Example:
...
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号