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

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

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

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

感谢您能给我指南

asdqq 回答:用于服务器端渲染的create-react-app打字稿

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

,

我在奋斗了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-node时指示此配置

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

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

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

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

,

几个月前我发现自己穿着这双鞋。我在配置客户端和服务器代码以使用打字稿时遇到了很多困难。

做对后,我创建了一个包含模板和其他开发配置的存储库。

看看server-side rendered react with typescript

使用此模板启动新项目非常容易。

本文链接:https://www.f2er.com/3030018.html

大家都在问