diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 65957a9..2e31503 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,7 +18,7 @@ jobs: - uses: astral-sh/setup-uv@v5.4.0 with: python-version: '3.12' - - run: uvx poetry install + - run: uvx poetry install --extras=ssr - run: uvx poetry run pytest -v -ra --cov --cov-report=term-missing env: COVERAGE_CORE: sysmon diff --git a/README.md b/README.md index 676c7a9..1174148 100644 --- a/README.md +++ b/README.md @@ -297,11 +297,14 @@ class LogoutView(auth_views.LogoutView): return response ``` -### SSR +### SSR #### Backend -Enable SSR via the `INERTIA_SSR_URL` and `INERTIA_SSR_ENABLED` settings +* Ensure `requests` is installed, so inertia-django can do SSR requests. + * `requests` is configured as a dependency if you install the `[ssr]` extra, + e.g. `inertia-django[ssr]` in your requirements. +* Enable SSR via the `INERTIA_SSR_URL` and `INERTIA_SSR_ENABLED` settings. #### Frontend diff --git a/inertia/http.py b/inertia/http.py index 7096341..9ddf96f 100644 --- a/inertia/http.py +++ b/inertia/http.py @@ -2,7 +2,6 @@ from http import HTTPStatus from json import dumps as json_encode -import requests from django.core.exceptions import ImproperlyConfigured from django.http import HttpResponse from django.template.loader import render_to_string @@ -11,6 +10,14 @@ from .prop_classes import DeferredProp, IgnoreOnFirstLoadProp, MergeableProp from .settings import settings +try: + # Must be early-imported so tests can patch it with + # a mock module + import requests +except ImportError: + requests = None + + INERTIA_REQUEST_ENCRYPT_HISTORY = "_inertia_encrypt_history" INERTIA_SESSION_CLEAR_HISTORY = "_inertia_clear_history" diff --git a/inertia/tests/test_ssr.py b/inertia/tests/test_ssr.py index b9927e9..f2897f7 100644 --- a/inertia/tests/test_ssr.py +++ b/inertia/tests/test_ssr.py @@ -2,7 +2,6 @@ from unittest.mock import Mock, patch from django.test import override_settings -from requests.exceptions import RequestException from inertia.test import InertiaTestCase, inertia_div, inertia_page @@ -66,7 +65,7 @@ def test_it_uses_inertia_if_inertia_requests_are_made(self, mock_requests): @patch("inertia.http.requests") def test_it_fallsback_on_failure(self, mock_requests): def uh_oh(*args, **kwargs): - raise RequestException() + raise ValueError() # all exceptions are caught and ignored mock_response = Mock() mock_response.raise_for_status.side_effect = uh_oh diff --git a/pyproject.toml b/pyproject.toml index 52a001f..2c14a7a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,9 +22,12 @@ classifiers = [ ] dependencies = [ "django>=4", - "requests>=2", ] +[project.optional-dependencies] +# Requests is requires for server-side rendering. +ssr = ["requests>=2"] + [tool.poetry] packages = [ { include = "inertia" },