Leaflet with GitHub Pages - not rendering

断了今生、忘了曾经 提交于 2019-12-11 05:14:32

问题


I have a simple webpage into which I have embedded a basic leaflet map. When I try to publish this via my GitHub Pages website, the leaflet map won't render (everything else is fine).

GitHub Pages spec says it supports JS and this shouldn't be a problem as it's an API. I have tried to use a downloaded version of leaflet (.css and .js files uploaded to my gh-pages repository), but still nothing.

Anyone have any ideas why this isn't working? Is this what GitHub Pages means when it says that it will host "static" webpages (no APIs, no JS interactivity)?


回答1:


EDIT

Leaflet now officially recommends unpkg CDN which does support https:

<link rel="stylesheet" href="https://unpkg.com/leaflet@0.7.7/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@0.7.7/dist/leaflet.js"></script>

or

<link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.3/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.3.3/dist/leaflet.js"></script>

Original answer

There is a chance you are just facing a "mixed content" issue (resources from HTTP protocol trying to be loaded on a page accessed through secure HTTPS protocol).

Please note that GitHub pages for recent sites now enforce HTTPS protocol:

HTTPS enforcement is required for GitHub Pages sites created after June 15, 2016 and using a github.io domain.

This means that even if you manually type http://my-site.github.io, you will be automatically redirected to https://my-site.github.io.

The official Leaflet CDN does not support HTTPS protocol as of today unfortunately.

However, you can use alternative CDN's that do support HTTPS, e.g. cdnjs:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.0-rc.1/leaflet.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.0-rc.1/leaflet.js"></script>

But it is strange it still does not work when you try to use local (downloaded) scripts?

Note:

GitHub Pages serving only "static" webpages means that they cannot be dynamically generated server-side, e.g. through PHP. However you are free to use client-side code (JavaScript). E.g., Leaflet official website is actually hosted on GitHub.



来源:https://stackoverflow.com/questions/38136637/leaflet-with-github-pages-not-rendering

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!