mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-22 03:05:19 -04:00
feat: fetch frontend config in server side rendering
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
parent
312d1adf6f
commit
24f1b2a361
41 changed files with 270 additions and 220 deletions
35
frontend/src/utils/frontend-config-fetcher.ts
Normal file
35
frontend/src/utils/frontend-config-fetcher.ts
Normal file
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import { getConfig } from '../api/config'
|
||||
import type { FrontendConfig } from '../api/config/types'
|
||||
import type { BaseUrls } from '../components/common/base-url/base-url-context-provider'
|
||||
import { Logger } from './logger'
|
||||
|
||||
/**
|
||||
* Fetches and caches the {@link FrontendConfig frontend config} from the backend.
|
||||
*/
|
||||
export class FrontendConfigFetcher {
|
||||
private readonly logger = new Logger('Frontend config fetcher')
|
||||
|
||||
private frontendConfig: FrontendConfig | undefined = undefined
|
||||
|
||||
public async fetch(baseUrls: BaseUrls | undefined): Promise<FrontendConfig | undefined> {
|
||||
if (!this.frontendConfig) {
|
||||
if (baseUrls === undefined) {
|
||||
return undefined
|
||||
}
|
||||
const baseUrl = baseUrls.editor.toString()
|
||||
try {
|
||||
this.frontendConfig = await getConfig(baseUrl)
|
||||
} catch (error) {
|
||||
this.logger.error(`Couldn't fetch frontend configuration from ${baseUrl}`, error)
|
||||
return undefined
|
||||
}
|
||||
this.logger.info(`Fetched frontend config from ${baseUrl}`)
|
||||
}
|
||||
return this.frontendConfig
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue