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

feat: merge exisiting headers with ones loader/action sets #406

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/eight-planes-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@mcansh/remix-fastify": minor
---

merge exisiting fastify reply headers with ones loader/action sets
35 changes: 29 additions & 6 deletions packages/remix-fastify/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import "@remix-run/node/install";
import type { MockedFunction } from "vitest";
import { afterAll, afterEach, describe, expect, it, vi } from "vitest";

import type { GetLoadContextFunction, HttpServer } from "../src/server";
import {
createRemixHeaders,
createRemixRequest,
Expand All @@ -31,18 +32,20 @@ let mockedCreateRequestHandler = createRemixRequestHandler as MockedFunction<
typeof createRemixRequestHandler
>;

function createApp() {
function createApp(getLoadContext?: GetLoadContextFunction<HttpServer>) {
let app = fastify();

app.all(
"*",
createRequestHandler({
app.all("*", (request, reply) => {
let handler = createRequestHandler({
// We don't have a real app to test, but it doesn't matter. We
// won't ever call through to the real createRequestHandler
// @ts-expect-error
build: undefined,
}),
);
getLoadContext,
});

return handler(request, reply);
});

return app;
}
Expand Down Expand Up @@ -134,6 +137,26 @@ describe("fastify createRequestHandler", () => {
expect(response.statusCode).toBe(204);
});

it("merges headers set by the server and by remix", async () => {
mockedCreateRequestHandler.mockImplementation(() => async () => {
return new Response(null, {
headers: { Link: "</style.css>; rel=preload; as=style" },
});
});

let app = createApp((_request, reply) => {
reply.header("Link", "</script.js>; rel=preload; as=script");
return {};
});

let response = await app.inject("/");

expect(response.headers["link"]).toEqual([
"</style.css>; rel=preload; as=style",
"</script.js>; rel=preload; as=script",
]);
});

it("sets headers", async () => {
mockedCreateRequestHandler.mockImplementation(() => async () => {
let headers = new Headers({ "X-Time-Of-Year": "most wonderful" });
Expand Down
2 changes: 1 addition & 1 deletion packages/remix-fastify/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export let remixFastify = fp<RemixFastifyOptions>(
done(null, payload);
});

let basepath = basename.replace(/\/+$/, "") + "/*";
let basepath = basename.replace(/\/+$/, "/*");

childServer.all(basepath, remixHandler);
});
Expand Down
32 changes: 30 additions & 2 deletions packages/remix-fastify/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,36 @@ export async function sendRemixResponse<Server extends HttpServer>(
): Promise<void> {
reply.status(nodeResponse.status);

for (let [key, values] of nodeResponse.headers.entries()) {
reply.headers({ [key]: values });
let appliedHeaders = new Map<string, Set<string>>();

for (let [key, value] of nodeResponse.headers.entries()) {
let currentFastifyHeader = reply.getHeader(key);
let currentValues = appliedHeaders.get(key) || new Set();

if (currentFastifyHeader) {
if (Array.isArray(currentFastifyHeader)) {
for (let header of currentFastifyHeader) {
currentValues.add(value);
currentValues.add(header);
}
} else {
currentValues.add(value);
currentValues.add(currentFastifyHeader as string);
}
} else {
currentValues.add(value);
}

appliedHeaders.set(key, currentValues);
}

for (let [key, values] of appliedHeaders.entries()) {
if (values.size === 1) {
reply.header(key, values.values().next().value);
continue;
}

reply.header(key, [...values]);
}

if (nodeResponse.body) {
Expand Down