Skip to content

Commit 5c140b3

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 5c140b3

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

lizmap/app/responses/AbstractLizmapHtmlResponse.php

Lines changed: 36 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,25 @@ 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+
// other preload links
100+
foreach ($this->preloadLink as $link) {
101+
$this->addHeadContent('<link rel="preload" href="'.$link['href'].'" as="'.$link['as'].'"'.($link['type'] ? ' type="'.$link['type'].'"' : '').'>');
102+
}
103+
$this->addHeadContent('<!-- End preload -->');
104+
$this->addHeadContent('<script id="lizmap-vars" type="application/json">'.json_encode($this->jsVarData).'</script>'."\n");
70105
}
71106
}

0 commit comments

Comments
 (0)