Skip to content

Commit 36e2974

Browse files
authored
V2: Deprecate lazy add optional (#59)
* have actions run on PRs * deprecate lazy and add optional
1 parent 385b700 commit 36e2974

File tree

8 files changed

+63
-14
lines changed

8 files changed

+63
-14
lines changed

.github/workflows/pytest.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
name: Unit tests
22

3-
on: [push]
3+
on:
4+
push:
5+
branches:
6+
- main
7+
paths-ignore:
8+
- '**.md'
9+
pull_request:
10+
paths-ignore:
11+
- '**.md'
412

513
jobs:
614
unit-test:

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,7 @@ cython_debug/
160160
#.idea/
161161

162162
# because this is a package, we don't want to commit the lock file
163-
poetry.lock
163+
poetry.lock
164+
165+
.DS_Store
166+
.tool-versions

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,25 +131,25 @@ def external():
131131
It will generate a `409 Conflict` response and include the destination URL in the `X-Inertia-Location` header.
132132
When this response is received client-side, Inertia will automatically perform a `window.location = url` visit.
133133

134-
### Lazy Props
134+
### Optional Props
135135

136136
On the front end, Inertia supports the concept of "partial reloads" where only the props requested
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.
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 `Optional props`. Optional props aren't evaluated unless they're specifically requested by name in a partial reload.
138138

139139
```python
140-
from inertia import lazy, inertia
140+
from inertia import optional, inertia
141141

142142
@inertia('ExampleComponent')
143143
def example(request):
144144
return {
145145
'name': lambda: 'Brandon', # this will be rendered on the first load as usual
146-
'data': lazy(lambda: some_long_calculation()), # this will only be run when specifically requested by partial props and WILL NOT be included on the initial load
146+
'data': optional(lambda: some_long_calculation()), # this will only be run when specifically requested by partial props and WILL NOT be included on the initial load
147147
}
148148
```
149149

150150
### Deferred Props
151151

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.
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 `Optional 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.
153153

154154
To mark props as deferred on the server side use the `defer` function.
155155

inertia/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from .http import inertia, render, location
2-
from .utils import lazy, defer
2+
from .utils import lazy, optional, defer
33
from .share import share

inertia/tests/test_rendering.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from inertia.test import InertiaTestCase, inertia_div, inertia_page
2+
from pytest import warns
23

34
class FirstLoadTestCase(InertiaTestCase):
45
def test_with_props(self):
@@ -74,15 +75,30 @@ def test_redirects_from_inertia_views(self):
7475

7576
class LazyPropsTestCase(InertiaTestCase):
7677
def test_lazy_props_are_not_included(self):
78+
with warns(DeprecationWarning):
79+
self.assertJSONResponse(
80+
self.inertia.get('/lazy/'),
81+
inertia_page('lazy', props={'name': 'Brian'})
82+
)
83+
84+
def test_lazy_props_are_included_when_requested(self):
85+
with warns(DeprecationWarning):
86+
self.assertJSONResponse(
87+
self.inertia.get('/lazy/', HTTP_X_INERTIA_PARTIAL_DATA='sport,grit', HTTP_X_INERTIA_PARTIAL_COMPONENT='TestComponent'),
88+
inertia_page('lazy', props={'sport': 'Basketball', 'grit': 'intense'})
89+
)
90+
91+
class OptionalPropsTestCase(InertiaTestCase):
92+
def test_optional_props_are_not_included(self):
7793
self.assertJSONResponse(
78-
self.inertia.get('/lazy/'),
79-
inertia_page('lazy', props={'name': 'Brian'})
94+
self.inertia.get('/optional/'),
95+
inertia_page('optional', props={'name': 'Brian'})
8096
)
8197

82-
def test_lazy_props_are_included_when_requested(self):
98+
def test_optional_props_are_included_when_requested(self):
8399
self.assertJSONResponse(
84-
self.inertia.get('/lazy/', HTTP_X_INERTIA_PARTIAL_DATA='sport,grit', HTTP_X_INERTIA_PARTIAL_COMPONENT='TestComponent'),
85-
inertia_page('lazy', props={'sport': 'Basketball', 'grit': 'intense'})
100+
self.inertia.get('/optional/', HTTP_X_INERTIA_PARTIAL_DATA='sport,grit', HTTP_X_INERTIA_PARTIAL_COMPONENT='TestComponent'),
101+
inertia_page('optional', props={'sport': 'Basketball', 'grit': 'intense'})
86102
)
87103

88104
class ComplexPropsTestCase(InertiaTestCase):

inertia/tests/testapp/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
path('props/', views.props_test),
99
path('template_data/', views.template_data_test),
1010
path('lazy/', views.lazy_test),
11+
path('optional/', views.optional_test),
1112
path('defer/', views.defer_test),
1213
path('defer-group/', views.defer_group_test),
1314
path('complex-props/', views.complex_props_test),

inertia/tests/testapp/views.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from django.http.response import HttpResponse
22
from django.shortcuts import redirect
33
from django.utils.decorators import decorator_from_middleware
4-
from inertia import inertia, render, lazy, defer, share, location
4+
from inertia import inertia, render, lazy, optional, defer, share, location
55
from inertia.http import INERTIA_SESSION_CLEAR_HISTORY, clear_history, encrypt_history
66

77
class ShareMiddleware:
@@ -52,6 +52,14 @@ def lazy_test(request):
5252
'grit': lazy(lambda: 'intense'),
5353
}
5454

55+
@inertia('TestComponent')
56+
def optional_test(request):
57+
return {
58+
'name': 'Brian',
59+
'sport': optional(lambda: 'Basketball'),
60+
'grit': optional(lambda: 'intense'),
61+
}
62+
5563
@inertia('TestComponent')
5664
def defer_test(request):
5765
return {

inertia/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from django.db import models
33
from django.db.models.query import QuerySet
44
from django.forms.models import model_to_dict as base_model_to_dict
5+
import warnings
56

67
def model_to_dict(model):
78
return base_model_to_dict(model, exclude=('password',))
@@ -18,10 +19,19 @@ def default(self, value):
1819

1920
class LazyProp:
2021
def __init__(self, prop):
22+
warnings.warn(
23+
"lazy and LazyProp are deprecated and will be removed in a future version. Please use optional instead.",
24+
DeprecationWarning,
25+
stacklevel=2
26+
)
2127
self.prop = prop
2228

2329
def __call__(self):
2430
return self.prop() if callable(self.prop) else self.prop
31+
32+
class OptionalProp(LazyProp):
33+
def __init__(self, prop):
34+
self.prop = prop
2535

2636
class DeferredProp:
2737
def __init__(self, prop, group):
@@ -34,5 +44,8 @@ def __call__(self):
3444
def lazy(prop):
3545
return LazyProp(prop)
3646

47+
def optional(prop):
48+
return OptionalProp(prop)
49+
3750
def defer(prop, group="default"):
3851
return DeferredProp(prop, group)

0 commit comments

Comments
 (0)