Skip to content

Commit 13625d2

Browse files
authored
Move requests (required for SSR only) to an extra (#73)
1 parent d88a140 commit 13625d2

File tree

5 files changed

+19
-7
lines changed

5 files changed

+19
-7
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- uses: astral-sh/[email protected]
1919
with:
2020
python-version: '3.12'
21-
- run: uvx poetry install
21+
- run: uvx poetry install --extras=ssr
2222
- run: uvx poetry run pytest -v -ra --cov --cov-report=term-missing
2323
env:
2424
COVERAGE_CORE: sysmon

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,14 @@ class LogoutView(auth_views.LogoutView):
297297
return response
298298
```
299299

300-
### SSR
300+
### SSR
301301

302302
#### Backend
303303

304-
Enable SSR via the `INERTIA_SSR_URL` and `INERTIA_SSR_ENABLED` settings
304+
* Ensure `requests` is installed, so inertia-django can do SSR requests.
305+
* `requests` is configured as a dependency if you install the `[ssr]` extra,
306+
e.g. `inertia-django[ssr]` in your requirements.
307+
* Enable SSR via the `INERTIA_SSR_URL` and `INERTIA_SSR_ENABLED` settings.
305308

306309
#### Frontend
307310

inertia/http.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from http import HTTPStatus
33
from json import dumps as json_encode
44

5-
import requests
65
from django.core.exceptions import ImproperlyConfigured
76
from django.http import HttpResponse
87
from django.template.loader import render_to_string
@@ -11,6 +10,14 @@
1110
from .prop_classes import DeferredProp, IgnoreOnFirstLoadProp, MergeableProp
1211
from .settings import settings
1312

13+
try:
14+
# Must be early-imported so tests can patch it with
15+
# a mock module
16+
import requests
17+
except ImportError:
18+
requests = None
19+
20+
1421
INERTIA_REQUEST_ENCRYPT_HISTORY = "_inertia_encrypt_history"
1522
INERTIA_SESSION_CLEAR_HISTORY = "_inertia_clear_history"
1623

inertia/tests/test_ssr.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from unittest.mock import Mock, patch
33

44
from django.test import override_settings
5-
from requests.exceptions import RequestException
65

76
from inertia.test import InertiaTestCase, inertia_div, inertia_page
87

@@ -66,7 +65,7 @@ def test_it_uses_inertia_if_inertia_requests_are_made(self, mock_requests):
6665
@patch("inertia.http.requests")
6766
def test_it_fallsback_on_failure(self, mock_requests):
6867
def uh_oh(*args, **kwargs):
69-
raise RequestException()
68+
raise ValueError() # all exceptions are caught and ignored
7069

7170
mock_response = Mock()
7271
mock_response.raise_for_status.side_effect = uh_oh

pyproject.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ classifiers = [
2222
]
2323
dependencies = [
2424
"django>=4",
25-
"requests>=2",
2625
]
2726

27+
[project.optional-dependencies]
28+
# Requests is requires for server-side rendering.
29+
ssr = ["requests>=2"]
30+
2831
[tool.poetry]
2932
packages = [
3033
{ include = "inertia" },

0 commit comments

Comments
 (0)