Skip to content

Commit 1378bad

Browse files
committed
Add an option to desactive browser cache for openapi files
1 parent ee2c425 commit 1378bad

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

demo-mkdocs/docs/demo/nocache.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Disable browser cache.
2+
3+
## Markdown
4+
5+
```html
6+
<swagger-ui nocache src="./openapi-spec/sample.yaml"/>
7+
<swagger-ui nocache src="https://petstore.swagger.io/v2/swagger.json"/>
8+
```
9+
10+
## Swagger UI
11+
12+
<swagger-ui nocache src="./openapi-spec/sample.yaml"/>
13+
<swagger-ui nocache src="https://petstore.swagger.io/v2/swagger.json"/>

mkdocs_swagger_ui_tag/plugin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ def render_template(openapi_spec_url, swagger_ui_ele):
177177
css_dir=css_dir,
178178
extra_css_files=extra_css_files,
179179
js_dir=js_dir,
180+
nocache=swagger_ui_ele.has_attr("nocache"),
180181
background=self.config["background"],
181182
id="{{ID_PLACEHOLDER}}", # ID is unknown yet - it's the hash of the content.
182183
openapi_spec_url=openapi_spec_url,

mkdocs_swagger_ui_tag/swagger-ui/swagger.html

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,25 @@
1717
<script src="{{ js_dir }}swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
1818
<script>
1919
window.onload = function () {
20+
{% if openapi_spec_url is string %}
21+
var url = "{{openapi_spec_url}}";
22+
{% if nocache %}
23+
url += (url.includes('?') ? '&' : '?') + "nocache=" + new Date().getTime();
24+
{% endif %}
25+
{% elif openapi_spec_url is sequence %}
26+
var urls = [
27+
{% for item in openapi_spec_url %}
28+
{url:"{{item.url}}", name:"{{item.name}}"},
29+
{% endfor %}
30+
];
31+
{% if nocache %}
32+
urls = urls.map(function(item){
33+
var url = item.url;
34+
url += (url.includes('?') ? '&' : '?') + "nocache=" + new Date().getTime();
35+
return {url: url, name: item.name};
36+
});
37+
{% endif %}
38+
{% endif %}
2039
window.ui = SwaggerUIBundle({
2140
dom_id: "#swagger-ui",
2241
onComplete: onComplete,
@@ -25,13 +44,9 @@
2544
SwaggerUIStandalonePreset
2645
],
2746
{% if openapi_spec_url is string %}
28-
url: "{{openapi_spec_url}}",
47+
url,
2948
{% elif openapi_spec_url is sequence %}
30-
urls: [
31-
{% for item in openapi_spec_url %}
32-
{url:"{{item.url}}", name:"{{item.name}}"},
33-
{% endfor %}
34-
],
49+
urls,
3550
layout: "StandaloneLayout",
3651
{% endif %}
3752
{% if oauth2_redirect_url.startswith('.') %}

tests/fixtures/docs/nocache.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<swagger-ui nocache src="./openapi-spec/sample.yaml"/>
2+
<swagger-ui nocache src="./openapi-spec/sample-oauth2.yaml?doudou=dada"/>
3+
<swagger-ui nocache src="https://petstore.swagger.io/v2/swagger.json"/>

0 commit comments

Comments
 (0)