Skip to content

Commit 2265b8c

Browse files
committed
Speedup Lizmap Html page loading by adding preload link
Documentation about preload link https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel/preload
1 parent 3f12763 commit 2265b8c

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

lizmap/app/responses/AbstractLizmapHtmlResponse.php

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,24 @@ class AbstractLizmapHtmlResponse extends jResponseHtml
1616
{
1717
protected $CSPPropName = 'mapCSPHeader';
1818

19+
protected function outputJsScriptTag($fileUrl, $scriptParams)
20+
{
21+
echo '<link rel="preload" href="',htmlspecialchars($fileUrl),'" as="script" ',$this->_endTag;
22+
parent::outputJsScriptTag($fileUrl, $scriptParams);
23+
}
24+
25+
protected function outputCssLinkTag($fileUrl, $cssParams)
26+
{
27+
echo '<link rel="preload" href="',htmlspecialchars($fileUrl),'" as="style" ',$this->_endTag;
28+
parent::outputCssLinkTag($fileUrl, $cssParams);
29+
}
30+
31+
protected function outputIconLinkTag($fileUrl, $iconParams)
32+
{
33+
echo '<link rel="preload" href="',htmlspecialchars($fileUrl),'" as="image" ',$this->_endTag;
34+
parent::outputIconLinkTag($fileUrl, $iconParams);
35+
}
36+
1937
protected function prepareHeadContent()
2038
{
2139
$bp = jApp::urlBasePath();
@@ -64,8 +82,44 @@ public function addJsVariables(array $variables)
6482
$this->jsVarData = array_merge($this->jsVarData, $variables);
6583
}
6684

85+
protected $preloadLink = array();
86+
87+
public function addPreloadLink($href, $as, $type = null)
88+
{
89+
$this->preloadLink[] = array(
90+
'href' => $href,
91+
'as' => $as,
92+
'type' => $type,
93+
);
94+
}
95+
6796
protected function doAfterActions()
6897
{
69-
$this->addHeadContent('<script id="lizmap-vars" type="application/json">'.json_encode($this->jsVarData).'</script>');
98+
$this->addHeadContent('<!-- Start preload -->');
99+
// Preload links
100+
// css link
101+
/*foreach ($this->webAssetsSelection->getCssLinks() as $src) {
102+
$this->addHeadContent('<link rel="preload" href="'.$src[0].'" as="style">');
103+
}
104+
foreach ($this->_CSSLink as $src => $params) {
105+
$this->addHeadContent('<link rel="preload" href="'.$src.'" as="style">');
106+
}
107+
// js link
108+
foreach ($this->webAssetsSelection->getJsLinks() as $src) {
109+
$this->addHeadContent('<link rel="preload" href="'.$src[0].'" as="script">');
110+
}
111+
foreach ($this->_JSLink as $src => $params) {
112+
$this->addHeadContent('<link rel="preload" href="'.$src.'" as="script">');
113+
}
114+
// others links
115+
foreach ($this->_Link as $href => $params) {
116+
$this->addHeadContent('<link rel="preload" href="'.$href.'" as="'.$params[0].'"'.($params[1] ? ' type="'.$params[1].'"' : '').'>');
117+
}*/
118+
// other preload links
119+
foreach ($this->preloadLink as $link) {
120+
$this->addHeadContent('<link rel="preload" href="'.$link['href'].'" as="'.$link['as'].'"'.($link['type'] ? ' type="'.$link['type'].'"' : '').'>');
121+
}
122+
$this->addHeadContent('<!-- End preload -->');
123+
$this->addHeadContent('<script id="lizmap-vars" type="application/json">'.json_encode($this->jsVarData).'</script>'."\n");
70124
}
71125
}

0 commit comments

Comments
 (0)