diff --git a/src/octoprint/server/util/flask.py b/src/octoprint/server/util/flask.py index 649704da..11ef9bc3 100644 --- a/src/octoprint/server/util/flask.py +++ b/src/octoprint/server/util/flask.py @@ -312,6 +312,18 @@ class LessSimpleCache(BaseCache): return False return len(self._cache) > self._threshold + def __getitem__(self, key): + return self.get(key) + + def __setitem__(self, key, value): + return self.set(key, value) + + def __delitem__(self, key): + return self.delete(key) + + def __contains__(self, key): + return key in self._cache + _cache = LessSimpleCache() def cached(timeout=5 * 60, key=lambda: "view:%s" % flask.request.path, unless=None, refreshif=None, unless_response=None): @@ -358,6 +370,9 @@ def cached(timeout=5 * 60, key=lambda: "view:%s" % flask.request.path, unless=No return decorator +def is_in_cache(key=lambda: "view:%s" % flask.request.path): + return key() in _cache + def cache_check_headers(): return "no-cache" in flask.request.cache_control or "no-cache" in flask.request.pragma diff --git a/src/octoprint/server/views.py b/src/octoprint/server/views.py index 0a825e2f..bcc29ce1 100644 --- a/src/octoprint/server/views.py +++ b/src/octoprint/server/views.py @@ -9,7 +9,7 @@ import os import datetime from collections import defaultdict -from flask import request, g, url_for, make_response, render_template, send_from_directory, redirect +from flask import request, g, url_for, make_response, render_template, send_from_directory, redirect, abort import octoprint.plugin @@ -20,6 +20,7 @@ from octoprint.settings import settings from octoprint.filemanager import get_all_extensions import re +import base64 from . import util @@ -29,6 +30,17 @@ _logger = logging.getLogger(__name__) _valid_id_re = re.compile("[a-z_]+") _valid_div_re = re.compile("[a-zA-Z_-]+") +@app.route("/cached.gif") +def in_cache(): + url = request.base_url.replace("/cached.gif", "/") + key = lambda: "view:{}:{}".format(url, g.locale.language if g.locale else "en") + if not util.flask.is_in_cache(key): + return abort(404) + + response = make_response(bytes(base64.b64decode("R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"))) + response.headers["Content-Type"] = "image/gif" + return response + @app.route("/") @util.flask.preemptively_cached(cache=preemptiveCache, data=lambda: dict(path=request.path, base_url=request.url_root, query_string="l10n={}".format(g.locale.language) if g.locale else "en"), diff --git a/src/octoprint/static/intermediary.html b/src/octoprint/static/intermediary.html index 1f8b3919..56c621e5 100644 --- a/src/octoprint/static/intermediary.html +++ b/src/octoprint/static/intermediary.html @@ -153,20 +153,20 @@ if (baseUrl[baseUrl.length - 1] != "/") { baseUrl += "/"; } + var indexCachedUrl = baseUrl + "cached.gif"; var serverOnlineUrl = baseUrl + "online.gif"; var backendOnlineUrl = baseUrl + "intermediary.gif"; var serverTimeout; + var cachedTimeout; var message = window.document.getElementById("message"); - var serverIsOnline = false; - var serverOnlineCallback = function(result) { + var indexCachedCallback = function(result) { if (result == "load") { - // our online.gif loaded, so the server is up, let's reload - serverIsOnline = true; + // our cached.gif loaded, so the index is cached now, let's reload message.className = "pulsate1 green"; - message.innerText = "OctoPrint server online, reloading page..."; + message.innerText = "OctoPrint server ready, reloading page..."; var reloadUrl = baseUrl + currentQuery + currentFragment; if (reloadUrl == window.location.href) { @@ -175,7 +175,21 @@ window.location.href = reloadUrl; } } else { - // online.gif still not available, let's look at + // cached.gif still not available, let's look at it again in a second + cachedTimeout = setTimeout(function() { + ping(indexCachedUrl, timeout, indexCachedCallback); + }, 1000) + } + }; + + var serverIsOnline = false; + var serverOnlineCallback = function(result) { + if (result == "load") { + // our online.gif loaded, so the server is up, let's wait for it to become ready + serverIsOnline = true; + ping(indexCachedUrl, timeout, indexCachedCallback); + } else { + // online.gif still not available, let's look at it again later var interval = 10; if (intervals.length) { interval = intervals.shift(); @@ -204,6 +218,9 @@ if (serverTimeout) { window.clearTimeout(serverTimeout); } + if (cachedTimeout) { + window.clearTimeout(cachedTimeout); + } message.className = "red"; message.innerHTML = "Looks like something went wrong during startup, the server is gone again. You should check octoprint.log.";