Skip to content

Commit 54c47e5

Browse files
vicbanonrig
andauthored
perf: low-hanging fruits (#939)
Co-authored-by: Yagiz Nizipli <[email protected]>
1 parent 0ada6dc commit 54c47e5

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

.changeset/public-trees-call.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/cloudflare": patch
3+
---
4+
5+
perf: low-hanging fruits

packages/cloudflare/src/cli/build/patches/plugins/require-hook.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import { getCrossPlatformPathRegex } from "@opennextjs/aws/utils/regex.js";
55
import type { Plugin } from "esbuild";
66

77
export function shimRequireHook(options: BuildOptions): Plugin {
8+
const emptyShimPath = join(options.outputDir, "cloudflare-templates/shims/empty.js");
89
return {
910
name: "replaceRelative",
1011
setup(build) {
1112
// Note: we (empty) shim require-hook modules as they generate problematic code that uses requires
1213
build.onResolve(
1314
{ filter: getCrossPlatformPathRegex(String.raw`^\./require-hook$`, { escape: false }) },
1415
() => ({
15-
path: join(options.outputDir, "cloudflare-templates/shims/empty.js"),
16+
path: emptyShimPath,
1617
})
1718
);
1819
},

packages/cloudflare/src/cli/templates/images.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export type LocalPattern = {
1313
search?: string;
1414
};
1515

16+
let NEXT_IMAGE_REGEXP: RegExp;
17+
1618
/**
1719
* Fetches an images.
1820
*
@@ -27,17 +29,18 @@ export async function fetchImage(fetcher: Fetcher | undefined, imageUrl: string,
2729

2830
// Local
2931
if (imageUrl.startsWith("/")) {
30-
let pathname: string;
31-
let url: URL;
32-
try {
33-
// We only need pathname and search
34-
url = new URL(imageUrl, "http://n");
35-
pathname = decodeURIComponent(url.pathname);
36-
} catch {
32+
// @ts-expect-error TS2339 Missing types for URL.parse
33+
const url = URL.parse(imageUrl, "http://n");
34+
35+
if (url == null) {
3736
return getUrlErrorResponse();
3837
}
3938

40-
if (/\/_next\/image($|\/)/.test(pathname)) {
39+
// This method will never throw because URL parser will handle invalid input.
40+
const pathname = decodeURIComponent(url.pathname);
41+
42+
NEXT_IMAGE_REGEXP ??= /\/_next\/image($|\/)/;
43+
if (NEXT_IMAGE_REGEXP.test(pathname)) {
4144
return getUrlErrorResponse();
4245
}
4346

packages/cloudflare/src/cli/templates/skew-protection.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export const DEPLOYMENT_MAPPING_ENV_NAME = "CF_DEPLOYMENT_MAPPING";
55
/** Version used for the latest worker */
66
export const CURRENT_VERSION_ID = "current";
77

8+
let deploymentMapping: Record<string, string>;
9+
810
/**
911
* Routes the request to the requested deployment.
1012
*
@@ -42,16 +44,16 @@ export function maybeGetSkewProtectionResponse(request: Request): Promise<Respon
4244
return undefined;
4345
}
4446

45-
const mapping = process.env[DEPLOYMENT_MAPPING_ENV_NAME]
47+
deploymentMapping ??= process.env[DEPLOYMENT_MAPPING_ENV_NAME]
4648
? JSON.parse(process.env[DEPLOYMENT_MAPPING_ENV_NAME])
4749
: {};
4850

49-
if (!(requestDeploymentId in mapping)) {
51+
if (!(requestDeploymentId in deploymentMapping)) {
5052
// Unknown deployment id, serve the current version
5153
return undefined;
5254
}
5355

54-
const version = mapping[requestDeploymentId];
56+
const version = deploymentMapping[requestDeploymentId];
5557

5658
if (!version || version === CURRENT_VERSION_ID) {
5759
return undefined;

0 commit comments

Comments
 (0)