Skip to content

Commit

Permalink
feat: pass renderCtx to document hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
liximomo committed Apr 27, 2020
1 parent c31c553 commit e776a55
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
22 changes: 12 additions & 10 deletions packages/shuvi/src/renderer/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { IBuiltResource } from '../api';
import IHtmlTag = Runtime.IHtmlTag;
import IDocumentProps = Runtime.IDocumentProps;
import IRenderResultRedirect = Runtime.IRenderResultRedirect;
import IRequest = Runtime.IRequest;
import IRendererContext = Runtime.IRendererContext;

export function isRedirect(obj: any): obj is IRenderResultRedirect {
return obj && (obj as IRenderResultRedirect).$type === 'redirect';
Expand All @@ -38,27 +38,29 @@ export abstract class BaseRenderer {
parsedUrl = parseUrl(req.url, true);
}
const { document } = this._resources.server;

const docProps = await this.getDocumentProps({
...req,
parsedUrl,
headers: req.headers || {}
});
const ctx: IRendererContext = {
req: {
...req,
parsedUrl,
headers: req.headers || {}
}
};
const docProps = await this.getDocumentProps(ctx);
if (isRedirect(docProps)) {
return docProps;
}

if (document.onDocumentProps) {
document.onDocumentProps(docProps);
document.onDocumentProps(docProps, ctx);
}
return this._renderDocument(
docProps,
document.getTemplateData ? document.getTemplateData() : {}
document.getTemplateData ? document.getTemplateData(ctx) : {}
);
}

protected abstract getDocumentProps(
req: IRequest
ctx: IRendererContext
):
| Promise<IDocumentProps | IRenderResultRedirect>
| IDocumentProps
Expand Down
4 changes: 2 additions & 2 deletions packages/shuvi/src/renderer/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import { IServerContext } from './types';

import IAppData = Runtime.IAppData;
import IHtmlTag = Runtime.IHtmlTag;
import IRequest = Runtime.IRequest;
import IRendererContext = Runtime.IRendererContext;

export class SsrRenderer extends BaseRenderer {
constructor(ctx: IServerContext) {
super(ctx);
}

async getDocumentProps(req: IRequest) {
async getDocumentProps({ req }: IRendererContext) {
const { api } = this._serverCtx;
const { renderer, App, routes } = api.resources.server;
const result = await renderer({
Expand Down
9 changes: 7 additions & 2 deletions packages/types/src/runtime.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,16 @@ export interface IRouter {
onChange(listener: IRouterListener): () => void;
}

export interface IRendererContext {
req: IRequest;
}

export interface IDocumentModule {
onDocumentProps(
documentProps: IDocumentProps
documentProps: IDocumentProps,
ctx: IRendererContext
): Promise<IDocumentProps> | IDocumentProps;
getTemplateData(): Promise<ITemplateData> | ITemplateData;
getTemplateData(ctx: IRendererContext): Promise<ITemplateData> | ITemplateData;
}

export interface IRuntime<CompType = unknown> {
Expand Down

0 comments on commit e776a55

Please sign in to comment.