An open-source, self-hosted system for apps built with web technologies.
Cache immutable uploads
zohayrslilehc50fe7331843
diff --git a/source/server/view/http/uploads.ts b/source/server/view/http/uploads.tsindex 5426c86..55762bd 100644--- a/source/server/view/http/uploads.ts+++ b/source/server/view/http/uploads.ts@@ -7,6 +7,8 @@ import { isUploadFile } from "@phreshos/core"import { readFile } from "node:fs/promises"import { wallpaperKind, wallpaperSizeLimit } from "@shared/wallpaper"+const immutableCache = "public, max-age=31536000, immutable"+const wallpaperPolicy = ["default-src 'none'","script-src 'unsafe-inline' data: blob:",@@ -91,7 +93,11 @@ export default function (application: Application) {const upload = application.uploads.stat(context.req.param("file"))- return upload ? context.json(upload) : context.body(null, 404)+ if (!upload) return context.body(null, 404)++ context.header("Cache-Control", immutableCache)++ return context.json(upload)}catch (error) {@@ -115,6 +121,7 @@ export default function (application: Application) {context.header("Content-Security-Policy", wallpaperPolicy)context.header("Content-Type", "text/html; charset=utf-8")context.header("X-Content-Type-Options", "nosniff")+ context.header("Cache-Control", immutableCache)return context.body(Uint8Array.from(await readFile(application.uploads.path(file))))}@@ -142,6 +149,7 @@ export default function (application: Application) {await next()+ context.header("Cache-Control", immutableCache)context.header("Access-Control-Allow-Origin", "*")})diff --git a/tests/uploads.test.ts b/tests/uploads.test.tsindex a7ee6fb..ed1dc30 100644--- a/tests/uploads.test.ts+++ b/tests/uploads.test.ts@@ -64,6 +64,8 @@ test("uploads contract", async () => {assert.equal(described.status, 200)assert.equal(downloaded.status, 200)+ assert.equal(described.headers.get("cache-control"), "public, max-age=31536000, immutable")+ assert.equal(downloaded.headers.get("cache-control"), "public, max-age=31536000, immutable")assert.deepEqual(await described.json(), { size: created.size, modifiedAt: created.modifiedAt })assert.deepEqual(JSON.parse(await downloaded.text()), { ready: true })@@ -72,6 +74,7 @@ test("uploads contract", async () => {const wallpaper = await view.request(`http://system/uploads/wallpaper/${htmlFile}`)assert.equal(wallpaper.status, 200)+ assert.equal(wallpaper.headers.get("cache-control"), "public, max-age=31536000, immutable")assert.equal(wallpaper.headers.get("content-type"), "text/html; charset=utf-8")assert.match(wallpaper.headers.get("content-security-policy") ?? "", /connect-src 'none'/)assert.match(wallpaper.headers.get("content-security-policy") ?? "", /script-src 'unsafe-inline' data: blob:/)