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

wip on ssr #31

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ node_modules/
.vscode
dist
tmp
lerna-debug.log
lerna-debug.log
2 changes: 1 addition & 1 deletion packages/client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<title>Falcon Tanks</title>
</head>
<body>
<div id="root"></div>
<div id="root"><!--ssr-outlet--></div>
<div id="modal-root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
Expand Down
2 changes: 2 additions & 0 deletions packages/client/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// index.js
// Necessary for correct operation of modules
1 change: 1 addition & 0 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"build:ssr": "tsc && vite build --config ssr.config.ts",
"preview": "vite preview",
"lint": "eslint . && prettier .",
"format": "prettier --write .",
Expand Down
7 changes: 4 additions & 3 deletions packages/client/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import '@/scss/styles.scss'
import { store } from '@/store'
import React from 'react'
import ReactDOM from 'react-dom/client'
import { Provider } from 'react-redux'
import { store } from '@/store'
import { ToastContainer } from 'react-toastify'
import 'react-toastify/dist/ReactToastify.css'
import App from './app/App'
import '@/scss/styles.scss'
import { registerServiceWorker } from './registerServiceWorker'

ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
ReactDOM.hydrateRoot(
document.getElementById('root') as HTMLElement as HTMLElement,
<React.StrictMode>
<Provider store={store}>
<App />
Expand Down
25 changes: 25 additions & 0 deletions packages/client/ssr.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import react from '@vitejs/plugin-react'
import * as path from 'path'
import { defineConfig } from 'vite'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
resolve: {
alias: {
'@': path.resolve(__dirname, 'src'),
},
},
build: {
lib: {
entry: path.resolve(__dirname, 'ssr.tsx'),
name: 'Client',
formats: ['cjs'],
},
rollupOptions: {
output: {
dir: 'ssr-dist',
},
},
},
})
5 changes: 5 additions & 0 deletions packages/client/ssr.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import ReactDOM from 'react-dom/server'
import App from './src/app/App'
import './src/scss/styles.scss'

export const render = () => ReactDOM.renderToString(<App />)
2 changes: 1 addition & 1 deletion packages/client/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
"@/*": ["./src/*"]
}
},
"include": ["src"],
"include": ["src", "ssr.tsx"],
"references": [{ "path": "./tsconfig.node.json" }]
}
4 changes: 2 additions & 2 deletions packages/client/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import dotenv from 'dotenv'
dotenv.config()
import path from 'path'
import { defineConfig } from 'vite'
dotenv.config()

// https://vitejs.dev/config/
export default defineConfig({
Expand Down
2 changes: 2 additions & 0 deletions packages/server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
ssr-dist
64 changes: 52 additions & 12 deletions packages/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,60 @@
import dotenv from 'dotenv'
import cors from 'cors'
import dotenv from 'dotenv'
import fs from 'node:fs'
import path from 'node:path'
dotenv.config()

import express from 'express'
import { createClientAndConnect } from './db'
// import { createClientAndConnect } from './db'

const startServer = () => {
const app = express()
app.use(cors())
const port = Number(process.env.SERVER_PORT) || 3001

// createClientAndConnect()

app.get('/api', (_, res) => {
res.json('👋 Howdy from the server :)')
})

app.use('*', async (_, res) => {
global.window = {} as Window & typeof globalThis
;(global.Image as unknown) = class {
constructor() {
// Добавьте любую необходимую реализацию или оставьте пустой класс
}
}
global.document = {} as Document

const distPath = path.dirname(
require.resolve('falcon-tanks/dist/index.html')
)

const app = express()
app.use(cors())
const port = Number(process.env.SERVER_PORT) || 3001
const ssrClientPath = require.resolve(
'falcon-tanks/ssr-dist/falcon-tanks.cjs'
)

createClientAndConnect()
try {
const template = fs.readFileSync(
path.resolve(distPath, 'index.html'),
'utf-8'
)
const { render } = await import(ssrClientPath)
const appHtml = await render()
const html = template.replace('<!--ssr-outlet-->', appHtml)
res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
} catch (e) {
console.log('error in try catch: ', e)
// vite.ssrFixStacktrace(e)
// next(e)
}
})

app.get('/', (_, res) => {
res.json('👋 Howdy from the server :)')
})
app.listen(port, () => {
console.log(` ➜ 🎸 Server is listening on port: ${port}`)
})
}

app.listen(port, () => {
console.log(` ➜ 🎸 Server is listening on port: ${port}`)
})
console.log('start')
startServer()
6 changes: 4 additions & 2 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@
"version": "1.0.0",
"description": "",
"scripts": {
"build": "tsc --p ./tsconfig.prod.json",
"build": "rm -rf dist && tsc --p ./tsconfig.prod.json",
"preview": "node ./dist/index.js",
"dev": "cross-env NODE_ENV=development nodemon index.ts",
"lint": "eslint .",
"format": "prettier --write .",
"test": "jest ."
},
"dependencies": {
"@types/node": "^22.7.9",
"cors": "^2.8.5",
"cross-env": "^7.0.3",
"dotenv": "^16.0.2",
"eslint-config-prettier": "^8.5.0",
"express": "^4.18.1",
"pg": "^8.8.0",
"prettier": "^2.7.1"
"prettier": "^2.7.1",
"vite": "^5.4.10"
},
"devDependencies": {
"@types/cors": "^2.8.12",
Expand Down
3 changes: 2 additions & 1 deletion packages/server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"composite": true,
"target": "es2019",
"module": "commonjs",
"allowSyntheticDefaultImports": true,
"moduleResolution": "node",
"esModuleInterop": true,
"sourceMap": true,
Expand All @@ -18,7 +19,7 @@
"noUnusedLocals": true,
"forceConsistentCasingInFileNames": true,
"importsNotUsedAsValues": "error",
"lib": ["es2019", "esnext.asynciterable"],
"lib": ["es2019", "esnext.asynciterable", "DOM"],
"types": ["node", "jest"],
"baseUrl": ".",
"paths": {
Expand Down
Loading
Loading