Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
381924237 opened this issue Oct 16, 2023 · 0 comments
Closed

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

381924237 opened this issue Oct 16, 2023 · 0 comments

Comments

@381924237
Copy link

详细描述你的问题

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant