用于服务器端租用的create-react-app打字稿

问题描述 投票:0回答:2

我有一个由create-react-app打字稿(tsx文件)构建的项目。

我现在想使项目成为SSR,但是我不确定如何开始。

我在this article之后使用打字稿之前能够进行SSR

感谢您能给我指南

reactjs typescript express create-react-app ssr
2个回答
0
投票

我真的很喜欢这个article。它回顾了实现SSR的不同方法,并为您提供了足够的解释。


0
投票

我奋斗了8个小时后解决了问题。

您不能在服务器上使用react的tsconfig文件。

我必须制作一个新的配置文件。 (server.tsconfig.json)

{
  "compilerOptions": {
    "baseUrl": "./src",
    "paths": {
      "*": ["*", "./src/@types/*"]
    },
    "outDir": "./dist",
    "target": "es5",
    "typeRoots": ["./src/@types", "./node_modules/@types"],
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noImplicitAny": false,
    "moduleResolution": "node",
    "strictNullChecks": false,
    "jsx": "react",
    "noEmit": true
  },
  "exclude": ["node_modules"]
}

并在运行ts节点时指示此配置

ts-node --project server.tsconfig.json --require tsconfig-paths/register server/index.js

如果使用自定义类型,则必须在服务器文件上添加类型的引用

/// <reference path="../src/@types/global.d.ts" />

我希望这可以节省您的时间

© www.soinside.com 2019 - 2024. All rights reserved.