From 722ef2c948972df75b9d314bd38a1f2d5bdf80fa Mon Sep 17 00:00:00 2001 From: rldhont Date: Fri, 7 Mar 2025 14:01:15 +0100 Subject: [PATCH] Speedup Lizmap Html map page by prefetching data --- assets/src/legacy/view.js | 89 +++++++++++++++++++ .../view/controllers/default.classic.php | 8 ++ 2 files changed, 97 insertions(+) diff --git a/assets/src/legacy/view.js b/assets/src/legacy/view.js index 4aa0e0a276..a18520af9b 100644 --- a/assets/src/legacy/view.js +++ b/assets/src/legacy/view.js @@ -281,6 +281,95 @@ var searchProjects = function(){ } } +var addPrefetchOnClick = function () { + console.log('Test '+$('a.liz-project-view').length ); + const links = [{ + url: lizUrls.map, + type: 'text/html', + as: 'document', + params: {}, + },{ + url: lizUrls.config, + type: 'application/json', + as: 'fetch', + params: {}, + },{ + url: lizUrls.keyValueConfig, + type: 'application/json', + as: 'fetch', + params: {}, + },{ + url: lizUrls.ogcService, + type: 'text/xml', + as: 'fetch', + params: { + SERVICE: 'WMS', + REQUEST: 'GetCapabilities', + VERSION: '1.3.0', + }, + },{ + url: lizUrls.ogcService, + type: 'text/xml', + as: 'fetch', + params: { + SERVICE: 'WFS', + REQUEST: 'GetCapabilities', + VERSION: '1.0.0', + }, + },{ + url: lizUrls.ogcService, + type: 'text/xml', + as: 'fetch', + params: { + SERVICE: 'WMTS', + REQUEST: 'GetCapabilities', + VERSION: '1.0.0', + }, + }]; + $('a.liz-project-view').click(function () { + var self = $(this); + var projElem = self.parent().parent().find('div.liz-project'); + if (projElem.length < 1) { + alert('no project'); + return false; + } + projElem = projElem[0]; + var repId = projElem.dataset.lizmapRepository; + var projId = projElem.dataset.lizmapProject; + links.forEach(link => { + const params = new URLSearchParams(); + params.append('repository', repId); + params.append('project', projId); + for (const key in link.params) { + params.append(key, link.params[key]); + } + //create link tag + const linkTag = document.createElement('link'); + linkTag.rel = 'prefetch'; + linkTag.href = link.url+'?'+params; + linkTag.type = link.type; + linkTag.as = link.as; + //inject tag in the head of the document + document.head.appendChild(linkTag); + }); + + return true; + }); +} + window.addEventListener('load', function () { + // Initialize global variables + const lizmapVariablesJSON = document.getElementById('lizmap-vars')?.innerText; + if (lizmapVariablesJSON) { + try { + const lizmapVariables = JSON.parse(lizmapVariablesJSON); + for (const variable in lizmapVariables) { + globalThis[variable] = lizmapVariables[variable]; + } + } catch { + console.warn('JSON for Lizmap global variables is not valid!'); + } + } searchProjects(); + addPrefetchOnClick(); }); diff --git a/lizmap/modules/view/controllers/default.classic.php b/lizmap/modules/view/controllers/default.classic.php index 72d2b12321..583716807d 100644 --- a/lizmap/modules/view/controllers/default.classic.php +++ b/lizmap/modules/view/controllers/default.classic.php @@ -181,6 +181,14 @@ public function index() } $rep->body->assign('showHomeLink', false); + $lizUrls = array( + 'map' => jUrl::get('view~map:index'), + 'config' => jUrl::get('lizmap~service:getProjectConfig'), + 'keyValueConfig' => jUrl::get('lizmap~service:getKeyValueConfig'), + 'ogcService' => jUrl::get('lizmap~service:index'), + ); + $rep->addJsVariable('lizUrls', $lizUrls); + return $rep; }