Skip to content

Commit

Permalink
feat: set preconnect link for tilelayer
Browse files Browse the repository at this point in the history
  • Loading branch information
yohanboniface committed Jan 22, 2024
1 parent dc22cf8 commit d4e2531
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 4 deletions.
5 changes: 5 additions & 0 deletions umap/templates/umap/map_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
map_detail
{% endblock body_class %}
{% block extra_head %}
{% if preconnect_domains %}
{% for domain in preconnect_domains %}
<link rel="preconnect" href="{{ domain }}" />
{% endfor %}
{% endif %}
{% umap_css %}
{% umap_js locale=locale %}
{% if object.share_status != object.PUBLIC %}<meta name="robots" content="noindex">{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion umap/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class MapFactory(factory.django.DjangoModelFactory):
"attribution": "\xa9 OSM Contributors",
"maxZoom": 18,
"minZoom": 0,
"url_template": "https://{s}.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png",
"url_template": "https://a.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png",
},
"tilelayersControl": True,
"zoom": 7,
Expand Down
2 changes: 1 addition & 1 deletion umap/tests/integration/test_export_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_umap_export(map, live_server, datalayer, page):
"attribution": "© OSM Contributors",
"maxZoom": 18,
"minZoom": 0,
"url_template": "https://{s}.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png",
"url_template": "https://a.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png",
},
"tilelayersControl": True,
"zoom": 7,
Expand Down
29 changes: 29 additions & 0 deletions umap/tests/integration/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,35 @@
pytestmark = pytest.mark.django_db


def test_preconnect_for_tilelayer(map, page, live_server, tilelayer):
page.goto(f"{live_server.url}{map.get_absolute_url()}")
meta = page.locator('link[rel="preconnect"]')
expect(meta).to_have_count(1)
expect(meta).to_have_attribute("href", "//a.tile.openstreetmap.fr")
# Add custom tilelayer
map.settings["properties"]["tilelayer"] = {
"name": "OSM Piano FR",
"maxZoom": 20,
"minZoom": 0,
"attribution": "test",
"url_template": "https://a.piano.tiles.quaidorsay.fr/fr{r}/{z}/{x}/{y}.png",
}
map.save()
page.goto(f"{live_server.url}{map.get_absolute_url()}")
expect(meta).to_have_attribute("href", "//a.piano.tiles.quaidorsay.fr")
# Add custom tilelayer with variable in domain, should create a preconnect
map.settings["properties"]["tilelayer"] = {
"name": "OSM Piano FR",
"maxZoom": 20,
"minZoom": 0,
"attribution": "test",
"url_template": "https://{s}.piano.tiles.quaidorsay.fr/fr{r}/{z}/{x}/{y}.png",
}
map.save()
page.goto(f"{live_server.url}{map.get_absolute_url()}")
expect(meta).to_have_count(0)


def test_default_view_latest_without_datalayer_should_use_default_center(
map, live_server, datalayer, page
):
Expand Down
4 changes: 2 additions & 2 deletions umap/tests/test_map_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def post_data():
return {
"name": "name",
"center": '{"type":"Point","coordinates":[13.447265624999998,48.94415123418794]}', # noqa
"settings": '{"type":"Feature","geometry":{"type":"Point","coordinates":[5.0592041015625,52.05924589011585]},"properties":{"tilelayer":{"maxZoom":20,"url_template":"http://{s}.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png","minZoom":0,"attribution":"HOT and friends"},"licence":"","description":"","name":"test enrhûmé","tilelayersControl":true,"displayDataBrowserOnLoad":false,"displayPopupFooter":true,"displayCaptionOnLoad":false,"miniMap":true,"moreControl":true,"scaleControl":true,"zoomControl":true,"datalayersControl":true,"zoom":8}}', # noqa
"settings": '{"type":"Feature","geometry":{"type":"Point","coordinates":[5.0592041015625,52.05924589011585]},"properties":{"tilelayer":{"maxZoom":20,"url_template":"http://a.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png","minZoom":0,"attribution":"HOT and friends"},"licence":"","description":"","name":"test enrhûmé","tilelayersControl":true,"displayDataBrowserOnLoad":false,"displayPopupFooter":true,"displayCaptionOnLoad":false,"miniMap":true,"moreControl":true,"scaleControl":true,"zoomControl":true,"datalayersControl":true,"zoom":8}}', # noqa
}


Expand Down Expand Up @@ -624,7 +624,7 @@ def test_download(client, map, datalayer):
"attribution": "© OSM Contributors",
"maxZoom": 18,
"minZoom": 0,
"url_template": "https://{s}.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png",
"url_template": "https://a.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png",
},
"tilelayersControl": True,
"zoom": 7,
Expand Down
16 changes: 16 additions & 0 deletions umap/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,21 @@ class MapDetailMixin:
model = Map
pk_url_kwarg = "map_id"

def set_preconnect(self, properties, context):
# Try to extract the tilelayer domain, in order to but a preconnect meta.
url_template = properties.get("tilelayer", {}).get("url_template")
# Not explicit tilelayer set, take the first of the list, which will be
# used by frontend too.
if not url_template:
tilelayers = properties.get("tilelayers")
if tilelayers:
url_template = tilelayers[0].get("url_template")
if url_template:
domain = urlparse(url_template).netloc
# Do not try to preconnect on domains with variables
if domain and "{" not in domain:
context["preconnect_domains"] = [f"//{domain}"]

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
user = self.request.user
Expand Down Expand Up @@ -473,6 +488,7 @@ def get_context_data(self, **kwargs):
map_settings["properties"].update(properties)
map_settings["properties"]["datalayers"] = self.get_datalayers()
context["map_settings"] = json.dumps(map_settings, indent=settings.DEBUG)
self.set_preconnect(map_settings["properties"], context)
return context

def get_datalayers(self):
Expand Down

0 comments on commit d4e2531

Please sign in to comment.