Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move requests (required for SSR only) to an extra #73

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- uses: astral-sh/[email protected]
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
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 8 additions & 1 deletion inertia/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"

Expand Down
3 changes: 1 addition & 2 deletions inertia/tests/test_ssr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down
Loading