From 8772733e0b5456c37926b723fd6af7f69da824f4 Mon Sep 17 00:00:00 2001 From: forehalo Date: Thu, 19 Sep 2024 11:58:39 +0800 Subject: [PATCH] fix(server): renderer path --- .github/helm/affine/templates/ingress.yaml | 9 +- .../src/core/doc-renderer/controller.ts | 83 +++++++++++++------ 2 files changed, 58 insertions(+), 34 deletions(-) diff --git a/.github/helm/affine/templates/ingress.yaml b/.github/helm/affine/templates/ingress.yaml index 8a3f03a533d29..1327e4584bf57 100644 --- a/.github/helm/affine/templates/ingress.yaml +++ b/.github/helm/affine/templates/ingress.yaml @@ -60,14 +60,7 @@ spec: name: affine-graphql port: number: {{ .Values.graphql.service.port }} - - path: /workspace/([^/]+)/(home|all|search|collection|tag|trash) - pathType: ImplementationSpecific - backend: - service: - name: affine-web - port: - number: {{ .Values.web.service.port }} - - path: /workspace/([^/]+)/.+ + - path: /workspace/([^/]+)/([^/]+)$ pathType: ImplementationSpecific backend: service: diff --git a/packages/backend/server/src/core/doc-renderer/controller.ts b/packages/backend/server/src/core/doc-renderer/controller.ts index 85035ed05ea8e..c35c698aa07e1 100644 --- a/packages/backend/server/src/core/doc-renderer/controller.ts +++ b/packages/backend/server/src/core/doc-renderer/controller.ts @@ -18,6 +18,7 @@ interface RenderOptions { } interface HtmlAssets { + html: string; css: string[]; js: string[]; publicPath: string; @@ -26,6 +27,7 @@ interface HtmlAssets { } const defaultAssets: HtmlAssets = { + html: '', css: [], js: [], publicPath: '/', @@ -33,6 +35,16 @@ const defaultAssets: HtmlAssets = { description: '', }; +// TODO(@forehalo): reuse routes with frontend +const staticPaths = new Set([ + 'all', + 'home', + 'search', + 'collection', + 'tag', + 'trash', +]); + @Controller('/workspace') export class DocRendererController { private readonly logger = new Logger(DocRendererController.name); @@ -45,26 +57,18 @@ export class DocRendererController { private readonly config: Config, private readonly url: URLHelper ) { - try { - const webConfigMapsPath = join( + this.webAssets = this.readHtmlAssets( + join( this.config.projectRoot, - this.config.isSelfhosted ? 'static/selfhost' : 'static', - 'assets-manifest.json' - ); - const mobileConfigMapsPath = join( + this.config.isSelfhosted ? 'static/selfhost' : 'static' + ) + ); + this.mobileAssets = this.readHtmlAssets( + join( this.config.projectRoot, - this.config.isSelfhosted ? 'static/mobile/selfhost' : 'static/mobile', - 'assets-manifest.json' - ); - this.webAssets = JSON.parse(readFileSync(webConfigMapsPath, 'utf-8')); - this.mobileAssets = JSON.parse( - readFileSync(mobileConfigMapsPath, 'utf-8') - ); - } catch (e) { - if (this.config.node.prod) { - throw e; - } - } + this.config.isSelfhosted ? 'static/mobile/selfhost' : 'static/mobile' + ) + ); } @Public() @@ -84,14 +88,17 @@ export class DocRendererController { : this.webAssets; let opts: RenderOptions | null = null; - try { - opts = - workspaceId === docId - ? await this.getWorkspaceContent(workspaceId) - : await this.getPageContent(workspaceId, docId); - metrics.doc.counter('render').add(1); - } catch (e) { - this.logger.error('failed to render page', e); + + if (!staticPaths.has(docId)) { + try { + opts = + workspaceId === docId + ? await this.getWorkspaceContent(workspaceId) + : await this.getPageContent(workspaceId, docId); + metrics.doc.counter('render').add(1); + } catch (e) { + this.logger.error('failed to render page', e); + } } res.setHeader('Content-Type', 'text/html'); @@ -148,6 +155,10 @@ export class DocRendererController { } _render(opts: RenderOptions | null, assets: HtmlAssets): string { + if (!opts && assets.html) { + return assets.html; + } + const title = opts?.title ? htmlSanitize(`${opts.title} | AFFiNE`) : 'AFFiNE'; @@ -199,4 +210,24 @@ export class DocRendererController { `; } + + /** + * Should only be called at startup time + */ + private readHtmlAssets(path: string): HtmlAssets { + const manifestPath = join(path, 'assets-manifest.json'); + const htmlPath = join(path, 'index.html'); + + try { + const assets = JSON.parse(readFileSync(manifestPath, 'utf-8')); + assets.html = readFileSync(htmlPath, 'utf-8'); + return assets; + } catch (e) { + if (this.config.node.prod) { + throw e; + } else { + return defaultAssets; + } + } + } }