Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/hub-ui/src/client/state/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,34 @@ describe('dock route persistence', () => {
vi.unstubAllGlobals()
})

it('resolves a hub iframe against its serving origin and restores its saved route', async () => {
const { rpc, sharedStates, trust } = createStubRpc()
rpc.connection.metaBaseUrl = 'https://hub.example/tools/__connection.json'
const files = {
id: 'files',
type: 'iframe',
title: 'Files',
icon: 'ph:folder',
url: '/files/',
} as const
const route = 'https://hub.example/files/tree?sort=name#readme'
const session = ref<DockSessionStorage>({ open: true, selectedDockId: files.id, selectedDockRoute: route })
const context = await createDocksContext('embedded', rpc, undefined, session)
trust()
sharedStates.get(HUB_EVENTS.sharedState.docks)!.push([
files,
{ ...files, id: 'report', url: 'reports/panel' },
])
sharedStates.get(HUB_EVENTS.sharedState.dockRenderers)!.push({})
await flushRestore()

expect(context.docks.selected).toMatchObject({ url: 'https://hub.example/files/' })
expect(context.panel.consumeBootRoute?.(files.id)).toBe(route)
expect(context.docks.entries.find(entry => entry.id === 'report')).toMatchObject({ url: 'reports/panel' })
context.docks.register({ ...files, url: '/local/' }, true)
expect(context.docks.selected).toMatchObject({ url: '/local/' })
})

it('clears the old route before publishing a different dock selection', async () => {
expect.assertions(2)

Expand Down
19 changes: 17 additions & 2 deletions packages/hub-ui/src/client/state/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { SharedState } from 'devframe/utils/shared-state'
import type { WhenContext } from 'devframe/utils/when'
import type { Ref } from 'vue'
import type { DevframeDocksUserSettings } from './dock-settings'
import { attachFrameNavClient, createDockRenderersContext } from '@devframes/hub/client'
import { attachFrameNavClient, createDockRenderersContext, resolveDockUrl } from '@devframes/hub/client'
import { DEFAULT_STATE_USER_SETTINGS, DOCK_RENDERERS_STATE_KEY, HUB_EVENTS } from '@devframes/hub/constants'
import { DEVFRAME_EVENTS } from 'devframe/constants'
import { createEventEmitter } from 'devframe/utils/events'
Expand Down Expand Up @@ -55,6 +55,9 @@ export async function createDocksContext(
// `devframe:docks` docks by id, and never sync to shared state. Mirrors the
// merge semantics of `@devframes/hub`'s own client host.
const clientDocks = reactive(new Map<string, DevframeDockEntry>())
const connection = rpc.connection
const isCrossOriginEmbed = clientType === 'embedded' && connection?.metaBaseUrl
&& new URL(connection.metaBaseUrl, globalThis.location?.href).origin !== globalThis.location?.origin
const entries = computed<DevframeDockEntry[]>(() => {
const server = dockEntries.value
let base: DevframeDockEntry[]
Expand All @@ -78,7 +81,19 @@ export async function createDocksContext(
// hub-ui owns the built-in Settings tab so it's always reachable without a
// host registering `~settings`; a host that registered its own wins, so add
// ours only when the merged list has none.
const localized = base.map(entry => localizeTitle(entry))
const localized = base.map((entry) => {
const dock = localizeTitle(entry)
// Resolve /, ./ and ../ against the hub. Keep bare paths relative to the page.
if (
isCrossOriginEmbed
&& dock.type === 'iframe'
&& !clientDocks.has(dock.id)
&& /^\.{0,2}\//.test(dock.url)
) {
dock.url = resolveDockUrl(dock.url, connection)
}
return dock
})
if (localized.some(entry => entry.id === BUILTIN_ENTRY_SETTINGS.id))
return localized
return [
Expand Down
Loading