diff options
author | 2023-11-28 08:46:26 -0500 | |
---|---|---|
committer | 2023-11-28 08:46:26 -0500 | |
commit | 8f1d509574f5ee5d77816a13d89ce452dce403ff (patch) | |
tree | 56bfd6f61b019bc1cb9e49798cae21af35dae5a3 /packages/integrations/node/src/http-server.ts | |
parent | d90714fc3dd7c3eab0a6b29319b0b666bb04b678 (diff) | |
download | astro-8f1d509574f5ee5d77816a13d89ce452dce403ff.tar.gz astro-8f1d509574f5ee5d77816a13d89ce452dce403ff.tar.zst astro-8f1d509574f5ee5d77816a13d89ce452dce403ff.zip |
Support immutable cache headers for _astro assets (#9125)
* Support immutable cache headers for _astro assets
* Update .changeset/twelve-fishes-fail.md
Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
* Update packages/integrations/node/src/http-server.ts
* Update expected max-age
* Add teh docs
* Update .changeset/twelve-fishes-fail.md
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
* Update packages/integrations/node/README.md
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
---------
Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Diffstat (limited to 'packages/integrations/node/src/http-server.ts')
-rw-r--r-- | packages/integrations/node/src/http-server.ts | 16 |
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..73773538e 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; |