Skip to content

Commit 80b1a80

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

File tree

5 files changed

+47
-15
lines changed

5 files changed

+47
-15
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.yaml?doudou=doudou"/>
3+
<swagger-ui nocache src="https://petstore.swagger.io/v2/swagger.json"/>

tests/test_builds.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def validate_iframe(html_content, iframe_src_dir):
136136
iframe_src = iframe_tag.attrs.get("src")
137137
assert iframe_id is not None
138138
assert iframe_src is not None
139-
assert f"swagger-{iframe_id}.html" == iframe_src
139+
assert f"swagger-{iframe_id}.html" in iframe_src
140140
iframe_file = iframe_src_dir / iframe_src
141141
assert iframe_file.exists()
142142
iframe_content = iframe_file.read_text(encoding="utf8")
@@ -199,7 +199,7 @@ def test_basic(tmp_path):
199199

200200
# validate OpenAPI spec exist
201201
regex_obj = re.search(
202-
r'url: "(.*)"',
202+
r'var url = "(.*)"',
203203
iframe_contents,
204204
)
205205
assert regex_obj
@@ -224,7 +224,7 @@ def test_basic_sub_dir(tmp_path):
224224

225225
# validate OpenAPI spec exists
226226
regex_obj = re.search(
227-
r'url: "(.*)"',
227+
r'var url = "(.*)"',
228228
iframe_contents,
229229
)
230230
assert regex_obj
@@ -251,7 +251,7 @@ def test_use_directory_urls(tmp_path):
251251

252252
# validate OpenAPI spec exist
253253
regex_obj = re.search(
254-
r'url: "(.*)"',
254+
r'var url = "(.*)"',
255255
iframe_contents,
256256
)
257257
assert regex_obj
@@ -278,7 +278,7 @@ def test_use_directory_urls_sub_dir(tmp_path):
278278

279279
# validate OpenAPI spec exist
280280
regex_obj = re.search(
281-
r'url: "(.*)"',
281+
r'var url = "(.*)"',
282282
iframe_contents,
283283
)
284284
assert regex_obj
@@ -305,7 +305,7 @@ def test_material(tmp_path):
305305

306306
# validate OpenAPI spec exist
307307
regex_obj = re.search(
308-
r'url: "(.*)"',
308+
r'var url = "(.*)"',
309309
iframe_contents,
310310
)
311311
assert regex_obj
@@ -340,7 +340,7 @@ def test_url(tmp_path):
340340

341341
# validate OpenAPI spec exist
342342
regex_obj = re.search(
343-
r'url: "(.*)"',
343+
r'var url = "(.*)"',
344344
iframe_contents,
345345
)
346346
assert regex_obj
@@ -361,7 +361,7 @@ def test_multiple(tmp_path):
361361
for ind, iframe_contents in enumerate(iframe_content_list):
362362
# validate OpenAPI spec exist
363363
regex_obj = re.search(
364-
r'url: "(.*)"',
364+
r'var url = "(.*)"',
365365
iframe_contents,
366366
)
367367
assert regex_obj
@@ -396,7 +396,7 @@ def test_build_in_multiple(tmp_path):
396396
assert (file.parent / spec_url).resolve().exists()
397397
elif ind == 1:
398398
regex_obj = re.search(
399-
r'url: "(.*)"',
399+
r'var url = "(.*)"',
400400
iframe_contents,
401401
)
402402
assert regex_obj

0 commit comments

Comments
 (0)