summaryrefslogtreecommitdiff
path: root/packages/integrations/node/src
diff options
context:
space:
mode:
Diffstat (limited to 'packages/integrations/node/src')
-rw-r--r--packages/integrations/node/src/http-server.ts16
-rw-r--r--packages/integrations/node/src/index.ts3
-rw-r--r--packages/integrations/node/src/preview.ts3
-rw-r--r--packages/integrations/node/src/server.ts1
-rw-r--r--packages/integrations/node/src/standalone.ts1
-rw-r--r--packages/integrations/node/src/types.ts1
6 files changed, 23 insertions, 2 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;
diff --git a/packages/integrations/node/src/index.ts b/packages/integrations/node/src/index.ts
index 1f3707949..bac5c25ef 100644
--- a/packages/integrations/node/src/index.ts
+++ b/packages/integrations/node/src/index.ts
@@ -6,7 +6,7 @@ export function getAdapter(options: Options): AstroAdapter {
name: '@astrojs/node',
serverEntrypoint: '@astrojs/node/server.js',
previewEntrypoint: '@astrojs/node/preview.js',
- exports: ['handler', 'startServer'],
+ exports: ['handler', 'startServer', 'options'],
args: options,
supportedAstroFeatures: {
hybridOutput: 'stable',
@@ -49,6 +49,7 @@ export default function createIntegration(userOptions: UserOptions): AstroIntegr
server: config.build.server?.toString(),
host: config.server.host,
port: config.server.port,
+ assets: config.build.assets,
};
setAdapter(getAdapter(_options));
diff --git a/packages/integrations/node/src/preview.ts b/packages/integrations/node/src/preview.ts
index 70ed54698..89baa1897 100644
--- a/packages/integrations/node/src/preview.ts
+++ b/packages/integrations/node/src/preview.ts
@@ -17,11 +17,13 @@ const preview: CreatePreviewServer = async function ({
type ServerModule = ReturnType<typeof createExports>;
type MaybeServerModule = Partial<ServerModule>;
let ssrHandler: ServerModule['handler'];
+ let options: ServerModule['options'];
try {
process.env.ASTRO_NODE_AUTOSTART = 'disabled';
const ssrModule: MaybeServerModule = await import(serverEntrypoint.toString());
if (typeof ssrModule.handler === 'function') {
ssrHandler = ssrModule.handler;
+ options = ssrModule.options!;
} else {
throw new AstroError(
`The server entrypoint doesn't have a handler. Are you sure this is the right file?`
@@ -59,6 +61,7 @@ const preview: CreatePreviewServer = async function ({
port,
host,
removeBase,
+ assets: options.assets,
},
handler
);
diff --git a/packages/integrations/node/src/server.ts b/packages/integrations/node/src/server.ts
index 90bf8c44c..88bcd7d62 100644
--- a/packages/integrations/node/src/server.ts
+++ b/packages/integrations/node/src/server.ts
@@ -8,6 +8,7 @@ applyPolyfills();
export function createExports(manifest: SSRManifest, options: Options) {
const app = new NodeApp(manifest);
return {
+ options: options,
handler: middleware(app, options.mode),
startServer: () => startServer(app, options),
};
diff --git a/packages/integrations/node/src/standalone.ts b/packages/integrations/node/src/standalone.ts
index abe40ff5c..e167e8ab6 100644
--- a/packages/integrations/node/src/standalone.ts
+++ b/packages/integrations/node/src/standalone.ts
@@ -52,6 +52,7 @@ export default function startServer(app: NodeApp, options: Options) {
port,
host,
removeBase: app.removeBase.bind(app),
+ assets: options.assets,
},
handler
);
diff --git a/packages/integrations/node/src/types.ts b/packages/integrations/node/src/types.ts
index 85f4f4fbc..1917d8cf3 100644
--- a/packages/integrations/node/src/types.ts
+++ b/packages/integrations/node/src/types.ts
@@ -15,6 +15,7 @@ export interface Options extends UserOptions {
port: number;
server: string;
client: string;
+ assets: string;
}
export type RequestHandlerParams = [