Skip to content

Commit

Permalink
fix(server): response headers for static and 404 routes (#1194)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdawgs authored Jan 18, 2023
1 parent 216b744 commit 30ae9b4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ async function getConfig() {
version,
// Redoc specific extension to support loading image in docs
"x-logo": {
url: "/images/docsmith-logo-transparent-background-wide-canvas.png",
url: "/public/images/docsmith-logo-transparent-background-wide-canvas.png",
backgroundColor: "#005EB8",
altText: "Docsmith Logo",
},
Expand Down
14 changes: 7 additions & 7 deletions src/routes/docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<meta name="author" content="Frazer Smith" />
<meta name="viewport" content="width=device-width, initial-scale=1" />

<link rel="stylesheet" href="/docs.css" type="text/css" />
<link rel="stylesheet" href="/public/docs.css" type="text/css" />

<!-- Icons -->
<link rel="apple-touch-icon" sizes="120x120" href="/images/icons/apple-touch-icon-120x120.png" />
<link rel="apple-touch-icon" sizes="152x152" href="/images/icons/apple-touch-icon-152x152.png" />
<link rel="apple-touch-icon" sizes="167x167" href="/images/icons/apple-touch-icon-167x167.png" />
<link rel="apple-touch-icon" sizes="180x180" href="/images/icons/apple-touch-icon-180x180.png" />
<link rel="shortcut icon" type="image/x-icon" href="/images/icons/favicon.ico" />
<link rel="mask-icon" color="#005EB8" href="/images/icons/mask-icon.svg" />
<link rel="apple-touch-icon" sizes="120x120" href="/public/images/icons/apple-touch-icon-120x120.png" />
<link rel="apple-touch-icon" sizes="152x152" href="/public/images/icons/apple-touch-icon-152x152.png" />
<link rel="apple-touch-icon" sizes="167x167" href="/public/images/icons/apple-touch-icon-167x167.png" />
<link rel="apple-touch-icon" sizes="180x180" href="/public/images/icons/apple-touch-icon-180x180.png" />
<link rel="shortcut icon" type="image/x-icon" href="/public/images/icons/favicon.ico" />
<link rel="mask-icon" color="#005EB8" href="/public/images/icons/mask-icon.svg" />
<meta name="theme-color" content="#005EB8" />

<title>Docsmith | Documentation</title>
Expand Down
10 changes: 10 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,21 @@ async function plugin(server, config) {
// Set relaxed response headers
.register(helmet, relaxedHelmetConfig)

// Stop fastify-disablecache overwriting @fastify/static's cache headers
.addHook("onRequest", async (req, res) => {
res.removeHeader("cache-control")
.removeHeader("expires")
.removeHeader("pragma")
.removeHeader("surrogate-control");
})

// Register static files in public
.register(staticPlugin, {
root: path.joinSafe(__dirname, "public"),
immutable: true,
maxAge: "365 days",
prefix: "/public/",
wildcard: false,
})
.register(autoLoad, {
dir: path.joinSafe(__dirname, "routes", "docs"),
Expand Down
37 changes: 35 additions & 2 deletions src/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ const expResHeadersHtmlStatic = {
vary: "accept-encoding",
};

const expeResHeadersPublicImage = {
...expResHeaders,
"accept-ranges": "bytes",
"cache-control": "public, max-age=31536000, immutable",
"content-length": expect.any(Number), // @fastify/static plugin returns content-length as number
"content-type": expect.stringContaining("image/"),
etag: expect.any(String),
expires: undefined,
"last-modified": expect.any(String),
pragma: undefined,
"surrogate-control": undefined,
vary: "accept-encoding",
};

const expResHeadersJson = {
...expResHeaders,
"content-type": expect.stringContaining("application/json"),
Expand All @@ -64,7 +78,11 @@ const expResHeadersText = {

const expResHeaders4xxErrors = {
...expResHeadersJson,
"keep-alive": undefined,
vary: undefined,
};

const expResHeaders5xxErrors = {
...expResHeadersJson,
vary: "accept-encoding",
};

Expand Down Expand Up @@ -863,6 +881,21 @@ describe("Server Deployment", () => {
expect(response.statusCode).toBe(200);
});
});

describe("/public Route", () => {
test("Should return image", async () => {
const response = await server.inject({
method: "GET",
url: "/public/images/icons/favicon.ico",
headers: {
accept: "*/*",
},
});

expect(response.headers).toEqual(expeResHeadersPublicImage);
expect(response.statusCode).toBe(200);
});
});
});

describe("Frontend", () => {
Expand Down Expand Up @@ -935,7 +968,7 @@ describe("Server Deployment", () => {
message: "Internal Server Error",
statusCode: 500,
});
expect(response.headers).toEqual(expResHeaders4xxErrors);
expect(response.headers).toEqual(expResHeaders5xxErrors);
expect(response.statusCode).toBe(500);
});
});
Expand Down

0 comments on commit 30ae9b4

Please sign in to comment.