|
|
|
@ -1,6 +1,8 @@
|
|
|
|
|
import path from 'path'
|
|
|
|
|
import fs from 'fs/promises'
|
|
|
|
|
|
|
|
|
|
import dotenv from 'dotenv'
|
|
|
|
|
|
|
|
|
|
import express, { Handler, Router } from 'express'
|
|
|
|
|
import morgan from 'morgan'
|
|
|
|
|
|
|
|
|
@ -9,6 +11,9 @@ import { createServer as createViteServer } from 'vite'
|
|
|
|
|
import { createApiRouter } from './server/routes'
|
|
|
|
|
import { RenderFunction } from './shared/ssr'
|
|
|
|
|
|
|
|
|
|
// Load ".env"
|
|
|
|
|
dotenv.config()
|
|
|
|
|
|
|
|
|
|
const HTML_ROUTES = ['/', '/login', '/problem/:id', '/admin', '/profile']
|
|
|
|
|
|
|
|
|
|
const config = {
|
|
|
|
@ -47,8 +52,7 @@ async function createDevRouter() {
|
|
|
|
|
const transformedTemplate = await vite.transformIndexHtml(req.originalUrl, indexHtml)
|
|
|
|
|
|
|
|
|
|
// Load (to be bundled) entry point for server side rendering
|
|
|
|
|
const render: RenderFunction = (await vite.ssrLoadModule('./client/entry-server.tsx'))
|
|
|
|
|
.default
|
|
|
|
|
const render: RenderFunction = (await vite.ssrLoadModule('./client/entry-server.tsx')).default
|
|
|
|
|
|
|
|
|
|
const { html, metadata } = render(req.originalUrl)
|
|
|
|
|
|
|
|
|
@ -58,9 +62,7 @@ async function createDevRouter() {
|
|
|
|
|
const fullUrl = `${req.protocol}://${req.get('host')}${req.originalUrl}`
|
|
|
|
|
const metaTagsHtml =
|
|
|
|
|
'' +
|
|
|
|
|
(metadata.title
|
|
|
|
|
? `<meta property="og:title" content="${metadata.title}" />\n`
|
|
|
|
|
: '') +
|
|
|
|
|
(metadata.title ? `<meta property="og:title" content="${metadata.title}" />\n` : '') +
|
|
|
|
|
(metadata.description
|
|
|
|
|
? `<meta property="og:description" content="${metadata.description}" />\n`
|
|
|
|
|
: '') +
|
|
|
|
@ -91,10 +93,7 @@ async function createProductionRouter() {
|
|
|
|
|
r.use('/', express.static('dist/entry-client'))
|
|
|
|
|
|
|
|
|
|
mountIndexHtmlRoutes(r, async (req, res) => {
|
|
|
|
|
const transformedTemplate = await fs.readFile(
|
|
|
|
|
path.resolve('./dist/entry-client/index.html'),
|
|
|
|
|
'utf-8'
|
|
|
|
|
)
|
|
|
|
|
const transformedTemplate = await fs.readFile(path.resolve('./dist/entry-client/index.html'), 'utf-8')
|
|
|
|
|
|
|
|
|
|
const { html, metadata } = render(req.originalUrl)
|
|
|
|
|
|
|
|
|
|