this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

perf: low-hanging fruits (#939)

Co-authored-by: Yagiz Nizipli <yagiz@nizipli.com>

authored by

Victor Berchet
Yagiz Nizipli
and committed by
GitHub
54c47e5b 0ada6dcf

+23 -12
+5
.changeset/public-trees-call.md
··· 1 + --- 2 + "@opennextjs/cloudflare": patch 3 + --- 4 + 5 + perf: low-hanging fruits
+2 -1
packages/cloudflare/src/cli/build/patches/plugins/require-hook.ts
··· 5 5 import type { Plugin } from "esbuild"; 6 6 7 7 export function shimRequireHook(options: BuildOptions): Plugin { 8 + const emptyShimPath = join(options.outputDir, "cloudflare-templates/shims/empty.js"); 8 9 return { 9 10 name: "replaceRelative", 10 11 setup(build) { ··· 12 13 build.onResolve( 13 14 { filter: getCrossPlatformPathRegex(String.raw`^\./require-hook$`, { escape: false }) }, 14 15 () => ({ 15 - path: join(options.outputDir, "cloudflare-templates/shims/empty.js"), 16 + path: emptyShimPath, 16 17 }) 17 18 ); 18 19 },
+11 -8
packages/cloudflare/src/cli/templates/images.ts
··· 13 13 search?: string; 14 14 }; 15 15 16 + let NEXT_IMAGE_REGEXP: RegExp; 17 + 16 18 /** 17 19 * Fetches an images. 18 20 * ··· 27 29 28 30 // Local 29 31 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) { 37 36 return getUrlErrorResponse(); 38 37 } 39 38 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)) { 41 44 return getUrlErrorResponse(); 42 45 } 43 46
+5 -3
packages/cloudflare/src/cli/templates/skew-protection.ts
··· 5 5 /** Version used for the latest worker */ 6 6 export const CURRENT_VERSION_ID = "current"; 7 7 8 + let deploymentMapping: Record<string, string>; 9 + 8 10 /** 9 11 * Routes the request to the requested deployment. 10 12 * ··· 42 44 return undefined; 43 45 } 44 46 45 - const mapping = process.env[DEPLOYMENT_MAPPING_ENV_NAME] 47 + deploymentMapping ??= process.env[DEPLOYMENT_MAPPING_ENV_NAME] 46 48 ? JSON.parse(process.env[DEPLOYMENT_MAPPING_ENV_NAME]) 47 49 : {}; 48 50 49 - if (!(requestDeploymentId in mapping)) { 51 + if (!(requestDeploymentId in deploymentMapping)) { 50 52 // Unknown deployment id, serve the current version 51 53 return undefined; 52 54 } 53 55 54 - const version = mapping[requestDeploymentId]; 56 + const version = deploymentMapping[requestDeploymentId]; 55 57 56 58 if (!version || version === CURRENT_VERSION_ID) { 57 59 return undefined;