|
4 | 4 | import os
|
5 | 5 | from dataclasses import dataclass
|
6 | 6 | from pathlib import Path, PurePosixPath
|
7 |
| -from typing import Any, Awaitable |
| 7 | +from typing import Any, Awaitable, Sequence |
8 | 8 |
|
9 | 9 | from asgiref.typing import ASGIApplication
|
10 | 10 | from uvicorn.config import Config as UvicornConfig
|
@@ -91,26 +91,40 @@ def read_client_index_html(options: CommonOptions) -> str:
|
91 | 91 | return (
|
92 | 92 | (CLIENT_BUILD_DIR / "index.html")
|
93 | 93 | .read_text()
|
94 |
| - .format(__head__=vdom_to_html(options.head)) |
| 94 | + .format(__head__=vdom_head_elements_to_html(options.head)) |
95 | 95 | )
|
96 | 96 |
|
97 | 97 |
|
| 98 | +def vdom_head_elements_to_html(head: Sequence[VdomDict] | VdomDict | str) -> str: |
| 99 | + if isinstance(head, str): |
| 100 | + return head |
| 101 | + elif isinstance(head, dict): |
| 102 | + if head.get("tagName") == "head": |
| 103 | + head = {**head, "tagName": ""} |
| 104 | + return vdom_to_html(head) |
| 105 | + else: |
| 106 | + return vdom_to_html(html._(head)) |
| 107 | + |
| 108 | + |
98 | 109 | @dataclass
|
99 | 110 | class CommonOptions:
|
100 | 111 | """Options for IDOM's built-in backed server implementations"""
|
101 | 112 |
|
102 |
| - head: VdomDict | str = vdom_to_html( |
103 |
| - html.head( |
104 |
| - html.title("IDOM"), |
105 |
| - html.link( |
106 |
| - { |
107 |
| - "rel": "icon", |
108 |
| - "href": "_idom/assets/idom-logo-square-small.svg", |
109 |
| - "type": "image/svg+xml", |
110 |
| - } |
111 |
| - ), |
112 |
| - ) |
| 113 | + head: Sequence[VdomDict] | VdomDict | str = ( |
| 114 | + html.title("IDOM"), |
| 115 | + html.link( |
| 116 | + { |
| 117 | + "rel": "icon", |
| 118 | + "href": "_idom/assets/idom-logo-square-small.svg", |
| 119 | + "type": "image/svg+xml", |
| 120 | + } |
| 121 | + ), |
113 | 122 | )
|
| 123 | + """Add elements to the ``<head>`` of the application. |
| 124 | +
|
| 125 | + For example, this can be used to customize the title of the page, link extra |
| 126 | + scripts, or load stylesheets. |
| 127 | + """ |
114 | 128 |
|
115 | 129 | url_prefix: str = ""
|
116 | 130 | """The URL prefix where IDOM resources will be served from"""
|
0 commit comments