问题描述
我正在尝试改善我的Google PageSpeed Insigths。在“预加载密钥请求”类别中,我可以看到以下结果:
…css/199038f….woff2
4500 ms
…css/19c1b86….woff2
4230 ms
…css/570eb83….woff2
4080 ms
这些文件位于classic / assets / css /中,无论如何我都找不到将其添加到代码中的位置,因此我可以将这些链接修复为预加载。我在classic / templates / _partials /中找到了一个文件stylesheets.tpl,并且进行了更改:
{foreach $stylesheets.external as $stylesheet}
<link rel="stylesheet" href="{$stylesheet.uri}" type="text/css" media="{$stylesheet.media}">
{/foreach}
到
{foreach $stylesheets.external as $stylesheet}
<link rel="preload" as="style" href="{$stylesheet.uri}" media="{$stylesheet.media}">
{/foreach}
它对洞察有效,不再对此发出警告,但不幸的是,它破坏了网站(表现为根本没有样式)。我尝试了一些其他组合,例如:as =“ font”,as =“ font” type =“ woff2”,但这没有帮助。
我现在的计划是从$ stylesheets.external中删除这三个CSS文件,并在head.tpl中手动添加它们,但是我找不到在哪里找到该对象?
如何编辑$ stylesheets变量?
解决方法
您可以找到在FrontController.php中分配的样式表变量
函数名称显示()
文件路径:classes / controller / FrontController.php
,FrontController中确实提到的函数包含:
public function display()
{
$this->context->smarty->assign(array(
'layout' => $this->getLayout(),'stylesheets' => $this->getStylesheets(),'javascript' => $this->getJavascript(),'js_custom_vars' => Media::getJsDef(),'notifications' => $this->prepareNotifications(),));
$this->smartyOutputContent($this->template);
return true;
}
所以我搜索了这个getStylesheets(),就这样:
public function getStylesheets()
{
$cssFileList = $this->stylesheetManager->getList();
if (Configuration::get('PS_CSS_THEME_CACHE')) {
$cssFileList = $this->cccReducer->reduceCss($cssFileList);
}
return $cssFileList;
}
然后搜索stylesheetManager等,但是无论如何都不是将某些样式表添加到此变量的地方(在StylesheetManager.php中都不是)。我需要找到一个文件(或类似文件),所有这些样式表都已注册/添加到此样式表变量中。有谁知道它在哪里?
,我已经解释了如何修改显示功能并在样式表中分配新数组。请检查以下内容:
public function display()
{
$arrStylessheets = $this->getStylesheets();
print_r($arrStylessheets);//check those stylesheets keys here
unset($arrStylessheets['<those stylesheets here>']);
$this->context->smarty->assign(array(
'layout' => $this->getLayout(),'stylesheets' => $arrStylessheets,// pass new array stylesheet
'javascript' => $this->getJavascript(),));
$this->smartyOutputContent($this->template);
return true;
}