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

Change from Express to Fastify #198

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:

strategy:
matrix:
node-version: ['22.x']
node-version: ['22.13.0']

steps:
- uses: actions/checkout@v4
Expand All @@ -19,7 +19,7 @@ jobs:
- name: 🌎 install dependencies
run: npm ci --unsafe-perm && cd apps/front && npm ci --unsafe-perm

- name: ⚡️build front
- name: ⚡️ build front
run: npm run front:build

- name: 🙏🏽 test
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ npm i

- Setup the project:
By default, SSG mode is enabled, you can switch to SPA or SSR mode by setting `MODE` to `SPA` or `SSR` in the `cli/config.js` file.


### Development

```shell
Expand All @@ -95,6 +97,18 @@ npm run front:dev
HOST="your local IP"
```

### SSL

If you want to enable SSL, set the `PROTOCOL` var in your `.env.local` file to `https`
Then generate your localhost.crt & localhost.key in `apps/front` with :

```shell
openssl req -x509 -out localhost.crt -keyout localhost.key \
-newkey rsa:2048 -nodes -sha256 \
-subj '/CN=localhost' -extensions EXT -config <( \
printf "[dn]\nCN=localhost\n[req]\ndistinguished_name = dn\n[EXT]\nsubjectAltName=DNS:localhost\nkeyUsage=digitalSignature\nextendedKeyUsage=serverAuth")
```

## CLI

npm scripts command line interface is available from the main [package.json](./package.json).
Expand Down
2 changes: 2 additions & 0 deletions apps/front/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ dist-ssr
vite.config.ts.timestamp*
stats.html
tsconfig.tsbuildinfo
localhost.crt
localhost.key
4,041 changes: 2,685 additions & 1,356 deletions apps/front/package-lock.json

Large diffs are not rendered by default.

22 changes: 14 additions & 8 deletions apps/front/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
"main": "src/index-client.tsx",
"type": "module",
"scripts": {
"dev": "node server.dev.js",
"dev": "tsx watch server.dev.ts",
"start": "cross-env NODE_ENV=production node dist/ssr/scripts/server.prod.js",
"spa:preview": "serve dist/spa",
"static:preview": "serve dist/static/client",
"static:generate": "node dist/static/scripts/exe-prerender.js",
"static:prerender-server": "node dist/static/scripts/exe-prerender-server.js",
"test:watch": "vitest",
"test": "vitest run",
"build:spa": "cross-env VITE_SPA=true vite build --outDir dist/spa",
Expand All @@ -15,8 +19,7 @@
"build:ssr": "npm run build:ssr-scripts && npm run build:ssr-client && npm run build:ssr-server",
"build:static-scripts": "vite build -c vite.static-scripts.config.ts",
"build:static-client": "vite build --outDir dist/static/client",
"build:static": "npm run build:static-scripts && npm run build:static-client && npm run generate",
"generate": "node dist/static/scripts/exe-prerender.js",
"build:static": "npm run build:static-scripts && npm run build:static-client && npm run static:generate",
"build": "npm run build:spa && npm run build:ssr && npm run build:static"
},
"dependencies": {
Expand All @@ -32,23 +35,26 @@
"devDependencies": {
"@cher-ami/mfs": "^0.2.0",
"@eggjs/ip": "^2.1.0",
"@types/events": "^3.0.3",
"@types/node": "^22.13.1",
"@types/react": "^19.0.8",
"@types/react-dom": "^19.0.3",
"@vitejs/plugin-legacy": "^6.0.1",
"@vitejs/plugin-react-swc": "^3.8.0",
"@fastify/compress": "^8.0.1",
"@fastify/middie": "^9.0.3",
"@fastify/one-line-logger": "^2.0.2",
"@fastify/static": "^8.1.0",
"fastify": "^5.2.1",
"autoprefixer": "^10.4.20",
"chalk": "^5.4.1",
"compression": "^1.7.5",
"serve": "^14.2.4",
"cross-env": "^7.0.3",
"express": "^4.21.2",
"husky": "^9.1.7",
"portfinder-sync": "^0.0.2",
"rollup-plugin-visualizer": "^5.14.0",
"sass": "1.84.0",
"sirv": "^3.0.0",
"terser": "^5.38.1",
"terser": "^5.38.2",
"tsx": "^4.19.2",
"typescript": "^5.7.3",
"vite": "^6.1.0",
"vite-plugin-checker": "^0.8.0",
Expand Down
44 changes: 24 additions & 20 deletions apps/front/prerender/exe-prerender-server.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import chalk from "chalk"
import express from "express"
import Fastify, { FastifyRequest } from "fastify"
import * as process from "process"
import { loadEnv } from "vite"
import { prerender } from "./prerender"
import { fetchAvailableUrls } from "./urls"

const envs = loadEnv("", process.cwd(), "")
const port = envs.PRERENDER_SERVER_NODE_PORT || process.env.PRERENDER_SERVER_NODE_PORT
const app = express()

app.get("/generate", async (req, res) => {
console.log(chalk.grey("req.query"), req.query)
const fastify = Fastify({
logger: false
})

fastify.get("/generate", async (request: FastifyRequest, reply) => {
console.log(chalk.grey("request.query"), request.query)

let urlsArray
if (req.query?.url) {
urlsArray = [req.query.url]
if ((request?.query as any)?.url) {
urlsArray = [(request?.query as any)?.url]
} else {
try {
urlsArray = await fetchAvailableUrls()
Expand All @@ -23,20 +26,21 @@ app.get("/generate", async (req, res) => {
}
}

// second arg "./static" is matching cher-ami deploy conf
// need to be edited if we want to start this server locally
await prerender(urlsArray)
res?.send("Generated static pages: " + urlsArray.join(", "))
return `Generated static pages: ${urlsArray.join(", ")}`
})

app.listen(port, () => {
console.log("")
console.log(
`> Generate all pages ${chalk.cyan(`http://localhost:${port}/generate`)}`
)
console.log(
`> Generate specific page ${chalk.cyan(
`http://localhost:${port}/generate?url=/my-page-url`
)}`
)
})
const start = async () => {
try {
await fastify.listen({ port: parseInt(port), host: "0.0.0.0" })
console.log(`> Generate all pages ${chalk.cyan(`http://localhost:${port}/generate`)}`)
console.log(
`> Generate specific page ${chalk.cyan(`http://localhost:${port}/generate?url=/my-page-url`)}`
)
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}

start()
103 changes: 0 additions & 103 deletions apps/front/server.dev.js

This file was deleted.

Loading