vue_ssr

 1from .vue_ssr import ServerRenderer, SocketServerRenderer, ViteRenderer
 2
 3__all__ = [
 4    "ServerRenderer",
 5    "SocketServerRenderer",
 6    "ViteRenderer",
 7    "settings",
 8    "apps",
 9    "templatetags",
10]
class ServerRenderer(vue_ssr.vue_ssr.SSRRenderer):
25class ServerRenderer(SSRRenderer):
26    """Connect to a vue-ssr-service server via HTTP."""
27
28    host: Optional[str]
29    port: Optional[str]
30    protocol: Optional[str]
31
32    _session: requests.Session
33
34    def __init__(
35        self,
36        host: str = "127.0.0.1",
37        port: str | int = 3123,
38        protocol: str = "http",
39    ):
40        """
41        :param host: The server host.
42        :param port: The server port.
43        :param protocol: The protocol (http or https).
44        """
45
46        self._session = requests.Session()
47        self.host = host
48        self.port = str(port)
49        self.protocol = protocol
50
51    @cached_property
52    def address(self) -> str:
53        return f"{self.protocol}://{self.host}:{self.port}"
54
55    def render(
56        self,
57        entry: str,
58        props: dict[str, Any] = {},
59        timeout: float | None = None,
60    ) -> str:
61        """
62        Render the given entry with the provided props.
63        :param entry: The name of the SSR entry.
64        :param props: The props passed to the entry.
65        :return: The rendered HTML.
66        """
67        url = f"{self.address}/render"
68        data = {"entryName": entry, "props": props}
69        response = self._session.post(url, json=data, timeout=timeout)
70
71        if response.status_code == 200:
72            return response.text
73        else:
74            raise Exception(f"SSR error: {response.status_code} - {response.text}")

Connect to a vue-ssr-service server via HTTP.

ServerRenderer( host: str = '127.0.0.1', port: str | int = 3123, protocol: str = 'http')
34    def __init__(
35        self,
36        host: str = "127.0.0.1",
37        port: str | int = 3123,
38        protocol: str = "http",
39    ):
40        """
41        :param host: The server host.
42        :param port: The server port.
43        :param protocol: The protocol (http or https).
44        """
45
46        self._session = requests.Session()
47        self.host = host
48        self.port = str(port)
49        self.protocol = protocol
Parameters
  • host: The server host.
  • port: The server port.
  • protocol: The protocol (http or https).
host: Optional[str]
port: Optional[str]
protocol: Optional[str]
address: str
51    @cached_property
52    def address(self) -> str:
53        return f"{self.protocol}://{self.host}:{self.port}"
def render( self, entry: str, props: dict[str, typing.Any] = {}, timeout: float | None = None) -> str:
55    def render(
56        self,
57        entry: str,
58        props: dict[str, Any] = {},
59        timeout: float | None = None,
60    ) -> str:
61        """
62        Render the given entry with the provided props.
63        :param entry: The name of the SSR entry.
64        :param props: The props passed to the entry.
65        :return: The rendered HTML.
66        """
67        url = f"{self.address}/render"
68        data = {"entryName": entry, "props": props}
69        response = self._session.post(url, json=data, timeout=timeout)
70
71        if response.status_code == 200:
72            return response.text
73        else:
74            raise Exception(f"SSR error: {response.status_code} - {response.text}")

Render the given entry with the provided props.

Parameters
  • entry: The name of the SSR entry.
  • props: The props passed to the entry.
Returns

The rendered HTML.

class SocketServerRenderer(vue_ssr.ServerRenderer):
77class SocketServerRenderer(ServerRenderer):
78    """Connect to a vue-ssr-service server via a UNIX socket."""
79
80    def __init__(self, socket: PathLike | str):
81        """
82        :param socket: The path to the Unix socket.
83        """
84        self.unix_socket = str(socket)
85        self._session = requests_unixsocket.Session()
86
87    @cached_property
88    def address(self) -> str:
89        return f"http+unix://{quote(self.unix_socket, safe='')}"

Connect to a vue-ssr-service server via a UNIX socket.

SocketServerRenderer(socket: os.PathLike | str)
80    def __init__(self, socket: PathLike | str):
81        """
82        :param socket: The path to the Unix socket.
83        """
84        self.unix_socket = str(socket)
85        self._session = requests_unixsocket.Session()
Parameters
  • socket: The path to the Unix socket.
unix_socket
address: str
87    @cached_property
88    def address(self) -> str:
89        return f"http+unix://{quote(self.unix_socket, safe='')}"
class ViteRenderer(vue_ssr.vue_ssr.SSRRenderer):
 92class ViteRenderer(SSRRenderer):
 93    """Connect to a Vite dev server via HTTP."""
 94
 95    host: str
 96    port: str
 97    protocol: str
 98
 99    _session = requests.Session()
100
101    def __init__(
102        self, host: str = "127.0.0.1", port: int | str = "5173", protocol: str = "http"
103    ) -> None:
104        """
105        :param host: The Vite dev server host.
106        :param port: The Vite dev server port.
107        :param protocol: The protocol (http or https).
108        """
109        self.host = host
110        self.port = str(port)
111        self.protocol = protocol
112
113    @cached_property
114    def address(self) -> str:
115        return f"{self.protocol}://{self.host}:{self.port}"
116
117    def render(
118        self,
119        entry: str,
120        props: dict[str, Any] = {},
121        timeout: float | None = None,
122    ) -> str:
123        """
124        Render the given entry with the provided props.
125        :param entry: The name of the SSR entry.
126        :param props: The props passed to the entry.
127        :return: The rendered HTML.
128        """
129        url = f"{self.address}/__vue-ssr"
130        data = {"entryName": entry, "props": props}
131        response = self._session.post(url, json=data, timeout=timeout)
132
133        if response.status_code == 200:
134            return response.text
135        else:
136            raise Exception(f"SSR error: {response.status_code} - {response.text}")

Connect to a Vite dev server via HTTP.

ViteRenderer( host: str = '127.0.0.1', port: int | str = '5173', protocol: str = 'http')
101    def __init__(
102        self, host: str = "127.0.0.1", port: int | str = "5173", protocol: str = "http"
103    ) -> None:
104        """
105        :param host: The Vite dev server host.
106        :param port: The Vite dev server port.
107        :param protocol: The protocol (http or https).
108        """
109        self.host = host
110        self.port = str(port)
111        self.protocol = protocol
Parameters
  • host: The Vite dev server host.
  • port: The Vite dev server port.
  • protocol: The protocol (http or https).
host: str
port: str
protocol: str
address: str
113    @cached_property
114    def address(self) -> str:
115        return f"{self.protocol}://{self.host}:{self.port}"
def render( self, entry: str, props: dict[str, typing.Any] = {}, timeout: float | None = None) -> str:
117    def render(
118        self,
119        entry: str,
120        props: dict[str, Any] = {},
121        timeout: float | None = None,
122    ) -> str:
123        """
124        Render the given entry with the provided props.
125        :param entry: The name of the SSR entry.
126        :param props: The props passed to the entry.
127        :return: The rendered HTML.
128        """
129        url = f"{self.address}/__vue-ssr"
130        data = {"entryName": entry, "props": props}
131        response = self._session.post(url, json=data, timeout=timeout)
132
133        if response.status_code == 200:
134            return response.text
135        else:
136            raise Exception(f"SSR error: {response.status_code} - {response.text}")

Render the given entry with the provided props.

Parameters
  • entry: The name of the SSR entry.
  • props: The props passed to the entry.
Returns

The rendered HTML.