Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(node): Custom headers are not present in responses from standalone Node server in preview mode #10208

Merged
merged 5 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/spotty-dryers-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@astrojs/node": patch
"astro": patch
---

Fixes custom headers are not added to the Node standalone server responses in preview mode
1 change: 1 addition & 0 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2748,6 +2748,7 @@ export interface PreviewServerParams {
port: number;
base: string;
logger: AstroIntegrationLogger;
headers?: OutgoingHttpHeaders;
}

export type CreatePreviewServer = (
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/core/preview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export default async function preview(inlineConfig: AstroInlineConfig): Promise<
port: settings.config.server.port,
base: settings.config.base,
logger: new AstroIntegrationLogger(logger.options, settings.adapter.name),
headers: settings.config.server.headers,
});

return server;
Expand Down
13 changes: 13 additions & 0 deletions packages/integrations/node/src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ const createPreviewServer: CreatePreviewServer = async function (preview) {
const host = preview.host ?? 'localhost';
const port = preview.port ?? 4321;
const server = createServer(ssrHandler, host, port);

// If user specified custom headers append a listener
// to the server to add those headers to response
if (preview.headers) {
server.server.addListener('request', (_, res) => {
if (res.statusCode === 200) {
for (const [name, value] of Object.entries(preview.headers ?? {})) {
if (value) res.setHeader(name, value);
}
}
});
}

logListeningOn(preview.logger, server.server, options);
await new Promise<void>((resolve, reject) => {
server.server.once('listening', resolve);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@test/nodejs-preview-headers",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*",
"@astrojs/node": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello!
38 changes: 38 additions & 0 deletions packages/integrations/node/test/preview-headers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as assert from 'node:assert/strict';
import { after, before, describe, it } from 'node:test';
import nodejs from '../dist/index.js';
import { loadFixture } from './test-utils.js';

describe('Astro preview headers', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
let devPreview;
const headers = {
astro: 'test',
};

before(async () => {
fixture = await loadFixture({
root: './fixtures/preview-headers/',
output: 'server',
adapter: nodejs({ mode: 'standalone' }),
server: {
headers
},
});
await fixture.build();
devPreview = await fixture.preview();
});

after(async () => {
await devPreview.stop();
});

describe('Preview Headers', () => {
it('returns custom headers for valid URLs', async () => {
const result = await fixture.fetch('/');
assert.equal(result.status, 200);
assert.equal(Object.fromEntries(result.headers).astro, headers.astro);
});
});
});
94 changes: 94 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading