diff --git a/app/common/UserAPI.ts b/app/common/UserAPI.ts index 08f9df80..aa84ef21 100644 --- a/app/common/UserAPI.ts +++ b/app/common/UserAPI.ts @@ -773,10 +773,10 @@ export class UserAPIImpl extends BaseAPI implements UserAPI { } public async getWorker(key: string): Promise { - const json = await this.requestJson(`${this._url}/api/worker/${key}`, { + const json: PublicDocWorkerUrlInfo = (await this.requestJson(`${this._url}/api/worker/${key}`, { method: 'GET', credentials: 'include' - }); + })) as PublicDocWorkerUrlInfo; return getPublicDocWorkerUrl(this._homeUrl, json); } @@ -1168,7 +1168,7 @@ export type PublicDocWorkerUrlInfo = { docWorkerUrl: null; } | { selfPrefix: null; - docWorkerUrl: string|null; + docWorkerUrl: string; } export function getUrlFromPrefix(homeUrl: string, prefix: string) { @@ -1191,6 +1191,7 @@ export function getUrlFromPrefix(homeUrl: string, prefix: string) { * (result of the call to /api/worker/:docId) */ export function getPublicDocWorkerUrl(homeUrl: string, docWorkerInfo: PublicDocWorkerUrlInfo) { - const publicUrl = docWorkerInfo.docWorkerUrl; - return publicUrl || getUrlFromPrefix(homeUrl, docWorkerInfo?.selfPrefix); + return docWorkerInfo.selfPrefix ? + getUrlFromPrefix(homeUrl, docWorkerInfo.selfPrefix) : + docWorkerInfo.docWorkerUrl!; } diff --git a/app/server/lib/AppEndpoint.ts b/app/server/lib/AppEndpoint.ts index 33f38a4e..a676f5e7 100644 --- a/app/server/lib/AppEndpoint.ts +++ b/app/server/lib/AppEndpoint.ts @@ -195,10 +195,16 @@ export function attachAppEndpoint(options: AttachOptions): void { }); } + // When no doc worker seed, we're in single server mode. + // Return null, to signify that the URL prefix serving the + // current endpoint is the only one available. + const publicUrl = docStatus?.docWorker?.publicUrl; + const workerPublicUrl = publicUrl !== undefined ? customizeDocWorkerUrl(publicUrl, req) : null; + await sendAppPage(req, res, {path: "", content: body.page, tag: body.tag, status: 200, googleTagManager: 'anon', config: { assignmentId: docId, - getWorker: {[docId]: customizeDocWorkerUrl(docStatus?.docWorker?.publicUrl, req)}, + getWorker: {[docId]: workerPublicUrl }, getDoc: {[docId]: pruneAPIResult(doc as unknown as APIDocument)}, plugins }}); diff --git a/app/server/lib/DocWorkerUtils.ts b/app/server/lib/DocWorkerUtils.ts index 25be2234..a5bc6cf9 100644 --- a/app/server/lib/DocWorkerUtils.ts +++ b/app/server/lib/DocWorkerUtils.ts @@ -36,16 +36,7 @@ import { getAssignmentId } from './idUtils'; * TODO: doc worker registration could be redesigned to remove the assumption * of a fixed base domain. */ -export function customizeDocWorkerUrl( - docWorkerUrlSeed: string|undefined, - req: express.Request -): string|null { - if (!docWorkerUrlSeed) { - // When no doc worker seed, we're in single server mode. - // Return null, to signify that the URL prefix serving the - // current endpoint is the only one available. - return null; - } +export function customizeDocWorkerUrl( docWorkerUrlSeed: string, req: express.Request): string { const docWorkerUrl = new URL(docWorkerUrlSeed); const workerSubdomain = parseSubdomainStrictly(docWorkerUrl.hostname).org; adaptServerUrl(docWorkerUrl, req);