Bladeren bron

fixes favicon on pages with long path #194

Falko Schindler 2 jaren geleden
bovenliggende
commit
1190cc96e9
5 gewijzigde bestanden met toevoegingen van 11 en 6 verwijderingen
  1. 1 1
      nicegui/client.py
  2. 7 2
      nicegui/favicon.py
  3. 1 1
      nicegui/page.py
  4. 1 1
      nicegui/run.py
  5. 1 1
      nicegui/templates/index.html

+ 1 - 1
nicegui/client.py

@@ -75,7 +75,7 @@ class Client:
             'vue_scripts': vue_scripts,
             'js_imports': vue.generate_js_imports(prefix),
             'title': self.page.resolve_title(),
-            'favicon_url': get_favicon_url(self.page),
+            'favicon_url': get_favicon_url(self.page, prefix),
             'dark': str(self.page.resolve_dark()),
             'prefix': prefix,
             'socket_io_js_extra_headers': globals.socket_io_js_extra_headers,

+ 7 - 2
nicegui/favicon.py

@@ -18,11 +18,16 @@ def create_favicon_routes() -> None:
                               lambda _, favicon=favicon or globals.favicon or fallback: FileResponse(favicon))
 
 
-def get_favicon_url(page: 'page') -> str:
+def get_favicon_url(page: 'page', prefix: str) -> str:
     favicon = page.favicon or globals.favicon
     if is_remote_url(favicon):
         return favicon
-    return f'{page.path[1:]}/favicon.ico' if favicon else '_nicegui/static/favicon.ico'
+    elif not favicon:
+        return f'{prefix}/_nicegui/static/favicon.ico'
+    elif page.path == '/':
+        return f'{prefix}/favicon.ico'
+    else:
+        return f'{prefix}{page.path}/favicon.ico'
 
 
 def is_remote_url(favicon: str) -> bool:

+ 1 - 1
nicegui/page.py

@@ -26,7 +26,7 @@ class page:
 
         :param path: route of the new page (path must start with '/')
         :param title: optional page title
-        :param favicon: optional relative filepath to a favicon (default: `None`, NiceGUI icon will be used)
+        :param favicon: optional relative filepath or absolute URL to a favicon (default: `None`, NiceGUI icon will be used)
         :param dark: whether to use Quasar's dark mode (defaults to `dark` argument of `run` command)
         :param response_timeout: maximum time for the decorated function to build the page (default: 3.0)
         """

+ 1 - 1
nicegui/run.py

@@ -34,7 +34,7 @@ def run(*,
     :param host: start server with this host (default: `'0.0.0.0'`)
     :param port: use this port (default: `8080`)
     :param title: page title (default: `'NiceGUI'`, can be overwritten per page)
-    :param favicon: relative filepath to a favicon (default: `None`, NiceGUI icon will be used)
+    :param favicon: relative filepath or absolute URL to a favicon (default: `None`, NiceGUI icon will be used)
     :param dark: whether to use Quasar's dark mode (default: `False`, use `None` for "auto" mode)
     :param binding_refresh_interval: time between binding updates (default: `0.1` seconds, bigger is more CPU friendly)
     :param show: automatically open the UI in a browser tab (default: `True`)

+ 1 - 1
nicegui/templates/index.html

@@ -3,7 +3,7 @@
   <head>
     <title>{{ title }}</title>
     <script src="{{ prefix | safe }}/_nicegui/static/socket.io.min.js"></script>
-    <link rel="shortcut icon" href="{{ favicon_url }}" />
+    <link href="{{ favicon_url }}" rel="shortcut icon" />
     <link href="{{ prefix | safe }}/_nicegui/static/fonts.css" rel="stylesheet" type="text/css" />
     <link href="{{ prefix | safe }}/_nicegui/static/quasar.prod.css" rel="stylesheet" type="text/css" />
     <script src="{{ prefix | safe }}/_nicegui/static/tailwind.min.js"></script>