Sfoglia il codice sorgente

fix(docs): derive the mount prefix in the API docs page

The RapiDoc page hardcoded spec-url="/openapi.json" and
src="/docs/rapidoc-min.js", so under a subpath mount both 404'd and the
page rendered an error instead of the reference.

Relative URLs are not safe here: the page answers on both /docs and
/docs/, and those resolve relative paths differently. So strip a trailing
/docs or /docs/ from location.pathname to recover the mount, then set
spec-url and the script src from it.

Also set server-url from the same base, so "Try it" targets this mount -
the spec declares no `servers` block, so it would otherwise aim at the
origin root.
fszontagh 1 mese fa
parent
commit
20ce37b260
1 ha cambiato i file con 18 aggiunte e 2 eliminazioni
  1. 18 2
      api/docs/index.html

+ 18 - 2
api/docs/index.html

@@ -2,8 +2,24 @@
 <html lang="en"><head><meta charset="utf-8"><title>smartbotic-vectorapi API</title>
 <meta name="viewport" content="width=device-width,initial-scale=1"></head>
 <body style="margin:0">
-  <rapi-doc spec-url="/openapi.json" theme="dark" bg-color="#0b0f17" text-color="#e5e7eb"
+  <rapi-doc theme="dark" bg-color="#0b0f17" text-color="#e5e7eb"
             render-style="read" show-header="false" allow-try="true"
             regular-font="system-ui, sans-serif"></rapi-doc>
-  <script type="module" src="/docs/rapidoc-min.js"></script>
+  <script>
+    // This page is served from <mount>/docs, with or without a trailing slash,
+    // and a subpath reverse proxy (e.g. nginx `location /rag/`) strips the prefix
+    // before the server sees it. So derive the mount prefix here rather than
+    // hardcoding absolute URLs, which 404 whenever the app is not at the root.
+    //   '/docs'      -> ''      '/rag/docs'  -> '/rag'
+    //   '/docs/'     -> ''      '/rag/docs/' -> '/rag'
+    var BASE = location.pathname.replace(/\/docs\/?$/, '');
+    var doc = document.querySelector('rapi-doc');
+    doc.setAttribute('spec-url', BASE + '/openapi.json');
+    // The spec declares no `servers`, so point "Try it" at this same mount.
+    doc.setAttribute('server-url', location.origin + BASE);
+    var s = document.createElement('script');
+    s.type = 'module';
+    s.src = BASE + '/docs/rapidoc-min.js';
+    document.body.appendChild(s);
+  </script>
 </body></html>