summaryrefslogtreecommitdiff
path: root/packages/integrations/node/src/http-server.ts
diff options
context:
space:
mode:
authorGravatar bluwy <bjornlu.dev@gmail.com> 2023-11-30 22:42:29 +0800
committerGravatar bluwy <bjornlu.dev@gmail.com> 2023-11-30 22:42:29 +0800
commit05628aaa3c9a3702b59cbf3d0e99abf66df651df (patch)
treea5212b8b8f12d734579db8a2bb90dd4c772cba44 /packages/integrations/node/src/http-server.ts
parent9c2342c327a13d2f7d1eb387b743e81f431b9813 (diff)
parentff8eadb95d34833baaf3ec7575bf4f293eae97da (diff)
downloadastro-05628aaa3c9a3702b59cbf3d0e99abf66df651df.tar.gz
astro-05628aaa3c9a3702b59cbf3d0e99abf66df651df.tar.zst
astro-05628aaa3c9a3702b59cbf3d0e99abf66df651df.zip
Merge branch 'main' into next
Diffstat (limited to '')
-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;