client: proxy html render requests so they can be cached easier
This commit is contained in:
parent
5aca059953
commit
a61f2c00e2
2 changed files with 25 additions and 1 deletions
|
@ -15,7 +15,7 @@ const MarkdownPreview = ({ height = 500, fileId, content, title }: Props) => {
|
|||
useEffect(() => {
|
||||
async function fetchPost() {
|
||||
if (fileId) {
|
||||
const resp = await fetch(`/server-api/files/html/${fileId}`, {
|
||||
const resp = await fetch(`/api/html/${fileId}`, {
|
||||
method: "GET",
|
||||
})
|
||||
if (resp.ok) {
|
||||
|
|
24
client/pages/api/html/[id].ts
Normal file
24
client/pages/api/html/[id].ts
Normal 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
|
Loading…
Reference in a new issue