diff --git a/packages/kv-asset-handler/src/index.ts b/packages/kv-asset-handler/src/index.ts index 4ca62db5a8ad..c04762ad511e 100644 --- a/packages/kv-asset-handler/src/index.ts +++ b/packages/kv-asset-handler/src/index.ts @@ -93,11 +93,6 @@ const getAssetFromKV = async (event: FetchEvent, options?: Partial): Pr throw new InternalError(`there is no KV namespace bound to the script`) } - const SUPPORTED_METHODS = ['GET', 'HEAD'] - if (!SUPPORTED_METHODS.includes(request.method)) { - throw new MethodNotAllowedError(`${request.method} is not a valid request method`) - } - const rawPathKey = new URL(request.url).pathname.replace(/^\/+/, '') // strip any preceding /'s let pathIsEncoded = false let requestKey @@ -121,6 +116,11 @@ const getAssetFromKV = async (event: FetchEvent, options?: Partial): Pr } } + const SUPPORTED_METHODS = ['GET', 'HEAD'] + if (!SUPPORTED_METHODS.includes(requestKey.method)) { + throw new MethodNotAllowedError(`${requestKey.method} is not a valid request method`) + } + const parsedUrl = new URL(requestKey.url) const pathname = pathIsEncoded ? decodeURIComponent(parsedUrl.pathname) : parsedUrl.pathname // decode percentage encoded path only when necessary