Skip to content

Commit f785d4e

Browse files
authored
Change CB (cache buster) to use 'cache' from the request (#1684)
There was some confusion on the swagger URL builder to what `cb` was. This will remove the request to cache and bring the URL back to what it was before. `req.cache` sets the browser cache, more [on mdn's Request cache docs](https://developer.mozilla.org/en-US/docs/Web/API/Request/cache) `headers[...etc]` will keep the proxy/down the wire data sniffers in line with caching. (edge cache) I did some testing on localhost by 1. Clicking execute in the swagger page to see each request NOT cache 2. `fetch("http://localhost:8081/cwms-data/location/category")` in the browser console to see the requests cache 3. Repeating 1. after 2 to see no-cache was working 4. `fetch("http://localhost:8081/cwms-data/location/category", {cache: "no-cache"})` to mimic what swagger now does and confirming no "disk cache" in the network tab
1 parent 6eb6f8b commit f785d4e

1 file changed

Lines changed: 55 additions & 55 deletions

File tree

cda-gui/src/pages/swagger-ui/index.jsx

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,63 +5,63 @@ import { useEffect } from "react";
55
import { getBasePath } from "../../utils/base";
66

77
export default function SwaggerUI() {
8-
useEffect(() => {
9-
// document.querySelector("#swagger-ui").prepend(Index)
10-
// TODO: Add page index to top of page
11-
// Alter the page title to match the swagger page
12-
document.title = "CWMS Data API for Data Retrieval - Swagger UI";
13-
// Begin Swagger UI call region
14-
// TODO: add endpoint that dynamic returns swagger generated doc
8+
useEffect(() => {
9+
// document.querySelector("#swagger-ui").prepend(Index)
10+
// TODO: Add page index to top of page
11+
// Alter the page title to match the swagger page
12+
document.title = "CWMS Data API for Data Retrieval - Swagger UI";
13+
// Begin Swagger UI call region
14+
// TODO: add endpoint that dynamic returns swagger generated doc
1515

16-
const ui = SwaggerUIBundle({
17-
url: getBasePath() + "/swagger-docs",
18-
19-
dom_id: "#swagger-ui",
20-
deepLinking: false,
21-
presets: [SwaggerUIBundle.presets.apis],
22-
plugins: [SwaggerUIBundle.plugins.DownloadUrl],
23-
requestInterceptor: (req) => {
24-
// Add a cache-busting query param... but only if it's to our api. Some
25-
// external systems, like keycloak, don't allow random unknown parameters.
26-
const origin = window.location.origin;
27-
const re = new RegExp(`^${origin}.*`)
28-
if (re.test(req.url))
29-
{
30-
const sep = req.url.includes("?") ? "&" : "?";
31-
req.url = `${req.url}${sep}_cb=${Date.now()}`;
16+
const ui = SwaggerUIBundle({
17+
url: getBasePath() + "/swagger-docs",
3218

33-
// Also ask intermediaries not to serve from cache
34-
req.headers["Cache-Control"] = "no-cache, no-store, max-age=0";
35-
req.headers["Pragma"] = "no-cache";
36-
}
37-
return req;
38-
},
39-
onComplete: () => {
40-
const spec = JSON.parse(ui.spec().get("spec"));
41-
for (const schemeName in spec.components.securitySchemes) {
42-
const scheme = spec.components.securitySchemes[schemeName];
43-
if (scheme.type === "openIdConnect") {
44-
let additionalParams = null;
45-
let hints = scheme["x-kc_idp_hint"];
46-
if (hints) {
47-
additionalParams = {
48-
// Since getting the interface to allow users to choose
49-
// is likely impossible, we will assume the first in the list
50-
// is the "primary" auth system
51-
"kc_idp_hint": hints.values[0]
52-
};
53-
}
54-
ui.initOAuth({
55-
clientId: scheme["x-oidc-client-id"],
56-
usePkceWithAuthorizationCodeGrant: true,
57-
additionalQueryStringParams: additionalParams,
58-
});
59-
break;
60-
}
61-
}
62-
},
63-
});
64-
}, []);
19+
dom_id: "#swagger-ui",
20+
deepLinking: false,
21+
presets: [SwaggerUIBundle.presets.apis],
22+
plugins: [SwaggerUIBundle.plugins.DownloadUrl],
23+
requestInterceptor: (req) => {
24+
// Only alter cache behavior for same-origin API requests. External systems,
25+
// like keycloak, may reject unexpected request changes.
26+
const origin = window.location.origin;
27+
const re = new RegExp(`^${origin}.*`);
28+
if (re.test(req.url)) {
29+
// Control browser 'fetch' behavior
30+
req.cache = "no-store";
31+
// Ensure headers exist first
32+
req.headers = req.headers ?? {};
33+
// Reverse/forward proxies, intermediary, service worker caches
34+
req.headers["Cache-Control"] = "no-cache, no-store, max-age=0";
35+
req.headers["Pragma"] = "no-cache";
36+
}
37+
return req;
38+
},
39+
onComplete: () => {
40+
const spec = JSON.parse(ui.spec().get("spec"));
41+
for (const schemeName in spec.components.securitySchemes) {
42+
const scheme = spec.components.securitySchemes[schemeName];
43+
if (scheme.type === "openIdConnect") {
44+
let additionalParams = null;
45+
let hints = scheme["x-kc_idp_hint"];
46+
if (hints) {
47+
additionalParams = {
48+
// Since getting the interface to allow users to choose
49+
// is likely impossible, we will assume the first in the list
50+
// is the "primary" auth system
51+
kc_idp_hint: hints.values[0],
52+
};
53+
}
54+
ui.initOAuth({
55+
clientId: scheme["x-oidc-client-id"],
56+
usePkceWithAuthorizationCodeGrant: true,
57+
additionalQueryStringParams: additionalParams,
58+
});
59+
break;
60+
}
61+
}
62+
},
63+
});
64+
}, []);
6565

6666
return <div id="swagger-ui"></div>;
6767
}

0 commit comments

Comments
 (0)