summaryrefslogtreecommitdiff
path: root/packages/integrations/node/src/http-server.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/integrations/node/src/http-server.ts')
-rw-r--r--packages/integrations/node/src/http-server.ts16
1 files changed, 15 insertions, 1 deletions
diff --git a/packages/integrations/node/src/http-server.ts b/packages/integrations/node/src/http-server.ts
index 2f2339cdf..904937601 100644
--- a/packages/integrations/node/src/http-server.ts
+++ b/packages/integrations/node/src/http-server.ts
@@ -10,6 +10,7 @@ interface CreateServerOptions {
port: number;
host: string | undefined;
removeBase: (pathname: string) => string;
+ assets: string;
}
function parsePathname(pathname: string, host: string | undefined, port: number) {
@@ -22,9 +23,16 @@ function parsePathname(pathname: string, host: string | undefined, port: number)
}
export function createServer(
- { client, port, host, removeBase }: CreateServerOptions,
+ { client, port, host, removeBase, assets }: CreateServerOptions,
handler: http.RequestListener
) {
+ // The `base` is removed before passed to this function, so we don't
+ // need to check for it here.
+ const assetsPrefix = `/${assets}/`;
+ function isImmutableAsset(pathname: string) {
+ return pathname.startsWith(assetsPrefix);
+ }
+
const listener: http.RequestListener = (req, res) => {
if (req.url) {
let pathname: string | undefined = removeBase(req.url);
@@ -54,6 +62,12 @@ export function createServer(
// File not found, forward to the SSR handler
handler(req, res);
});
+ stream.on('headers', (_res: http.ServerResponse<http.IncomingMessage>) => {
+ if (isImmutableAsset(encodedURI)) {
+ // Taken from https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#immutable
+ _res.setHeader('Cache-Control', 'public, max-age=31536000, immutable');
+ }
+ });
stream.on('directory', () => {
// On directory find, redirect to the trailing slash
let location: string;