Skip to content

Commit

Permalink
use latest handleNodeResponse, add changeset
Browse files Browse the repository at this point in the history
  • Loading branch information
markdalgleish committed Nov 2, 2023
1 parent f66f1c2 commit e202cd7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 48 deletions.
5 changes: 5 additions & 0 deletions .changeset/silent-rice-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/dev": patch
---

Handle multiple "Set-Cookie" headers in Vite dev server
61 changes: 32 additions & 29 deletions integration/vite-dev-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,41 +77,28 @@ test.describe("Vite dev", () => {
);
}
`,
"app/routes/view-cookie.tsx": js`
import { useState, useEffect } from "react";
import { json } from "@remix-run/node";
import { useLoaderData } from "@remix-run/react"
export const loader = ({ request }: LoaderFunctionArgs) => json({cookies: request.headers.get("Cookie")});
export default function IndexRoute() {
const { cookies } = useLoaderData<typeof loader>();
return (
<div id="view-cookie">
<h2 data-title>View Cookie</h2>
<p data-cookie>{cookies}</p>
</div>
);
}
`,
"app/routes/set-cookie.tsx": js`
"app/routes/set-cookies.tsx": js`
import { LoaderFunction } from "@remix-run/node";
export const loader: LoaderFunction = () => {
const headers = new Headers();
headers.append(
"Set-Cookie",
"foo=bar; Domain=localhost; Path=/; SameSite=Lax"
"first=one; Domain=localhost; Path=/; SameSite=Lax"
);
headers.append(
"Set-Cookie",
"hello=world; Domain=localhost; Path=/; SameSite=Lax"
"second=two; Domain=localhost; Path=/; SameSite=Lax"
);
headers.set("location", "http://localhost:${devPort}/view-cookie");
headers.append(
"Set-Cookie",
"third=three; Domain=localhost; Path=/; SameSite=Lax"
);
headers.set("location", "http://localhost:${devPort}/get-cookies");
const response = new Response(null, {
headers,
Expand All @@ -121,6 +108,23 @@ test.describe("Vite dev", () => {
return response;
};
`,
"app/routes/get-cookies.tsx": js`
import { json, LoaderFunctionArgs } from "@remix-run/node";
import { useLoaderData } from "@remix-run/react"
export const loader = ({ request }: LoaderFunctionArgs) => json({cookies: request.headers.get("Cookie")});
export default function IndexRoute() {
const { cookies } = useLoaderData<typeof loader>();
return (
<div id="get-cookies">
<h2 data-title>Get Cookies</h2>
<p data-cookies>{cookies}</p>
</div>
);
}
`,
},
});

Expand Down Expand Up @@ -187,16 +191,15 @@ test.describe("Vite dev", () => {
});

test("handles multiple set-cookie headers", async ({ page }) => {
// setup: initial render
await page.goto(`http://localhost:${devPort}/set-cookie`, {
await page.goto(`http://localhost:${devPort}/set-cookies`, {
waitUntil: "networkidle",
});
await expect(page.locator("#view-cookie [data-title]")).toHaveText(
"View Cookie"
);

await expect(page.locator("#view-cookie [data-cookie]")).toHaveText(
"foo=bar; hello=world"
// Ensure we redirected
expect(new URL(page.url()).pathname).toBe("/get-cookies");

await expect(page.locator("#get-cookies [data-cookies]")).toHaveText(
"first=one; second=two; third=three"
);
});
});
Expand Down
29 changes: 10 additions & 19 deletions packages/remix-dev/vite/node/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,31 +167,22 @@ function createRequest(req: IncomingMessage): Request {
return new NodeRequest(url.href, init);
}

// Adapted from more recent version of `handleNodeResponse`:
// https://github.com/solidjs/solid-start/blob/7398163869b489cce503c167e284891cf51a6613/packages/start/node/fetch.js#L162-L185
async function handleNodeResponse(webRes: Response, res: ServerResponse) {
res.statusCode = webRes.status;
res.statusMessage = webRes.statusText;

for (let [name, value] of webRes.headers) {
let existingHeader = res.getHeader(name);

let newValue;
let cookiesStrings = [];

if (name.toLowerCase() === "set-cookie") {
newValue = splitCookiesString(value);
} else {
newValue = [value];
}
for (let [name, value] of webRes.headers) {
if (name === "set-cookie") {
cookiesStrings.push(...splitCookiesString(value));
} else res.setHeader(name, value);
}

if (existingHeader) {
res.setHeader(
name,
Array.isArray(existingHeader)
? [...existingHeader, ...newValue]
: [existingHeader, ...newValue]
);
} else {
res.setHeader(name, newValue);
}
if (cookiesStrings.length) {
res.setHeader("set-cookie", cookiesStrings);
}

if (webRes.body) {
Expand Down

0 comments on commit e202cd7

Please sign in to comment.