mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-28 15:40:06 +00:00
fix(plugin-sdk): serve dev /ui frame at /ui/index.html so relative assets resolve (#1526)
The dev server embedded the plugin UI as <iframe src="/ui"> (no trailing slash), so a multi-file build's relative asset URLs (./assets/x.js from Vite base:'./') resolved against the origin root -> /assets/x.js -> 404, even though the files are served at /ui/assets/*. The real host loads the frame at /plugin-frame/<id>/index.html where the same relative URLs resolve correctly, so dev now matches it by loading /ui/index.html (and also serves /ui/ as index.html).
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "trek-plugin-sdk",
|
||||
"version": "1.4.0",
|
||||
"version": "1.4.1",
|
||||
"description": "SDK for building TREK plugins: types, definePlugin, a mock host, manifest validation, and the create-trek-plugin / trek-plugin CLIs.",
|
||||
"type": "module",
|
||||
"license": "MIT",
|
||||
|
||||
@@ -360,9 +360,15 @@ export async function runDev(dir: string, opts: { port?: number } = {}): Promise
|
||||
return send(200, preview(id, String(manifest.type)), 'text/html; charset=utf-8');
|
||||
}
|
||||
|
||||
// Static plugin UI at /ui (page/widget client bundle)
|
||||
// Static plugin UI at /ui (page/widget client bundle). The iframe loads
|
||||
// `/ui/index.html` (not bare `/ui`) so relative asset URLs — `./assets/x.js`
|
||||
// from a multi-file Vite/React build with `base: './'` — resolve to
|
||||
// `/ui/assets/x.js`, exactly as the real host resolves them against
|
||||
// `/plugin-frame/<id>/index.html`. A bare `/ui` document would resolve them
|
||||
// against the origin root (`/assets/x.js`) and 404. Bare `/ui` and `/ui/` both
|
||||
// still serve index.html for direct navigation.
|
||||
if (url.pathname === '/ui' || url.pathname.startsWith('/ui/')) {
|
||||
const relFile = url.pathname === '/ui' ? 'index.html' : url.pathname.slice('/ui/'.length);
|
||||
const relFile = url.pathname === '/ui' ? 'index.html' : (url.pathname.slice('/ui/'.length) || 'index.html');
|
||||
const file = path.join(abs, 'client', relFile);
|
||||
if (!file.startsWith(path.join(abs, 'client'))) return send(403, 'forbidden');
|
||||
if (!fs.existsSync(file) || !fs.statSync(file).isFile()) return send(404, 'not found — build your client/ bundle');
|
||||
@@ -510,7 +516,7 @@ iframe{width:100%;border:0;background:transparent;min-height:120px;display:block
|
||||
<label><input type="checkbox" id="nt"> no transparency</label>
|
||||
<label><input type="checkbox" id="trip" checked> trip context</label>
|
||||
</header>
|
||||
<div class="stage"><div class="wrap"><iframe id="f" src="/ui" sandbox="allow-scripts allow-forms" referrerpolicy="no-referrer" title="${id}"></iframe></div></div>
|
||||
<div class="stage"><div class="wrap"><iframe id="f" src="/ui/index.html" sandbox="allow-scripts allow-forms" referrerpolicy="no-referrer" title="${id}"></iframe></div></div>
|
||||
<p class="hint">The frame runs sandboxed at an opaque origin, exactly like TREK — <code>trek.invoke()</code> is proxied to your /api routes as the dev user.</p>
|
||||
<div class="toast" id="toast"></div>
|
||||
<script>
|
||||
|
||||
Reference in New Issue
Block a user