diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts index 41a57d24..87bc78fd 100644 --- a/frontend/src/router/index.ts +++ b/frontend/src/router/index.ts @@ -1,4 +1,5 @@ import { createRouter, createWebHistory } from 'vue-router' +import { defineComponent } from 'vue' import type { RouteLocationNormalized } from 'vue-router' import { useAuthStore } from '@/stores/auth' import { useDeploymentCapabilitiesStore } from '@/stores/deploymentCapabilities' @@ -12,6 +13,11 @@ import { isToolboxSection, toolboxLocation } from '@/config/toolbox' /** Lite /桌面 WebView 硬刷新时可能只打开 `/`,用 session 记住上次页面以便恢复 */ const LITE_LAST_PATH_KEY = 'weknora_lite_last_path' +// views/platform/index.vue always mounts the settings modal and opens it when +// the path is /platform/settings, so this route only has to own the URL. +// Rendering Settings.vue here would mount a second, independent copy. +const SettingsRouteOutlet = defineComponent({ name: 'SettingsRouteOutlet', render: () => null }) + function isLiteEdition(authStore: ReturnType) { return authStore.isLiteMode || localStorage.getItem('weknora_lite_mode') === 'true' } @@ -99,7 +105,7 @@ const router = createRouter({ { path: "settings", name: "settings", - component: () => import("../views/settings/Settings.vue"), + component: SettingsRouteOutlet, meta: { requiresInit: true, requiresAuth: true } }, { diff --git a/frontend/src/router/settingsRoute.test.mjs b/frontend/src/router/settingsRoute.test.mjs new file mode 100644 index 00000000..069e8348 --- /dev/null +++ b/frontend/src/router/settingsRoute.test.mjs @@ -0,0 +1,25 @@ +import assert from 'node:assert/strict' +import { readFileSync } from 'node:fs' +import test from 'node:test' + +const router = readFileSync(new URL('./index.ts', import.meta.url), 'utf8') +const platformShell = readFileSync(new URL('../views/platform/index.vue', import.meta.url), 'utf8') + +function routeBlock(name) { + const at = router.indexOf(`name: "${name}"`) + assert.notEqual(at, -1, `route ${name} not found`) + return router.slice(router.lastIndexOf('{', at), router.indexOf('}', at) + 1) +} + +test('platform shell is the only place that mounts the settings modal', () => { + // Two copies each fetch their own data, so a save in one leaves the other stale. + assert.equal(platformShell.match(//g)?.length, 1) + assert.doesNotMatch(router, /views\/settings\/Settings\.vue/) +}) + +test('/platform/settings keeps a route but renders nothing into the outlet', () => { + const block = routeBlock('settings') + assert.match(block, /path: "settings"/) + assert.match(block, /component: SettingsRouteOutlet/) + assert.match(router, /const SettingsRouteOutlet = defineComponent\(\{[^}]*render: \(\) => null/) +})