-
-
Save jrson83/33c5482cc95c5a53dd599718b7e8bf64 to your computer and use it in GitHub Desktop.
vite-plugin-ssr_https
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import express from 'express' | |
import compression from 'compression' | |
import { renderPage } from 'vite-plugin-ssr' | |
import { Options } from 'sirv' | |
import fs from 'fs' | |
import path from 'path' | |
import https from 'https' | |
const isProduction = process.env.NODE_ENV === 'production' | |
const root = `${__dirname}/..` | |
startServer() | |
async function startServer() { | |
const app = express() | |
app.use(compression()) | |
const options = isProduction ? undefined : { | |
key: fs.readFileSync(path.join(__dirname,'../cert/key.pem')), | |
cert: fs.readFileSync(path.join(__dirname,'../cert/cert.pem')) | |
} | |
if (isProduction) { | |
const sirv = require('sirv') | |
const options: Options = { | |
setHeaders: (res, pathname, stats) => { | |
if (pathname.endsWith('.js')) { | |
res.setHeader('cache-control', 'max-age=604800') | |
} | |
} | |
} | |
app.use(sirv(`${root}/dist/client`, options)) | |
} else { | |
const vite = require('vite') | |
const viteDevMiddleware = ( | |
await vite.createServer({ | |
root, | |
server: { | |
middlewareMode: true | |
} | |
}) | |
).middlewares | |
app.use(viteDevMiddleware) | |
} | |
app.get('*', async (req, res, next) => { | |
const url = req.originalUrl | |
const hostName = req.hostname | |
const protocol = req.protocol | |
const host = req.headers.host | |
const pageContextInit = { | |
url, | |
hostName, | |
protocol, | |
host | |
} | |
const urlSplit = url.split('?') | |
const path = url[0] | |
let search = '' | |
if (urlSplit.length > 1) { | |
search = urlSplit[1] | |
} | |
if (path !== path.toLowerCase()) { | |
res.redirect(301, path.toLowerCase() + (search.length > 0 ? `?${search}` : '')); | |
} else { | |
const pageContext = await renderPage(pageContextInit) | |
const { httpResponse } = pageContext | |
if (!httpResponse) return next() | |
const { body, statusCode, contentType } = httpResponse | |
const notFound = (pageContext as any).notFound | |
const redirect = (pageContext as any).redirect | |
if (redirect !== undefined) { | |
const redirectUrl = `${protocol}://${host}${redirect}` | |
res.redirect(301, redirectUrl) | |
} else { | |
res.status(notFound ? 404 : statusCode).type(contentType).send(body) | |
} | |
} | |
}) | |
const port = process.env.PORT || 3000 | |
if (isProduction) { | |
app.listen(port) | |
console.log(`Server running at http://localhost:${port}`) | |
} else if (options) { | |
const server = https.createServer(options, app) | |
server.listen(3000) | |
console.log(`Server running at https://localhost:${port}`) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment