Skip to content

Commit

Permalink
fix: csr
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyuang committed Jan 30, 2021
1 parent d6817fa commit 6052c56
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
11 changes: 9 additions & 2 deletions packages/plugin-react/src/entry/server-entry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ declare const __isBrowser__: boolean
const serverRender = async (ctx: IFaaSContext, config: IConfig): Promise<React.ReactElement> => {
const { staticPrefix, cssOrder, jsOrder, isDev, fePort, dynamic, mode } = config
global.window = global.window ?? {} // 防止覆盖上层应用自己定义的 window 对象
const path = ctx.req.path || ctx.req.url
let path // path 不能够包涵 queryParams
if (ctx.req._parsedUrl) {
// 说明 服务端框架是 midway
path = ctx.req._parsedUrl.pathname
} else {
path = ctx.req.path
}
const { window } = global
const routeItem = findRoute<FeRouteItem<any>>(feRoutes, path)

let dynamicCssOrder = cssOrder
if (dynamic) {
dynamicCssOrder = cssOrder.concat([`${routeItem.webpackChunkName}.css`])
Expand All @@ -26,7 +33,7 @@ const serverRender = async (ctx: IFaaSContext, config: IConfig): Promise<React.R

const Layout = wrapLayout(routeItem.layout, __isBrowser__)
const Component = routeItem.component
if (mode === 'csr' || mode === 'csr' || ctx.query?.csr) {
if (mode === 'csr' || ctx.query?.csr) {
// 根据 mode 和 query 来决定当前渲染模式
logGreen(`The path ${path} use csr render mode`)
const Context = serverContext({}) // csr 不需要在服务端获取数据
Expand Down
6 changes: 1 addition & 5 deletions packages/server-utils/src/findRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ function matchPath (pathname, options = {}) {
function findRoute<T extends {path: string}> (Routes: T[], path: string): T {
// 根据请求的path来匹配到对应的Component
const route = Routes.find(route => matchPath(path, route) && matchPath(path, route).isExact)
if (route.mode) {
debug(`With path "${path}" find FaasRoute: `, route)
} else {
debug(`With path "${path}" find FeRoute: `, route)
}
debug(`With path "${path}" find Route: `, route)
return route
}

Expand Down
9 changes: 8 additions & 1 deletion packages/types/src/ctx.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { FaaSContext } from '@midwayjs/faas'
import { Action } from './component'

export type IFaaSContext<T={}> = FaaSContext & T
interface EggContext {
req: {
_parsedUrl?: {
pathname: string
}
}
}
export type IFaaSContext<T={}> = FaaSContext & EggContext &T

export interface Options {
mode?: string
Expand Down

0 comments on commit 6052c56

Please sign in to comment.