Skip to content

Commit 722ef2c

Browse files
committed
Speedup Lizmap Html map page by prefetching data
1 parent 0d91bf9 commit 722ef2c

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

assets/src/legacy/view.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,95 @@ var searchProjects = function(){
281281
}
282282
}
283283

284+
var addPrefetchOnClick = function () {
285+
console.log('Test '+$('a.liz-project-view').length );
286+
const links = [{
287+
url: lizUrls.map,
288+
type: 'text/html',
289+
as: 'document',
290+
params: {},
291+
},{
292+
url: lizUrls.config,
293+
type: 'application/json',
294+
as: 'fetch',
295+
params: {},
296+
},{
297+
url: lizUrls.keyValueConfig,
298+
type: 'application/json',
299+
as: 'fetch',
300+
params: {},
301+
},{
302+
url: lizUrls.ogcService,
303+
type: 'text/xml',
304+
as: 'fetch',
305+
params: {
306+
SERVICE: 'WMS',
307+
REQUEST: 'GetCapabilities',
308+
VERSION: '1.3.0',
309+
},
310+
},{
311+
url: lizUrls.ogcService,
312+
type: 'text/xml',
313+
as: 'fetch',
314+
params: {
315+
SERVICE: 'WFS',
316+
REQUEST: 'GetCapabilities',
317+
VERSION: '1.0.0',
318+
},
319+
},{
320+
url: lizUrls.ogcService,
321+
type: 'text/xml',
322+
as: 'fetch',
323+
params: {
324+
SERVICE: 'WMTS',
325+
REQUEST: 'GetCapabilities',
326+
VERSION: '1.0.0',
327+
},
328+
}];
329+
$('a.liz-project-view').click(function () {
330+
var self = $(this);
331+
var projElem = self.parent().parent().find('div.liz-project');
332+
if (projElem.length < 1) {
333+
alert('no project');
334+
return false;
335+
}
336+
projElem = projElem[0];
337+
var repId = projElem.dataset.lizmapRepository;
338+
var projId = projElem.dataset.lizmapProject;
339+
links.forEach(link => {
340+
const params = new URLSearchParams();
341+
params.append('repository', repId);
342+
params.append('project', projId);
343+
for (const key in link.params) {
344+
params.append(key, link.params[key]);
345+
}
346+
//create link tag
347+
const linkTag = document.createElement('link');
348+
linkTag.rel = 'prefetch';
349+
linkTag.href = link.url+'?'+params;
350+
linkTag.type = link.type;
351+
linkTag.as = link.as;
352+
//inject tag in the head of the document
353+
document.head.appendChild(linkTag);
354+
});
355+
356+
return true;
357+
});
358+
}
359+
284360
window.addEventListener('load', function () {
361+
// Initialize global variables
362+
const lizmapVariablesJSON = document.getElementById('lizmap-vars')?.innerText;
363+
if (lizmapVariablesJSON) {
364+
try {
365+
const lizmapVariables = JSON.parse(lizmapVariablesJSON);
366+
for (const variable in lizmapVariables) {
367+
globalThis[variable] = lizmapVariables[variable];
368+
}
369+
} catch {
370+
console.warn('JSON for Lizmap global variables is not valid!');
371+
}
372+
}
285373
searchProjects();
374+
addPrefetchOnClick();
286375
});

lizmap/modules/view/controllers/default.classic.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,14 @@ public function index()
181181
}
182182
$rep->body->assign('showHomeLink', false);
183183

184+
$lizUrls = array(
185+
'map' => jUrl::get('view~map:index'),
186+
'config' => jUrl::get('lizmap~service:getProjectConfig'),
187+
'keyValueConfig' => jUrl::get('lizmap~service:getKeyValueConfig'),
188+
'ogcService' => jUrl::get('lizmap~service:index'),
189+
);
190+
$rep->addJsVariable('lizUrls', $lizUrls);
191+
184192
return $rep;
185193
}
186194

0 commit comments

Comments
 (0)