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
* support deferred props
* fixed typo
* fixed formatting on utils, set the default group to be default on deferred props and added additional tests cases
* set indentation to 2 spaces on build_deffered_props
---------
Co-authored-by: Your Name <[email protected]>
Co-authored-by: Brandon Shar <[email protected]>
Add the Inertia app to your `INSTALLED_APPS` in `settings.py`
16
+
16
17
```python
17
18
INSTALLED_APPS= [
18
19
# django apps,
@@ -22,6 +23,7 @@ INSTALLED_APPS = [
22
23
```
23
24
24
25
Add the Inertia middleware to your `MIDDLEWARE` in `settings.py`
26
+
25
27
```python
26
28
MIDDLEWARE= [
27
29
# django middleware,
@@ -36,27 +38,29 @@ Now you're all set!
36
38
37
39
### Frontend
38
40
39
-
Django specific frontend docs coming soon. For now, we recommend installing [django_vite](https://github.com/MrBin99/django-vite)
41
+
Django specific frontend docs coming soon. For now, we recommend installing [django_vite](https://github.com/MrBin99/django-vite)
40
42
and following the commits on the Django Vite [example repo](https://github.com/MrBin99/django-vite-example). Once Vite is setup with
41
43
your frontend of choice, just replace the contents of `entry.js` with [this file (example in react)](https://github.com/BrandonShar/inertia-rails-template/blob/main/app/frontend/entrypoints/application.jsx)
42
44
43
-
44
-
You can also check out the official Inertia docs at https://inertiajs.com/.
45
+
You can also check out the official Inertia docs at https://inertiajs.com/.
45
46
46
47
### CSRF
47
48
48
-
Django's CSRF tokens are tightly coupled with rendering templates so Inertia Django automatically handles adding the CSRF cookie for you to each Inertia response. Because the default names Django users for the CSRF headers don't match Axios (the Javascript request library Inertia uses), we'll need to either modify Axios's defaults OR Django's settings.
49
+
Django's CSRF tokens are tightly coupled with rendering templates so Inertia Django automatically handles adding the CSRF cookie for you to each Inertia response. Because the default names Django users for the CSRF headers don't match Axios (the Javascript request library Inertia uses), we'll need to either modify Axios's defaults OR Django's settings.
49
50
50
51
**You only need to choose one of the following options, just pick whichever makes the most sense to you!**
51
52
52
53
In your `entry.js` file
54
+
53
55
```javascript
54
-
axios.defaults.xsrfHeaderName="X-CSRFToken"
55
-
axios.defaults.xsrfCookieName="csrftoken"
56
+
axios.defaults.xsrfHeaderName="X-CSRFToken";
57
+
axios.defaults.xsrfCookieName="csrftoken";
56
58
```
59
+
57
60
OR
58
61
59
62
In your Django `settings.py` file
63
+
60
64
```python
61
65
CSRF_HEADER_NAME='HTTP_X_XSRF_TOKEN'
62
66
CSRF_COOKIE_NAME='XSRF-TOKEN'
@@ -102,7 +106,7 @@ from .models import User
102
106
103
107
definertia_share(get_response):
104
108
defmiddleware(request):
105
-
share(request,
109
+
share(request,
106
110
app_name=settings.APP_NAME,
107
111
user_count=lambda: User.objects.count(), # evaluated lazily at render time
108
112
user=lambda: request.user, # evaluated lazily at render time
It is possible to redirect to an external website, or even another non-Inertia endpoint in your app while handling an Inertia request.
121
+
It is possible to redirect to an external website, or even another non-Inertia endpoint in your app while handling an Inertia request.
118
122
This can be accomplished using a server-side initiated `window.location` visit via the `location` method:
119
123
120
124
```python
@@ -124,10 +128,11 @@ def external():
124
128
return location("http://foobar.com/")
125
129
```
126
130
127
-
It will generate a `409 Conflict` response and include the destination URL in the `X-Inertia-Location` header.
131
+
It will generate a `409 Conflict` response and include the destination URL in the `X-Inertia-Location` header.
128
132
When this response is received client-side, Inertia will automatically perform a `window.location = url` visit.
129
133
130
134
### Lazy Props
135
+
131
136
On the front end, Inertia supports the concept of "partial reloads" where only the props requested
132
137
are returned by the server. Sometimes, you may want to use this flow to avoid processing a particularly slow prop on the intial load. In this case, you can use `Lazy props`. Lazy props aren't evaluated unless they're specifically requested by name in a partial reload.
133
138
@@ -142,10 +147,46 @@ def example(request):
142
147
}
143
148
```
144
149
150
+
### Deferred Props
151
+
152
+
As of version 2.0, Inertia supports the ability to defer the fetching of props until after the page has been initially rendered. Essentially this is similar to the concept of `Lazy props` however Inertia provides convenient frontend components to automatically fetch the deferred props after the page has initially loaded, instead of requiring the user to initiate a reload. For more info, see [Deferred props](https://inertiajs.com/deferred-props) in the Inertia documentation.
153
+
154
+
To mark props as deferred on the server side use the `defer` function.
155
+
156
+
```python
157
+
from inertia import defer, inertia
158
+
159
+
@inertia('ExampleComponent')
160
+
defexample(request):
161
+
return {
162
+
'name': lambda: 'Brandon', # this will be rendered on the first load as usual
163
+
'data': defer(lambda: some_long_calculation()), # this will only be run after the frontend has initially loaded and inertia requests this prop
164
+
}
165
+
```
166
+
167
+
#### Grouping requests
168
+
169
+
By default, all deferred props get fetched in one request after the initial page is rendered, but you can choose to fetch data in parallel by grouping props together.
170
+
171
+
```python
172
+
from inertia import defer, inertia
173
+
174
+
@inertia('ExampleComponent')
175
+
defexample(request):
176
+
return {
177
+
'name': lambda: 'Brandon', # this will be rendered on the first load as usual
In the example above, the `data1`, and `data2` props will be fetched in one request, while the `data` prop will be fetched in a separate request in parallel. Group names are arbitrary strings and can be anything you choose.
185
+
145
186
### Json Encoding
146
187
147
-
Inertia Django ships with a custom JsonEncoder at `inertia.utils.InertiaJsonEncoder` that extends Django's
148
-
`DjangoJSONEncoder` with additional logic to handle encoding models and Querysets. If you have other json
188
+
Inertia Django ships with a custom JsonEncoder at `inertia.utils.InertiaJsonEncoder` that extends Django's
189
+
`DjangoJSONEncoder` with additional logic to handle encoding models and Querysets. If you have other json
149
190
encoding logic you'd prefer, you can set a new JsonEncoder via the settings.
150
191
151
192
### History Encryption
@@ -191,9 +232,11 @@ class LogoutView(auth_views.LogoutView):
191
232
### SSR
192
233
193
234
#### Backend
235
+
194
236
Enable SSR via the `INERTIA_SSR_URL` and `INERTIA_SSR_ENABLED` settings
195
237
196
238
#### Frontend
239
+
197
240
Coming Soon!
198
241
199
242
## Settings
@@ -225,22 +268,22 @@ class ExampleTestCase(InertiaTestCase):
@@ -255,6 +298,6 @@ for you to simulate an inertia response. You can access and use it just like the
255
298
256
299
A huge thank you to the community members who have worked on InertiaJS for Django before us. Parts of this repo were particularly inspired by [Andres Vargas](https://github.com/zodman) and [Samuel Girardin](https://github.com/girardinsamuel). Additional thanks to Andres for the Pypi project.
257
300
258
-
*Maintained and sponsored by the team at [bellaWatt](https://bellawatt.com/)*
301
+
_Maintained and sponsored by the team at [bellaWatt](https://bellawatt.com/)_
0 commit comments