zhangyuang / ssr

A most advanced ssr framework support React17/React18/Vue2/Vue3 on Earth that implemented serverless-side render specification.
http://doc.ssr-fc.com/
MIT License
2.61k stars 284 forks source link

访问未定义的路由时,server端代码报错 #312

Closed 381924237 closed 1 year ago

381924237 commented 1 year ago

详细描述你的问题

ssr-common-utils > findRoute

function findRoute<T extends { path: string, childPath?: string, children?: [] }> (Routes: T[], path: string): T {
  const p = path.includes('?') ? path.split('?')[0] : path
  const route = Routes.find(route => {
    return route.children ? findRoute(route.children, p) : matchPath(p, route)?.isExact
  })
  return route ?? {} // 此处不会返回undefined
}

ssr-common-utils > checkRoute

const checkRoute = ({
  routeItem,
  path
}: {
  routeItem?: any
  path: string
}) => {
  // 永远不会抛出错误
  if (!routeItem) {
    throw new Error(`
      With Path: ${path} search component failed
      If you create new folder or component file, please restart server by npm start
      `)
  }
}

plugin-react-18 > src > entry > server-entry.tsx

const serverRender = async (ctx: ISSRContext, config: IConfig) => {
  ...
  const routeItem = findRoute<ReactESMPreloadFeRouteItem>(FeRoutes, path) // 此处返回的routeItem永远不为undefined
  checkRoute({ routeItem, path }) // 检查后不会抛出错误
  const { fetch, webpackChunkName, component } = routeItem // 获取到的component为undefined
  ...
  // 获取component.name时,报错
  const Component = isCsr ? React.Fragment : (component.name === 'dynamicComponent' ? (await (component as DynamicFC)()).default : component as StaticFC)

}

期望的结果

访问未定义的路由时,server端正常抛出 search component failed 错误

当前使用的版本

├─ ssr-common-utils@6.2.107 ├─ ssr-core@6.2.29 ├─ ssr-deepclone@1.0.1 ├─ ssr-hoc-react18@6.2.11 ├─ ssr-mini-css-extract-plugin@1.6.3 ├─ ssr-plugin-nestjs@6.2.24 ├─ ssr-plugin-react18@6.2.67 │ └─ ssri@7.1.1 ├─ ssr-serialize-javascript@6.0.4 ├─ ssr-types@6.2.49 ├─ ssr-vite-plugin-style-import@2.0.1 ├─ ssr-webpack@6.2.15 ├─ ssr-webpack4@4.46.2 ├─ ssr@6.2.61 └─ ssri@6.0.2