client: proxy html render requests so they can be cached easier

This commit is contained in:
Max Leiter 2022-03-24 18:24:09 -07:00
parent 5aca059953
commit a61f2c00e2
No known key found for this signature in database
GPG key ID: A3512F2F2F17EBDA
2 changed files with 25 additions and 1 deletions

View file

@ -15,7 +15,7 @@ const MarkdownPreview = ({ height = 500, fileId, content, title }: Props) => {
useEffect(() => { useEffect(() => {
async function fetchPost() { async function fetchPost() {
if (fileId) { if (fileId) {
const resp = await fetch(`/server-api/files/html/${fileId}`, { const resp = await fetch(`/api/html/${fileId}`, {
method: "GET", method: "GET",
}) })
if (resp.ok) { if (resp.ok) {

View file

@ -0,0 +1,24 @@
import { NextApiRequest, NextApiResponse } from "next"
const getRawFile = async (req: NextApiRequest, res: NextApiResponse) => {
const { id } = req.query
const file = await fetch(`${process.env.API_URL}/files/html/${id}`, {
headers: {
"x-secret-key": process.env.SECRET_KEY || "",
Authorization: `Bearer ${req.cookies["drift-token"]}`
}
})
if (file.ok) {
const json = await file.text()
const data = json
// serve the file raw as plain text
res.setHeader("Content-Type", "text/plain; charset=utf-8")
res.setHeader("Cache-Control", "s-maxage=86400")
res.status(200).write(data, "utf-8")
res.end()
} else {
res.status(404).send("File not found")
}
}
export default getRawFile