Skip to content

Commit

Permalink
chore: simplify template entry.server files (#8909)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-ebey committed Feb 27, 2024
1 parent 6a2a4dd commit d7c9ea5
Show file tree
Hide file tree
Showing 15 changed files with 350 additions and 674 deletions.
26 changes: 13 additions & 13 deletions integration/headers-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ test.describe("headers export", () => {
let response = await appFixture.requestDocument("/parent");
expect(JSON.stringify(Array.from(response.headers.entries()))).toBe(
JSON.stringify([
["content-type", "text/html"],
["content-type", "text/html; charset=utf-8"],
["x-parent-loader", "success"],
])
);
Expand All @@ -282,7 +282,7 @@ test.describe("headers export", () => {
let response = await appFixture.requestDocument("/parent/child");
expect(JSON.stringify(Array.from(response.headers.entries()))).toBe(
JSON.stringify([
["content-type", "text/html"],
["content-type", "text/html; charset=utf-8"],
["x-parent-loader", "success"],
])
);
Expand All @@ -295,7 +295,7 @@ test.describe("headers export", () => {
);
expect(JSON.stringify(Array.from(response.headers.entries()))).toBe(
JSON.stringify([
["content-type", "text/html"],
["content-type", "text/html; charset=utf-8"],
["x-parent-action", "success"],
["x-parent-loader", "success"],
])
Expand All @@ -309,7 +309,7 @@ test.describe("headers export", () => {
);
expect(JSON.stringify(Array.from(response.headers.entries()))).toBe(
JSON.stringify([
["content-type", "text/html"],
["content-type", "text/html; charset=utf-8"],
["x-parent-loader", "success"],
])
);
Expand All @@ -319,7 +319,7 @@ test.describe("headers export", () => {
let response = await appFixture.requestDocument("/parent?throw=parent");
expect(JSON.stringify(Array.from(response.headers.entries()))).toBe(
JSON.stringify([
["content-type", "text/html"],
["content-type", "text/html; charset=utf-8"],
["x-parent-loader", "error, error"], // Shows up in loaderHeaders and errorHeaders
])
);
Expand All @@ -331,7 +331,7 @@ test.describe("headers export", () => {
);
expect(JSON.stringify(Array.from(response.headers.entries()))).toBe(
JSON.stringify([
["content-type", "text/html"],
["content-type", "text/html; charset=utf-8"],
["x-child-loader", "error"],
["x-parent-loader", "success"],
])
Expand All @@ -344,7 +344,7 @@ test.describe("headers export", () => {
);
expect(JSON.stringify(Array.from(response.headers.entries()))).toBe(
JSON.stringify([
["content-type", "text/html"],
["content-type", "text/html; charset=utf-8"],
["x-parent-loader", "error, error"], // Shows up in loaderHeaders and errorHeaders
])
);
Expand All @@ -356,7 +356,7 @@ test.describe("headers export", () => {
);
expect(JSON.stringify(Array.from(response.headers.entries()))).toBe(
JSON.stringify([
["content-type", "text/html"],
["content-type", "text/html; charset=utf-8"],
["x-child-loader", "error"],
["x-parent-loader", "success"],
])
Expand All @@ -370,7 +370,7 @@ test.describe("headers export", () => {
);
expect(JSON.stringify(Array.from(response.headers.entries()))).toBe(
JSON.stringify([
["content-type", "text/html"],
["content-type", "text/html; charset=utf-8"],
["x-parent-action", "error, error"], // Shows up in actionHeaders and errorHeaders
])
);
Expand All @@ -383,7 +383,7 @@ test.describe("headers export", () => {
);
expect(JSON.stringify(Array.from(response.headers.entries()))).toBe(
JSON.stringify([
["content-type", "text/html"],
["content-type", "text/html; charset=utf-8"],
["x-child-action", "error"],
])
);
Expand All @@ -393,7 +393,7 @@ test.describe("headers export", () => {
let response = await appFixture.requestDocument("/cookie/child");
expect(JSON.stringify(Array.from(response.headers.entries()))).toBe(
JSON.stringify([
["content-type", "text/html"],
["content-type", "text/html; charset=utf-8"],
["set-cookie", "normal-cookie=true"],
])
);
Expand All @@ -403,7 +403,7 @@ test.describe("headers export", () => {
let response = await appFixture.requestDocument("/cookie/child?throw");
expect(JSON.stringify(Array.from(response.headers.entries()))).toBe(
JSON.stringify([
["content-type", "text/html"],
["content-type", "text/html; charset=utf-8"],
["set-cookie", "thrown-cookie=true"],
])
);
Expand All @@ -413,7 +413,7 @@ test.describe("headers export", () => {
let response = await appFixture.requestDocument("/cookie?parent-throw");
expect(JSON.stringify(Array.from(response.headers.entries()))).toBe(
JSON.stringify([
["content-type", "text/html"],
["content-type", "text/html; charset=utf-8"],
["set-cookie", "parent-thrown-cookie=true"],
])
);
Expand Down
34 changes: 27 additions & 7 deletions packages/remix-dev/config/defaults/entry.server.cloudflare.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* By default, Remix will handle generating the HTTP Response for you.
* You are free to delete this file if you'd like to, but if you ever want it revealed again, you can run `npx remix reveal` ✨
* For more information, see https://remix.run/file-conventions/entry.server
*/

import type { AppLoadContext, EntryContext } from "@remix-run/cloudflare";
import { RemixServer } from "@remix-run/react";
import * as isbotModule from "isbot";
Expand All @@ -8,31 +14,45 @@ export default async function handleRequest(
responseStatusCode: number,
responseHeaders: Headers,
remixContext: EntryContext,
// This is ignored so we can keep it in the template for visibility. Feel
// free to delete this parameter in your app if you're not using it!
// eslint-disable-next-line @typescript-eslint/no-unused-vars
loadContext: AppLoadContext
) {
let status = responseStatusCode;
const headers = new Headers(responseHeaders);
headers.set("Content-Type", "text/html; charset=utf-8");
headers.set("Transfer-Encoding", "chunked");

let shellRendered = false;
const body = await renderToReadableStream(
<RemixServer context={remixContext} url={request.url} />,
{
signal: request.signal,
onError(error: unknown) {
// Log streaming rendering errors from inside the shell
console.error(error);
responseStatusCode = 500;
status = 500;
// Log streaming rendering errors from inside the shell. Don't log
// errors encountered during initial shell rendering since they'll
// reject and get logged in handleDocumentRequest.
if (shellRendered) {
console.error(error);
}
},
}
);
shellRendered = true;

if (isBotRequest(request.headers.get("user-agent"))) {
if (isBotRequest(request.headers.get("user-agent") || "")) {
await body.allReady;
}

responseHeaders.set("Content-Type", "text/html");
return new Response(body, {
headers: responseHeaders,
status: responseStatusCode,
headers,
status,
});
}


// We have some Remix apps in the wild already running with isbot@3 so we need
// to maintain backwards compatibility even though we want new apps to use
// isbot@4. That way, we can ship this as a minor Semver update to @remix-run/dev.
Expand Down
34 changes: 27 additions & 7 deletions packages/remix-dev/config/defaults/entry.server.deno.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* By default, Remix will handle generating the HTTP Response for you.
* You are free to delete this file if you'd like to, but if you ever want it revealed again, you can run `npx remix reveal` ✨
* For more information, see https://remix.run/file-conventions/entry.server
*/

import type { AppLoadContext, EntryContext } from "@remix-run/deno";
import { RemixServer } from "@remix-run/react";
import * as isbotModule from "isbot";
Expand All @@ -8,31 +14,45 @@ export default async function handleRequest(
responseStatusCode: number,
responseHeaders: Headers,
remixContext: EntryContext,
// This is ignored so we can keep it in the template for visibility. Feel
// free to delete this parameter in your app if you're not using it!
// eslint-disable-next-line @typescript-eslint/no-unused-vars
loadContext: AppLoadContext
) {
let status = responseStatusCode;
const headers = new Headers(responseHeaders);
headers.set("Content-Type", "text/html; charset=utf-8");
headers.set("Transfer-Encoding", "chunked");

let shellRendered = false;
const body = await renderToReadableStream(
<RemixServer context={remixContext} url={request.url} />,
{
signal: request.signal,
onError(error: unknown) {
// Log streaming rendering errors from inside the shell
console.error(error);
responseStatusCode = 500;
status = 500;
// Log streaming rendering errors from inside the shell. Don't log
// errors encountered during initial shell rendering since they'll
// reject and get logged in handleDocumentRequest.
if (shellRendered) {
console.error(error);
}
},
}
);
shellRendered = true;

if (isBotRequest(request.headers.get("user-agent"))) {
if (isBotRequest(request.headers.get("user-agent") || "")) {
await body.allReady;
}

responseHeaders.set("Content-Type", "text/html");
return new Response(body, {
headers: responseHeaders,
status: responseStatusCode,
headers,
status,
});
}


// We have some Remix apps in the wild already running with isbot@3 so we need
// to maintain backwards compatibility even though we want new apps to use
// isbot@4. That way, we can ship this as a minor Semver update to @remix-run/dev.
Expand Down
Loading

0 comments on commit d7c9ea5

Please sign in to comment.