Show a fallback when Swagger UI and ReDoc cannot load

Both pages pull their JavaScript from cdn.jsdelivr.net, which is unreachable
behind an authenticating proxy — the page looked blank with no explanation.

- override drf_spectacular/swagger_ui.html and redoc.html in project
  templates
- if the library is missing or nothing rendered after 4 seconds, show a
  short message with a link to /api/docs/ (the offline reference)
- drop the Google Fonts request from the ReDoc page (also blocked)
- note the behaviour in docs/client-links.md
This commit is contained in:
apuc committed 2026-11-22 23:37:17 +03:00
1 parent 666e2d5685
commit 50468e2384
3 files changed
+145

No files matched your search

@@ -0,0 +1,72 @@
<!DOCTYPE html>
<html lang="ru">
<head>
{% block head %}
<title>{{ title|default:"ReDoc" }}</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body { margin: 0; font: 15px/1.55 -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
color: #16212b; background: #fff; }
.api-fallback { display: none; max-width: 640px; margin: 64px auto; padding: 24px;
border: 1px solid #e6e8ec; border-radius: 12px; }
.api-fallback h1 { font-size: 20px; margin: 0 0 10px; }
.api-fallback p { color: #5b6b7a; margin: 0 0 10px; }
.api-fallback a { color: #1c4fd8; }
.api-fallback .btn { display: inline-block; margin-top: 6px; padding: 10px 18px;
background: #14161a; color: #fff; border-radius: 10px;
text-decoration: none; }
</style>
{% endblock head %}
</head>
<body>
{% block body %}
{% if settings %}
<div id="redoc-container"></div>
{% else %}
<redoc spec-url="{{ schema_url }}"></redoc>
{% endif %}
<div class="api-fallback" id="api-fallback">
<h1>ReDoc не загрузился</h1>
<p>Эта страница подгружает библиотеку с CDN, а он недоступен из вашей сети
(закрытый прокси или нет интернета).</p>
<p>Справочник по тем же методам открывается без интернета:</p>
<a class="btn" href="/api/docs/">Открыть справочник API</a>
</div>
<script src="{{ redoc_standalone }}"></script>
<script>
(function () {
function showFallback() {
var node = document.getElementById('api-fallback');
if (node) { node.style.display = 'block'; }
}
{% if settings %}
if (typeof Redoc === 'undefined') {
showFallback();
} else {
try {
Redoc.init("{{ schema_url }}", {{ settings|safe }},
document.getElementById('redoc-container'));
} catch (error) {
showFallback();
}
}
{% else %}
if (typeof Redoc === 'undefined') { showFallback(); }
{% endif %}
setTimeout(function () {
var container = document.getElementById('redoc-container');
if (!container || container.children.length === 0) {
var root = document.querySelector('redoc');
if (!root || !root.children.length) { showFallback(); }
}
}, 4000);
})();
</script>
{% endblock body %}
</body>
</html>
@@ -0,0 +1,69 @@
<!DOCTYPE html>
<html lang="ru">
<head>
{% block head %}
<title>{{ title|default:"Swagger" }}</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
{% if favicon_href %}<link rel="icon" href="{{ favicon_href }}">{% endif %}
<link rel="stylesheet" href="{{ swagger_ui_css }}">
<style>
html { box-sizing: border-box; overflow-y: scroll; }
*, *:after, *:before { box-sizing: inherit; }
body { background: #fafafa; margin: 0;
font: 15px/1.55 -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
color: #16212b; }
.api-fallback { display: none; max-width: 640px; margin: 64px auto; padding: 24px;
background: #fff; border: 1px solid #e6e8ec; border-radius: 12px; }
.api-fallback h1 { font-size: 20px; margin: 0 0 10px; }
.api-fallback p { color: #5b6b7a; margin: 0 0 10px; }
.api-fallback a { color: #1c4fd8; }
.api-fallback .btn { display: inline-block; margin-top: 6px; padding: 10px 18px;
background: #14161a; color: #fff; border-radius: 10px;
text-decoration: none; }
</style>
{% endblock head %}
</head>
<body>
{% block body %}
<div id="swagger-ui"></div>
<div class="api-fallback" id="api-fallback">
<h1>Swagger UI не загрузился</h1>
<p>Эта страница подгружает библиотеку с CDN, а он недоступен из вашей сети
(закрытый прокси или нет интернета).</p>
<p>Справочник по тем же методам открывается без интернета:</p>
<a class="btn" href="/api/docs/">Открыть справочник API</a>
</div>
<script src="{{ swagger_ui_bundle }}"></script>
<script src="{{ swagger_ui_standalone }}"></script>
{% if script_url %}
<script src="{{ script_url }}"></script>
{% else %}
<script>
{% include template_name_js %}
</script>
{% endif %}
<script>
(function () {
function showFallback() {
var node = document.getElementById('api-fallback');
if (node) { node.style.display = 'block'; }
}
if (typeof SwaggerUIBundle === 'undefined') {
showFallback();
}
setTimeout(function () {
var container = document.getElementById('swagger-ui');
if (!container || !container.querySelector('.swagger-ui')) {
showFallback();
}
}, 4000);
})();
</script>
{% endblock %}
</body>
</html>
+4
View File
@@ -56,6 +56,10 @@ cd /home/apuc/homm/backend && ../.venv/bin/python manage.py createsuperuser
| https://homm.my-dpr.ru/api/schema/ | Схема OpenAPI в YAML — для генерации клиента | | https://homm.my-dpr.ru/api/schema/ | Схема OpenAPI в YAML — для генерации клиента |
| https://homm.my-dpr.ru/api/v1/health/ | Проверка живости сайта и базы | | https://homm.my-dpr.ru/api/v1/health/ | Проверка живости сайта и базы |
`/api/swagger/` и `/api/redoc/` подгружают библиотеки с CDN. В сети без доступа
к `cdn.jsdelivr.net` они показывают заглушку со ссылкой на `/api/docs/` —
рабочий вариант без интернета.
## Для 1С-программиста ## Для 1С-программиста
Базовый адрес: `https://homm.my-dpr.ru/api/v1/integration/1c/` Базовый адрес: `https://homm.my-dpr.ru/api/v1/integration/1c/`