improve receiver load times by concating javascript

This commit is contained in:
Jakob Ketterl 2020-05-02 13:35:42 +02:00
parent 7a3043559f
commit 8c8445eb3b
3 changed files with 67 additions and 21 deletions

View File

@ -24,18 +24,7 @@
<head>
<title>OpenWebRX | Open Source SDR Web App for Everyone!</title>
<link rel="shortcut icon" type="image/x-icon" href="static/favicon.ico" />
<script src="static/openwebrx.js"></script>
<script src="static/lib/jquery-3.2.1.min.js"></script>
<script src="static/lib/jquery.nanoscroller.js"></script>
<script src="static/lib/Demodulator.js"></script>
<script src="static/lib/DemodulatorPanel.js"></script>
<script src="static/lib/BookmarkBar.js"></script>
<script src="static/lib/AudioEngine.js"></script>
<script src="static/lib/ProgressBar.js"></script>
<script src="static/lib/Measurement.js"></script>
<script src="static/lib/FrequencyDisplay.js"></script>
<script src="static/lib/Js8Threads.js"></script>
<script src="static/lib/Modes.js"></script>
<script src="compiled/receiver.js"></script>
<link rel="stylesheet" type="text/css" href="static/lib/nanoscroller.css" />
<link rel="stylesheet" type="text/css" href="static/css/openwebrx.css" />
<meta charset="utf-8">

View File

@ -4,13 +4,22 @@ from datetime import datetime
import mimetypes
import os
import pkg_resources
from abc import ABCMeta, abstractmethod
import logging
logger = logging.getLogger(__name__)
class AssetsController(Controller):
class AssetsController(Controller, metaclass=ABCMeta):
def getModified(self, file):
return None
return datetime.fromtimestamp(os.path.getmtime(self.getFilePath(file)))
def openFile(self, file):
return open(self.getFilePath(file), "rb")
@abstractmethod
def getFilePath(self, file):
pass
def serve_file(self, file, content_type=None):
@ -41,8 +50,8 @@ class AssetsController(Controller):
class OwrxAssetsController(AssetsController):
def openFile(self, file):
return pkg_resources.resource_stream("htdocs", file)
def getFilePath(self, file):
return pkg_resources.resource_filename("htdocs", file)
class AprsSymbolsController(AssetsController):
@ -57,8 +66,54 @@ class AprsSymbolsController(AssetsController):
def getFilePath(self, file):
return self.path + file
def getModified(self, file):
return datetime.fromtimestamp(os.path.getmtime(self.getFilePath(file)))
def openFile(self, file):
return open(self.getFilePath(file), "rb")
class CompiledAssetsController(Controller):
profiles = {
"receiver.js": [
"openwebrx.js",
"lib/jquery-3.2.1.min.js",
"lib/jquery.nanoscroller.js",
"lib/Demodulator.js",
"lib/DemodulatorPanel.js",
"lib/BookmarkBar.js",
"lib/AudioEngine.js",
"lib/ProgressBar.js",
"lib/Measurement.js",
"lib/FrequencyDisplay.js",
"lib/Js8Threads.js",
"lib/Modes.js",
]
}
def indexAction(self):
profileName = self.request.matches.group(1)
if profileName not in CompiledAssetsController.profiles:
self.send_response("profile not found", code=404)
files = CompiledAssetsController.profiles[profileName]
logger.debug(files)
files = [pkg_resources.resource_filename("htdocs", f) for f in files]
logger.debug(files)
modified = self.getModified(files)
if modified is not None and "If-Modified-Since" in self.handler.headers:
client_modified = datetime.strptime(
self.handler.headers["If-Modified-Since"], "%a, %d %b %Y %H:%M:%S %Z"
)
if modified <= client_modified:
self.send_response("", code=304)
return
contents = [self.getContents(f) for f in files]
(content_type, encoding) = mimetypes.MimeTypes().guess_type(profileName)
self.send_response("\n".join(contents), content_type=content_type, last_modified=modified, max_age=3600)
def getContents(self, file):
with open(file) as f:
return f.read()
def getModified(self, files):
modified = [datetime.fromtimestamp(os.path.getmtime(f)) for f in files]
return max(*modified)

View File

@ -6,7 +6,8 @@ from owrx.controllers.template import (
)
from owrx.controllers.assets import (
OwrxAssetsController,
AprsSymbolsController
AprsSymbolsController,
CompiledAssetsController
)
from owrx.controllers.websocket import WebSocketController
from owrx.controllers.api import ApiController
@ -91,6 +92,7 @@ class Router(object):
StaticRoute("/status", StatusController),
StaticRoute("/status.json", StatusController, options={"action": "jsonAction"}),
RegexRoute("/static/(.+)", OwrxAssetsController),
RegexRoute("/compiled/(.+)", CompiledAssetsController),
RegexRoute("/aprs-symbols/(.+)", AprsSymbolsController),
StaticRoute("/ws/", WebSocketController),
RegexRoute("(/favicon.ico)", OwrxAssetsController),