add a fallback if js is disabled or sw is not supported

This commit is contained in:
zjcqoo 2021-09-09 16:25:40 +08:00
parent 9fc1d6fb7c
commit fd634988af
2 changed files with 28 additions and 10 deletions

View file

@ -23,6 +23,25 @@ Source Files: https://github.com/fanhtml5/test-site
## FAQ
Q: What if JavaScript is disabled?
A: Unfortunately, the page can't be displayed. You can add a fallback in `404.html` such as:
```html
<noscript>
<meta http-equiv=Refresh content="0;url=FALLBACK_URL">
</noscript>
<script src=/x.js></script>
```
----
Q: What if the browser doesn't support Service Worker?
A: Same as above. The program will read the contents of `<noscript>` and render them.
----
Q: Is free CDN safe?
A: Yes, the program will verify the data integrity.
@ -59,12 +78,6 @@ A: Just overwrite `x.js`, the client polls this file every 2 minutes.
----
Q: What if the browser doesn't support Service Worker?
A: Unfortunately, the page can't be displayed. You can add a fallback in `404.html`.
----
Q: Will new features be added?
A: This project is just an experiment, there is a new project named [freecdn](https://github.com/EtherDream/freecdn) which is much more powerful. (better docs will be released soon)

View file

@ -1,8 +1,12 @@
function pageEnv() {
var container = document.documentElement
function showErr(msg) {
container.innerHTML = msg
function fallback(html) {
var noscript = document.getElementsByTagName('noscript')
if (noscript.length > 0) {
html = noscript[0].innerHTML
}
container.innerHTML = html
}
function reload() {
@ -16,11 +20,11 @@ function pageEnv() {
if (jsUrl) {
var sw = navigator.serviceWorker
if (!sw) {
showErr('Error: Service Worker is not supported')
fallback('Service Worker is not supported')
return
}
var swPending = sw.register(jsUrl).catch(function(err) {
showErr(err.message)
fallback(err.message)
})
rootPath = getRootPath(jsUrl)
} else {
@ -119,6 +123,7 @@ function pageEnv() {
function loadNextUrl() {
var url = URLS.shift()
if (!url) {
fallback('failed to load resources')
return
}
if (PRIVACY === 2) {