Skip to content

Commit c35e7f0

Browse files
brophdawg11fifi98
andauthored
Handle isbot v4 in a backwards-compatible manner (#8415)
Co-authored-by: Filip Bel <[email protected]>
1 parent 7c3b050 commit c35e7f0

File tree

11 files changed

+78
-15
lines changed

11 files changed

+78
-15
lines changed

integration/defer-test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ test.describe("aborted", () => {
985985
import type { AppLoadContext, EntryContext } from "@remix-run/node";
986986
import { createReadableStreamFromReadable } from "@remix-run/node";
987987
import { RemixServer } from "@remix-run/react";
988-
import isbot from "isbot";
988+
import { isbot } from "isbot";
989989
import { renderToPipeableStream } from "react-dom/server";
990990
991991
const ABORT_DELAY = 1;
@@ -997,7 +997,7 @@ test.describe("aborted", () => {
997997
remixContext: EntryContext,
998998
loadContext: AppLoadContext
999999
) {
1000-
return isbot(request.headers.get("user-agent"))
1000+
return isbot(request.headers.get("user-agent") || "")
10011001
? handleBotRequest(
10021002
request,
10031003
responseStatusCode,

integration/helpers/vite-template/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"@remix-run/node": "*",
1313
"@remix-run/react": "*",
1414
"@remix-run/serve": "*",
15-
"isbot": "^3.6.8",
15+
"isbot": "^4.1.0",
1616
"react": "^18.2.0",
1717
"react-dom": "^18.2.0"
1818
},

integration/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"get-port": "^5.1.1",
2222
"glob": "8.0.3",
2323
"globby": "^11.1.0",
24-
"isbot": "^3.6.8",
24+
"isbot": "^4.1.0",
2525
"npm-run-all": "^4.1.5",
2626
"pidtree": "^0.6.0",
2727
"postcss": "^8.4.19",

packages/remix-dev/__tests__/fixtures/cloudflare/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"@remix-run/cloudflare-pages": "*",
1414
"@remix-run/css-bundle": "*",
1515
"@remix-run/react": "*",
16-
"isbot": "^3.6.8",
16+
"isbot": "^4.1.0",
1717
"react": "^18.2.0",
1818
"react-dom": "^18.2.0"
1919
},

packages/remix-dev/__tests__/fixtures/deno/.vscode/resolve_npm_imports.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"@remix-run/dev/server-build": "https://esm.sh/@remix-run/[email protected]/server-build",
1414
"@remix-run/react": "https://esm.sh/@remix-run/[email protected]",
1515
"@remix-run/server-runtime": "https://esm.sh/@remix-run/[email protected]",
16-
"isbot": "https://esm.sh/isbot@^3.6.8",
16+
"isbot": "https://esm.sh/isbot@^4.1.0",
1717
"react": "https://esm.sh/react@^18.2.0",
1818
"react-dom": "https://esm.sh/react-dom@^18.2.0",
1919
"react-dom/client": "https://esm.sh/react-dom@^18.2.0/client",

packages/remix-dev/__tests__/fixtures/deno/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"@remix-run/css-bundle": "*",
1717
"@remix-run/deno": "*",
1818
"@remix-run/react": "*",
19-
"isbot": "^3.6.8",
19+
"isbot": "^4.1.0",
2020
"react": "^18.2.0",
2121
"react-dom": "^18.2.0"
2222
},

packages/remix-dev/__tests__/fixtures/node/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"@remix-run/node": "*",
1414
"@remix-run/react": "*",
1515
"@remix-run/serve": "*",
16-
"isbot": "^3.6.8",
16+
"isbot": "^4.1.0",
1717
"react": "^18.2.0",
1818
"react-dom": "^18.2.0"
1919
},

packages/remix-dev/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ export async function resolveConfig(
493493
pkgJson.update({
494494
dependencies: {
495495
...pkgJson.content.dependencies,
496-
isbot: "latest",
496+
isbot: "^4",
497497
},
498498
});
499499

packages/remix-dev/config/defaults/entry.server.cloudflare.tsx

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { AppLoadContext, EntryContext } from "@remix-run/cloudflare";
22
import { RemixServer } from "@remix-run/react";
3-
import isbot from "isbot";
3+
import * as isbotModule from "isbot";
44
import { renderToReadableStream } from "react-dom/server";
55

66
export default async function handleRequest(
@@ -22,7 +22,7 @@ export default async function handleRequest(
2222
}
2323
);
2424

25-
if (isbot(request.headers.get("user-agent"))) {
25+
if (isBotRequest(request.headers.get("user-agent"))) {
2626
await body.allReady;
2727
}
2828

@@ -32,3 +32,24 @@ export default async function handleRequest(
3232
status: responseStatusCode,
3333
});
3434
}
35+
36+
// We have some Remix apps in the wild already running with isbot@3 so we need
37+
// to maintain backwards compatibility even though we want new apps to use
38+
// isbot@4. That way, we can ship this as a minor Semver update to @remix-run/dev.
39+
function isBotRequest(userAgent: string | null) {
40+
if (!userAgent) {
41+
return false;
42+
}
43+
44+
// isbot >= 3.8.0, >4
45+
if ("isbot" in isbotModule && typeof isbotModule.isbot === "function") {
46+
return isbotModule.isbot(userAgent);
47+
}
48+
49+
// isbot < 3.8.0
50+
if ("default" in isbotModule && typeof isbotModule.default === "function") {
51+
return isbotModule.default(userAgent);
52+
}
53+
54+
return false;
55+
}

packages/remix-dev/config/defaults/entry.server.deno.tsx

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { AppLoadContext, EntryContext } from "@remix-run/deno";
22
import { RemixServer } from "@remix-run/react";
3-
import isbot from "isbot";
3+
import * as isbotModule from "isbot";
44
import { renderToReadableStream } from "react-dom/server";
55

66
export default async function handleRequest(
@@ -22,7 +22,7 @@ export default async function handleRequest(
2222
}
2323
);
2424

25-
if (isbot(request.headers.get("user-agent"))) {
25+
if (isBotRequest(request.headers.get("user-agent"))) {
2626
await body.allReady;
2727
}
2828

@@ -32,3 +32,24 @@ export default async function handleRequest(
3232
status: responseStatusCode,
3333
});
3434
}
35+
36+
// We have some Remix apps in the wild already running with isbot@3 so we need
37+
// to maintain backwards compatibility even though we want new apps to use
38+
// isbot@4. That way, we can ship this as a minor Semver update to @remix-run/dev.
39+
function isBotRequest(userAgent: string | null) {
40+
if (!userAgent) {
41+
return false;
42+
}
43+
44+
// isbot >= 3.8.0, >4
45+
if ("isbot" in isbotModule && typeof isbotModule.isbot === "function") {
46+
return isbotModule.isbot(userAgent);
47+
}
48+
49+
// isbot < 3.8.0
50+
if ("default" in isbotModule && typeof isbotModule.default === "function") {
51+
return isbotModule.default(userAgent);
52+
}
53+
54+
return false;
55+
}

packages/remix-dev/config/defaults/entry.server.node.tsx

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { PassThrough } from "node:stream";
33
import type { AppLoadContext, EntryContext } from "@remix-run/node";
44
import { createReadableStreamFromReadable } from "@remix-run/node";
55
import { RemixServer } from "@remix-run/react";
6-
import isbot from "isbot";
6+
import * as isbotModule from "isbot";
77
import { renderToPipeableStream } from "react-dom/server";
88

99
const ABORT_DELAY = 5_000;
@@ -15,7 +15,7 @@ export default function handleRequest(
1515
remixContext: EntryContext,
1616
loadContext: AppLoadContext
1717
) {
18-
return isbot(request.headers.get("user-agent"))
18+
return isBotRequest(request.headers.get("user-agent"))
1919
? handleBotRequest(
2020
request,
2121
responseStatusCode,
@@ -30,6 +30,27 @@ export default function handleRequest(
3030
);
3131
}
3232

33+
// We have some Remix apps in the wild already running with isbot@3 so we need
34+
// to maintain backwards compatibility even though we want new apps to use
35+
// isbot@4. That way, we can ship this as a minor Semver update to @remix-run/dev.
36+
function isBotRequest(userAgent: string | null) {
37+
if (!userAgent) {
38+
return false;
39+
}
40+
41+
// isbot >= 3.8.0, >4
42+
if ("isbot" in isbotModule && typeof isbotModule.isbot === "function") {
43+
return isbotModule.isbot(userAgent);
44+
}
45+
46+
// isbot < 3.8.0
47+
if ("default" in isbotModule && typeof isbotModule.default === "function") {
48+
return isbotModule.default(userAgent);
49+
}
50+
51+
return false;
52+
}
53+
3354
function handleBotRequest(
3455
request: Request,
3556
responseStatusCode: number,

0 commit comments

Comments
 (0)