Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/356373 #273

Merged
merged 16 commits into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/sitecore-jss-proxy/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/sitecore-jss-proxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},
"dependencies": {
"http-proxy-middleware": "^0.20.0",
"http-status-codes": "^1.3.2",
"set-cookie-parser": "^2.2.1"
},
"devDependencies": {
Expand Down
8 changes: 4 additions & 4 deletions packages/sitecore-jss-proxy/src/ProxyConfig.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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<string>;
/** 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<object>;
/** Hook to alter HTTP headers in a custom way. */
setHeaders?: (
request: ClientRequest,
request: IncomingMessage,
response: ServerResponse,
proxyResponse: IncomingMessage
) => void;
Expand Down
24 changes: 16 additions & 8 deletions packages/sitecore-jss-proxy/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -87,8 +88,13 @@ async function renderAppToResponse(
return true;
};

if (request.method === 'HEAD') {
completeProxyResponse(null, proxyResponse.statusCode || HttpStatus.OK)
return
}

async function extractLayoutServiceDataFromProxyResponse(): Promise<any> {
if (proxyResponse.statusCode === 200 || proxyResponse.statusCode === 404) {
if (proxyResponse.statusCode === HttpStatus.OK || proxyResponse.statusCode === HttpStatus.NOT_FOUND) {
let responseString: Promise<string>;

if (
Expand Down Expand Up @@ -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',
};

Expand Down Expand Up @@ -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(
Expand All @@ -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);
}

Expand Down Expand Up @@ -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}`;
Expand Down