|
| 1 | +import fs from 'node:fs/promises' |
| 2 | +import express from 'express' |
| 3 | +import { |
| 4 | + discoverProjectStyles, |
| 5 | + loadStyleDefinitions, |
| 6 | + createCriticalStyleStream, |
| 7 | +} from 'used-styles' |
| 8 | + |
| 9 | +// Constants |
| 10 | +const isProduction = process.env.NODE_ENV === 'production' |
| 11 | +const port = process.env.PORT || 5173 |
| 12 | +const base = process.env.BASE || '/' |
| 13 | +const ABORT_DELAY = 10000 |
| 14 | + |
| 15 | +// generate lookup table on server start |
| 16 | +const stylesLookup = isProduction |
| 17 | + ? discoverProjectStyles('./dist/client') |
| 18 | + // in dev mode vite injects all styles to <head/> element |
| 19 | + : loadStyleDefinitions(async () => []) |
| 20 | + |
| 21 | +// Cached production assets |
| 22 | +const templateHtml = isProduction |
| 23 | + ? await fs.readFile('./dist/client/index.html', 'utf-8') |
| 24 | + : '' |
| 25 | +const ssrManifest = isProduction |
| 26 | + ? await fs.readFile('./dist/client/.vite/ssr-manifest.json', 'utf-8') |
| 27 | + : undefined |
| 28 | + |
| 29 | +// Create http server |
| 30 | +const app = express() |
| 31 | + |
| 32 | +// Add Vite or respective production middlewares |
| 33 | +let vite |
| 34 | +if (!isProduction) { |
| 35 | + const { createServer } = await import('vite') |
| 36 | + vite = await createServer({ |
| 37 | + server: { middlewareMode: true }, |
| 38 | + appType: 'custom', |
| 39 | + base |
| 40 | + }) |
| 41 | + app.use(vite.middlewares) |
| 42 | +} else { |
| 43 | + const compression = (await import('compression')).default |
| 44 | + const sirv = (await import('sirv')).default |
| 45 | + app.use(compression()) |
| 46 | + app.use(base, sirv('./dist/client', { extensions: [] })) |
| 47 | +} |
| 48 | + |
| 49 | +// Serve HTML |
| 50 | +app.use('*', async (req, res) => { |
| 51 | + try { |
| 52 | + await stylesLookup |
| 53 | + |
| 54 | + const url = req.originalUrl.replace(base, '') |
| 55 | + |
| 56 | + let template |
| 57 | + let render |
| 58 | + if (!isProduction) { |
| 59 | + // Always read fresh template in development |
| 60 | + template = await fs.readFile('./index.html', 'utf-8') |
| 61 | + template = await vite.transformIndexHtml(url, template) |
| 62 | + render = (await vite.ssrLoadModule('/src/entry-server.tsx')).render |
| 63 | + } else { |
| 64 | + template = templateHtml |
| 65 | + render = (await import('./dist/server/entry-server.js')).render |
| 66 | + } |
| 67 | + |
| 68 | + const styledStream = createCriticalStyleStream(stylesLookup) |
| 69 | + |
| 70 | + let didError = false |
| 71 | + |
| 72 | + const { pipe, abort } = render(url, ssrManifest, { |
| 73 | + onShellError() { |
| 74 | + res.status(500) |
| 75 | + res.set({ 'Content-Type': 'text/html' }) |
| 76 | + res.send('<h1>Something went wrong</h1>') |
| 77 | + }, |
| 78 | + // Can use also `onAllReady` callback |
| 79 | + onShellReady() { |
| 80 | + res.status(didError ? 500 : 200) |
| 81 | + res.set({ 'Content-Type': 'text/html' }) |
| 82 | + |
| 83 | + let [htmlStart, htmlEnd] = template.split(`<!--app-html-->`) |
| 84 | + |
| 85 | + // React 19 supports document metadata out of box, |
| 86 | + // but for react 18 we can use `react-helmet-async` here: |
| 87 | + // htmlStart = htmlStart.replace(`<!--app-head-->`, helmet.title.toString()) |
| 88 | + |
| 89 | + res.write(htmlStart) |
| 90 | + |
| 91 | + styledStream.pipe(res, { end: false }) |
| 92 | + |
| 93 | + pipe(styledStream) |
| 94 | + |
| 95 | + styledStream.on('end', () => { |
| 96 | + res.end(htmlEnd) |
| 97 | + }) |
| 98 | + }, |
| 99 | + onError(error) { |
| 100 | + didError = true |
| 101 | + console.error(error) |
| 102 | + // You can log crash reports here: |
| 103 | + // logServerCrashReport(error) |
| 104 | + } |
| 105 | + }) |
| 106 | + |
| 107 | + setTimeout(() => { |
| 108 | + abort() |
| 109 | + }, ABORT_DELAY) |
| 110 | + } catch (e) { |
| 111 | + vite?.ssrFixStacktrace(e) |
| 112 | + console.log(e.stack) |
| 113 | + res.status(500).end(e.stack) |
| 114 | + } |
| 115 | +}) |
| 116 | + |
| 117 | +// Start http server |
| 118 | +app.listen(port, () => { |
| 119 | + console.log(`Server started at http://localhost:${port}`) |
| 120 | +}) |
0 commit comments