From a866e0dffe527de192fbdf1f4fca0ef50bab2eda Mon Sep 17 00:00:00 2001 From: WuYongJie Date: Fri, 10 Jul 2026 14:23:20 +0800 Subject: [PATCH] fix: use public origin for installation demo shares --- .../share/route.ts | 3 +- .../share/share-url.test.ts | 38 +++++++++++++++++++ .../share/share-url.ts | 6 +++ 3 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 furniture-3d-system/src/app/api/installation-demo-workbench/share/share-url.test.ts create mode 100644 furniture-3d-system/src/app/api/installation-demo-workbench/share/share-url.ts diff --git a/furniture-3d-system/src/app/api/installation-demo-workbench/share/route.ts b/furniture-3d-system/src/app/api/installation-demo-workbench/share/route.ts index 78e4b65d..43cfe07b 100644 --- a/furniture-3d-system/src/app/api/installation-demo-workbench/share/route.ts +++ b/furniture-3d-system/src/app/api/installation-demo-workbench/share/route.ts @@ -2,6 +2,7 @@ import { NextRequest } from 'next/server' import { AppError } from '@/server/core/errors' import { handleRequest, jsonError, jsonSuccess } from '@/server/core/response' import { installationDemoShareService } from '@/server/services' +import { buildInstallationDemoShareUrl } from './share-url' export async function POST(request: NextRequest) { return handleRequest( @@ -14,7 +15,7 @@ export async function POST(request: NextRequest) { { share: { token: snapshot.token, - url: `${request.nextUrl.origin}/installation-demo-share/${snapshot.token}`, + url: buildInstallationDemoShareUrl(snapshot.token, request.nextUrl.origin), expiresAt: snapshot.expiresAt, snapshot, }, diff --git a/furniture-3d-system/src/app/api/installation-demo-workbench/share/share-url.test.ts b/furniture-3d-system/src/app/api/installation-demo-workbench/share/share-url.test.ts new file mode 100644 index 00000000..2de1fe58 --- /dev/null +++ b/furniture-3d-system/src/app/api/installation-demo-workbench/share/share-url.test.ts @@ -0,0 +1,38 @@ +import { buildInstallationDemoShareUrl } from './share-url' + +const originalPublicAppUrl = process.env.NEXT_PUBLIC_APP_URL +const originalNextAuthUrl = process.env.NEXTAUTH_URL + +describe('buildInstallationDemoShareUrl', () => { + afterAll(() => { + if (originalPublicAppUrl === undefined) { + delete process.env.NEXT_PUBLIC_APP_URL + } else { + process.env.NEXT_PUBLIC_APP_URL = originalPublicAppUrl + } + + if (originalNextAuthUrl === undefined) { + delete process.env.NEXTAUTH_URL + } else { + process.env.NEXTAUTH_URL = originalNextAuthUrl + } + }) + + it('uses the configured public origin instead of the internal listener address', () => { + process.env.NEXT_PUBLIC_APP_URL = 'https://cp.jiansun.vip/' + delete process.env.NEXTAUTH_URL + + expect( + buildInstallationDemoShareUrl('share-token-1', 'https://0.0.0.0:3001'), + ).toBe('https://cp.jiansun.vip/installation-demo-share/share-token-1') + }) + + it('uses the request origin for local development when no public origin is configured', () => { + delete process.env.NEXT_PUBLIC_APP_URL + delete process.env.NEXTAUTH_URL + + expect( + buildInstallationDemoShareUrl('share-token-2', 'http://localhost:3001'), + ).toBe('http://localhost:3001/installation-demo-share/share-token-2') + }) +}) diff --git a/furniture-3d-system/src/app/api/installation-demo-workbench/share/share-url.ts b/furniture-3d-system/src/app/api/installation-demo-workbench/share/share-url.ts new file mode 100644 index 00000000..c1997374 --- /dev/null +++ b/furniture-3d-system/src/app/api/installation-demo-workbench/share/share-url.ts @@ -0,0 +1,6 @@ +export function buildInstallationDemoShareUrl(token: string, requestOrigin: string) { + const publicOrigin = + process.env.NEXT_PUBLIC_APP_URL?.trim() || process.env.NEXTAUTH_URL?.trim() || requestOrigin + + return new URL(`/installation-demo-share/${token}`, publicOrigin).toString() +}