You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Allow users to modify component's host URLs (#172)
### Added
- **Distributed Computing:** ReactPy components can now optionally be rendered by a completely separate server!
- `REACTPY_DEFAULT_HOSTS` setting can round-robin a list of ReactPy rendering hosts.
- `host` argument has been added to the `component` template tag to force components to render on a specific host.
- `reactpy_django.utils.register_component` function to manually register root components.
- Useful if you have dedicated ReactPy rendering application(s) that do not use HTML templates.
### Changed
- ReactPy will now provide a warning if your HTTP URLs are not on the same prefix as your websockets.
- Cleaner logging output for detected ReactPy root components.
### Deprecated
- `reactpy_django.REACTPY_WEBSOCKET_PATH` is deprecated. The similar replacement is `REACTPY_WEBSOCKET_ROUTE`.
- `settings.py:REACTPY_WEBSOCKET_URL` is deprecated. The similar replacement is `REACTPY_URL_PREFIX`.
### Removed
- Warning W007 (`REACTPY_WEBSOCKET_URL doesn't end with a slash`) has been removed. ReactPy now automatically handles slashes.
- Warning W008 (`REACTPY_WEBSOCKET_URL doesn't start with an alphanumeric character`) has been removed. ReactPy now automatically handles this scenario.
- Error E009 (`channels is not in settings.py:INSTALLED_APPS`) has been removed. Newer versions of `channels` do not require installation via `INSTALLED_APPS` to receive an ASGI webserver.
Copy file name to clipboardExpand all lines: CHANGELOG.md
+23-1
Original file line number
Diff line number
Diff line change
@@ -34,7 +34,29 @@ Using the following categories, list your changes in this order:
34
34
35
35
## [Unreleased]
36
36
37
-
- Nothing (yet)!
37
+
### Added
38
+
39
+
-**Distributed Computing:** ReactPy components can now optionally be rendered by a completely separate server!
40
+
-`REACTPY_DEFAULT_HOSTS` setting can round-robin a list of ReactPy rendering hosts.
41
+
-`host` argument has been added to the `component` template tag to force components to render on a specific host.
42
+
-`reactpy_django.utils.register_component` function to manually register root components.
43
+
- Useful if you have dedicated ReactPy rendering application(s) that do not use HTML templates.
44
+
45
+
### Changed
46
+
47
+
- ReactPy will now provide a warning if your HTTP URLs are not on the same prefix as your websockets.
48
+
- Cleaner logging output for auto-detected ReactPy root components.
49
+
50
+
### Deprecated
51
+
52
+
-`reactpy_django.REACTPY_WEBSOCKET_PATH` is deprecated. The identical replacement is `REACTPY_WEBSOCKET_ROUTE`.
53
+
-`settings.py:REACTPY_WEBSOCKET_URL` is deprecated. The similar replacement is `REACTPY_URL_PREFIX`.
54
+
55
+
### Removed
56
+
57
+
- Warning W007 (`REACTPY_WEBSOCKET_URL doesn't end with a slash`) has been removed. ReactPy now automatically handles slashes.
58
+
- Warning W008 (`REACTPY_WEBSOCKET_URL doesn't start with an alphanumeric character`) has been removed. ReactPy now automatically handles this scenario.
59
+
- Error E009 (`channels is not in settings.py:INSTALLED_APPS`) has been removed. Newer versions of `channels` do not require installation via `INSTALLED_APPS` to receive an ASGI webserver.
Copy file name to clipboardExpand all lines: docs/src/features/components.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -37,7 +37,7 @@ Convert any Django view into a ReactPy component by using this decorator. Compat
37
37
38
38
It is your responsibility to ensure privileged information is not leaked via this method.
39
39
40
-
This can be done via directly writing conditionals into your view, or by adding decorators such as [`user_passes_test`](https://docs.djangoproject.com/en/dev/topics/auth/default/#django.contrib.auth.decorators.user_passes_test) to your views prior to using `view_to_component`.
40
+
You must implement a method to ensure only authorized users can access your view. This can be done via directly writing conditionals into your view, or by adding decorators such as [`user_passes_test`](https://docs.djangoproject.com/en/dev/topics/auth/default/#django.contrib.auth.decorators.user_passes_test) to your views. For example...
Copy file name to clipboardExpand all lines: docs/src/features/settings.md
+14-5
Original file line number
Diff line number
Diff line change
@@ -6,16 +6,25 @@
6
6
7
7
## Primary Configuration
8
8
9
+
<!--config-details-start-->
10
+
9
11
These are ReactPy-Django's default settings values. You can modify these values in your **Django project's**`settings.py` to change the behavior of ReactPy.
10
12
11
-
=== "settings.py"
13
+
| Setting | Default Value | Example Value(s) | Description |
14
+
| --- | --- | --- | --- |
15
+
|`REACTPY_CACHE`|`#!python "default"`|`#!python "my-reactpy-cache"`| Cache used to store ReactPy web modules. ReactPy benefits from a fast, well indexed cache.<br/>We recommend installing [`redis`](https://redis.io/) or [`python-diskcache`](https://grantjenks.com/docs/diskcache/tutorial.html#djangocache). |
16
+
|`REACTPY_DATABASE`|`#!python "default"`|`#!python "my-reactpy-database"`| Database ReactPy uses to store session data. ReactPy requires a multiprocessing-safe and thread-safe database.<br/>If configuring `REACTPY_DATABASE`, it is mandatory to also configure `DATABASE_ROUTERS` like such:<br/>`#!python DATABASE_ROUTERS = ["reactpy_django.database.Router", ...]`|
17
+
|`REACTPY_RECONNECT_MAX`|`#!python 259200`|`#!python 96000`, `#!python 60`, `#!python 0`| Maximum seconds between reconnection attempts before giving up.<br/>Use `#!python 0` to prevent reconnection. |
18
+
|`REACTPY_URL_PREFIX`|`#!python "reactpy/"`|`#!python "rp/"`, `#!python "render/reactpy/"`| The prefix to be used for all ReactPy websocket and HTTP URLs. |
19
+
|`REACTPY_DEFAULT_QUERY_POSTPROCESSOR`|`#!python "reactpy_django.utils.django_query_postprocessor"`|`#!python "example_project.my_query_postprocessor"`| Dotted path to the default `reactpy_django.hooks.use_query` postprocessor function. |
20
+
|`REACTPY_AUTH_BACKEND`|`#!python "django.contrib.auth.backends.ModelBackend"`|`#!python "example_project.auth.MyModelBackend"`| Dotted path to the Django authentication backend to use for ReactPy components. This is only needed if:<br/> 1. You are using `AuthMiddlewareStack` and...<br/> 2. You are using Django's `AUTHENTICATION_BACKENDS` setting and...<br/> 3. Your Django user model does not define a `backend` attribute. |
21
+
|`REACTPY_BACKHAUL_THREAD`|`#!python False`|`#!python True`| Whether to render ReactPy components in a dedicated thread. This allows the webserver to process web traffic while during ReactPy rendering.<br/>Vastly improves throughput with web servers such as `hypercorn` and `uvicorn`. |
22
+
|`REACTPY_DEFAULT_HOSTS`|`#!python None`|`#!python ["localhost:8000", "localhost:8001", "localhost:8002/subdir" ]`| Default host(s) to use for ReactPy components. ReactPy will use these hosts in a round-robin fashion, allowing for easy distributed computing.<br/>You can use the `host` argument in your [template tag](../features/template-tag.md#component) to override this default. |
12
23
13
-
```python
14
-
{% include "../../python/settings.py" %}
15
-
```
24
+
<!--config-details-end-->
16
25
17
26
??? question "Do I need to modify my settings?"
18
27
19
-
The default configuration of ReactPy is adequate for the majority of use cases.
28
+
The default configuration of ReactPy is suitable for the majority of use cases.
20
29
21
30
You should only consider changing settings when the necessity arises.
Copy file name to clipboardExpand all lines: docs/src/features/template-tag.md
+22-1
Original file line number
Diff line number
Diff line change
@@ -20,6 +20,7 @@ The `component` template tag can be used to insert any number of ReactPy compone
20
20
| --- | --- | --- | --- |
21
21
| `dotted_path` | `str` | The dotted path to the component to render. | N/A |
22
22
| `*args` | `Any` | The positional arguments to provide to the component. | N/A |
23
+
| `host` | `str | None` | The host to use for the ReactPy connections. If set to `None`, the host will be automatically configured.<br/>Example values include: `localhost:8000`, `example.com`, `example.com/subdir` | `None` |
23
24
| `**kwargs` | `Any` | The keyword arguments to provide to the component. | N/A |
24
25
25
26
<font size="4">**Returns**</font>
@@ -73,6 +74,27 @@ The `component` template tag can be used to insert any number of ReactPy compone
73
74
```
74
75
75
76
<!--reserved-sarg-end-->
77
+
78
+
??? question "Can I render components on a different server (distributed computing)?"
79
+
80
+
Yes! By using the `host` keyword argument, you can render components from a completely separate ASGI server.
This configuration most commonly involves you deploying multiple instances of your project. But, you can also create dedicated Django project(s) that only render specific ReactPy components if you wish.
91
+
92
+
Here's a couple of things to keep in mind:
93
+
94
+
1. If your host address are completely separate ( `origin1.com != origin2.com` ) you will need to [configure CORS headers](https://pypi.org/project/django-cors-headers/) on your main application during deployment.
95
+
2. You will not need to register ReactPy HTTP or websocket paths on any applications that do not perform any component rendering.
96
+
3. Your component will only be able to access `*args`/`**kwargs` you provide to the template tag if your applications share a common database.
97
+
76
98
<!--multiple-components-start-->
77
99
78
100
??? question "Can I use multiple components on one page?"
@@ -98,7 +120,6 @@ The `component` template tag can be used to insert any number of ReactPy compone
98
120
Additionally, in scenarios where you are trying to create a Single Page Application (SPA) within Django, you will only have one component within your `#!html <body>` tag.
99
121
100
122
<!--multiple-components-end-->
101
-
102
123
<!--args-kwargs-start-->
103
124
104
125
??? question "Can I use positional arguments instead of keyword arguments?"
Copy file name to clipboardExpand all lines: docs/src/features/utils.md
+22
Original file line number
Diff line number
Diff line change
@@ -37,3 +37,25 @@ This postprocessor is designed to avoid Django's `SynchronousOnlyException` by r
37
37
| Type | Description |
38
38
| --- | --- |
39
39
| `QuerySet | Model` | The `Model` or `QuerySet` with all fields fetched. |
40
+
41
+
## Register Component
42
+
43
+
The `register_component` function is used manually register a root component with ReactPy.
44
+
45
+
You should always call `register_component` within a Django [`AppConfig.ready()` method](https://docs.djangoproject.com/en/4.2/ref/applications/#django.apps.AppConfig.ready) to retain compatibility with ASGI webserver workers.
46
+
47
+
=== "apps.py"
48
+
49
+
```python
50
+
{% include "../../python/register-component.py" %}
51
+
```
52
+
53
+
??? question "Do I need to register my components?"
54
+
55
+
You typically will not need to use this function.
56
+
57
+
For security reasons, ReactPy does not allow non-registered components to be root components. However, all components contained within Django templates are automatically considered root components.
58
+
59
+
You only need to use this function if your host application does not contain any HTML templates that [reference](../features/template-tag.md#component) your components.
60
+
61
+
A common scenario where this is needed is when you are modifying the [template tag `host = ...` argument](../features/template-tag.md#component) in order to configure a dedicated Django application as a rendering server for ReactPy. On this dedicated rendering server, you would need to manually register your components.
0 commit comments