diff --git a/packages/sitecore-jss-proxy/package-lock.json b/packages/sitecore-jss-proxy/package-lock.json index 9a0ed3eac3..0bf821bf32 100644 --- a/packages/sitecore-jss-proxy/package-lock.json +++ b/packages/sitecore-jss-proxy/package-lock.json @@ -729,6 +729,11 @@ "micromatch": "^4.0.2" } }, + "http-status-codes": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/http-status-codes/-/http-status-codes-1.3.2.tgz", + "integrity": "sha512-nDUtj0ltIt08tGi2VWSpSzNNFye0v3YSe9lX3lIqLTuVvvRiYCvs4QQBSHo0eomFYw1wlUuofurUAlTm+vHnXg==" + }, "ignore": { "version": "5.1.4", "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.1.4.tgz", diff --git a/packages/sitecore-jss-proxy/package.json b/packages/sitecore-jss-proxy/package.json index 92047938d0..ec90d3bfb5 100644 --- a/packages/sitecore-jss-proxy/package.json +++ b/packages/sitecore-jss-proxy/package.json @@ -25,6 +25,7 @@ }, "dependencies": { "http-proxy-middleware": "^0.20.0", + "http-status-codes": "^1.3.2", "set-cookie-parser": "^2.2.1" }, "devDependencies": { diff --git a/packages/sitecore-jss-proxy/src/ProxyConfig.ts b/packages/sitecore-jss-proxy/src/ProxyConfig.ts index a66bb174ce..34ce5cd136 100644 --- a/packages/sitecore-jss-proxy/src/ProxyConfig.ts +++ b/packages/sitecore-jss-proxy/src/ProxyConfig.ts @@ -1,4 +1,4 @@ -import { ClientRequest, IncomingMessage, ServerResponse } from 'http'; +import { IncomingMessage, ServerResponse } from 'http'; import { Config as HttpProxyConfig } from 'http-proxy-middleware'; import { RenderResponse } from './RenderResponse'; @@ -34,19 +34,19 @@ export interface ProxyConfig { /** Enables transforming SSR'ed HTML after it is rendered, i.e. to replace paths. */ transformSSRContent?: ( response: RenderResponse, - request: ClientRequest, + request: IncomingMessage, serverResponse: ServerResponse ) => Promise; /** Hook to fill the SSR viewBag object; if you're customizing the viewBag in Sitecore integrated SSR mode, do the same here. */ createViewBag?: ( - request: ClientRequest, + request: IncomingMessage, response: ServerResponse, proxyResponse: IncomingMessage, layoutServiceData: any ) => Promise; /** Hook to alter HTTP headers in a custom way. */ setHeaders?: ( - request: ClientRequest, + request: IncomingMessage, response: ServerResponse, proxyResponse: IncomingMessage ) => void; diff --git a/packages/sitecore-jss-proxy/src/index.ts b/packages/sitecore-jss-proxy/src/index.ts index 05cc25aa43..6c6deb4794 100644 --- a/packages/sitecore-jss-proxy/src/index.ts +++ b/packages/sitecore-jss-proxy/src/index.ts @@ -1,6 +1,7 @@ -import { ClientRequest, IncomingMessage, ServerResponse } from 'http'; +import { IncomingMessage, ServerResponse } from 'http'; import proxy from 'http-proxy-middleware'; import setCookieParser from 'set-cookie-parser'; +import HttpStatus from 'http-status-codes'; import zlib from 'zlib'; // node.js standard lib import { AppRenderer } from './AppRenderer'; import { ProxyConfig } from './ProxyConfig'; @@ -36,7 +37,7 @@ export const removeEmptyAnalyticsCookie = (proxyResponse: any) => { // inspired by: http://stackoverflow.com/a/22487927/9324 async function renderAppToResponse( proxyResponse: IncomingMessage, - request: ClientRequest, + request: IncomingMessage, serverResponse: ServerResponse, renderer: AppRenderer, config: ProxyConfig @@ -87,8 +88,13 @@ async function renderAppToResponse( return true; }; + if (request.method === 'HEAD') { + completeProxyResponse(null, proxyResponse.statusCode || HttpStatus.OK) + return + } + async function extractLayoutServiceDataFromProxyResponse(): Promise { - if (proxyResponse.statusCode === 200 || proxyResponse.statusCode === 404) { + if (proxyResponse.statusCode === HttpStatus.OK || proxyResponse.statusCode === HttpStatus.NOT_FOUND) { let responseString: Promise; if ( @@ -125,7 +131,7 @@ async function renderAppToResponse( console.error(error); let errorResponse = { - statusCode: proxyResponse.statusCode || 500, + statusCode: proxyResponse.statusCode || HttpStatus.INTERNAL_SERVER_ERROR, content: proxyResponse.statusMessage || 'Internal Server Error', }; @@ -176,7 +182,7 @@ async function renderAppToResponse( proxyResponse.headers['location'] = result.redirect; } - const finalStatusCode = result.status || proxyResponse.statusCode || 200; + const finalStatusCode = result.status || proxyResponse.statusCode || HttpStatus.OK; if (config.debug) { console.log( @@ -190,13 +196,15 @@ async function renderAppToResponse( completeProxyResponse(content, finalStatusCode); } - function completeProxyResponse(content: Buffer, statusCode: number, headers?: any) { + function completeProxyResponse(content: Buffer | null, statusCode: number, headers?: any) { if (!headers) { headers = proxyResponse.headers; } originalWriteHead.apply(serverResponse, [statusCode, headers]); - originalWrite.call(serverResponse, content); + + if (content) originalWrite.call(serverResponse, content); + originalEnd.call(serverResponse); } @@ -343,7 +351,7 @@ export function rewriteRequestPath( let path = `${config.layoutServiceRoute}?item=${encodeURIComponent(finalReqPath)}&sc_apikey=${ config.apiKey - }`; + }`; if (lang) { path = `${path}&sc_lang=${lang}`;