Does Swagger UI retain the selected server across page reloads in embedded deployments?
0 reputation · 05 Sept 2020, 01:33 UTC
0 reputation · 05 Sept 2020, 01:33 UTC
When an OpenAPI 3.x document defines multiple servers entries, Swagger UI renders them as a dropdown for "Try it out" requests. The first entry in the array is selected by default, yet the behavior after a user changes that selection and reloads the page varies across releases and embedding contexts.
Recent standalone Swagger UI distributions appear to persist the active server in browser local storage, whereas older versions or instances embedded in host applications with restricted storage may reset to the first entry on every load. Because local-storage access is not guaranteed in private browsing modes or sandboxed iframes, teams should verify persistence against the exact version bundled in their application rather than relying on general documentation.
Does the selected server survive a page reload in a given Swagger UI version, and what configuration or deployment factors determine whether local-storage persistence is available? Which behavior should teams expect when the same document is rendered in both a standalone distribution and an embedded iframe?
35275 reputation · 05 Sept 2020, 08:02 UTC
It depends on the deployment context. Swagger UI restores the user's server choice only when it can read and write localStorage under the current origin. In a same-origin standalone page or embedded iframe, the selection usually survives a reload. In a cross-origin iframe, private-browsing window, or sandboxed iframe without storage access, the UI falls back to the first servers entry.
servers entries render as a dropdown; the first entry is the default.localStorage, not server-side state or URL query parameters.allow-same-origin affect whether the iframe can access localStorage.Newer Swagger UI releases (approximately v4.15 and later) write the selected server index or URL to localStorage. Older releases may keep the choice in memory only and reset on reload. Treat the exact version as unverified until you check the bundled package.json.
Application > Local Storage, and look for a key such as swagger-ui:serverIndex or swagger-ui:serverUrl.Provide the exact Swagger UI version bundled in your application. The persistence logic changed between releases, and the version determines whether built-in localStorage persistence exists at all.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.