Skip to content

Commit

Permalink
Merge pull request #2 from Eugenio161288/code-clean-up
Browse files Browse the repository at this point in the history
Code clean up
  • Loading branch information
coffee-lover16 authored Feb 26, 2019
2 parents 92cb01e + 98fc45e commit f949b73
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 23 deletions.
22 changes: 13 additions & 9 deletions packages/sitecore-jss-proxy/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
# Sitecore JavaScript Services Proxy
# WeAreYou Sitecore JavaScript Services Proxy

This module is provided as a part of Sitecore JavaScript Services. It contains the headless-mode SSR proxy implementation with some critical defects.
This module is provided as a part of Sitecore JavaScript Services. It contains the headless-mode SSR proxy implementation with some critical defects and new features.
You can find Sitecore JSS source code [here](https://github.com/Sitecore/jss).

The **2.3.0** version includes the following defects:
## The **2.3.0** version includes the following defects:

1. [301 is not valid response](https://github.com/Sitecore/jss/issues/138)
2. [Query string aren't transferred properly](https://github.com/Sitecore/jss/issues/140)
3. [CURL URL returns 500 OK](https://github.com/Sitecore/jss/issues/160)
4. Build LayoutService url with sc_site param during rendering page (no github bug)
5. Build LayoutService url for unsupported languages(new **supportedLanguages** is available in proxy configuration)
- [301 is not valid response](https://github.com/Sitecore/jss/issues/138)
- [Query string aren't transferred properly](https://github.com/Sitecore/jss/issues/140)
- [CURL URL returns 500 OK](https://github.com/Sitecore/jss/issues/160)

## The **2.3.0** version includes the following features:

Consult the [Sitecore JSS documentation](https://jss.sitecore.net) for further details.
- Build LayoutService url with **sc_site** and **sc_lang** params during rendering page (no github bug)
- Build LayoutService url for unsupported languages(new **supportedLanguages** param is available in proxy configuration)


You can find the Sitecore JSS documentation [here](https://jss.sitecore.net).

4 changes: 2 additions & 2 deletions packages/sitecore-jss-proxy/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@weareyou/sitecore-jss-proxy",
"version": "2.3.0",
"description": "Proxy middleware for express.js server with setting redirect statuses + added empty rendering(for curl with -IL).",
"version": "2.3.1",
"description": "Proxy middleware for express.js server with setting redirect statuses.",
"main": "dist/index.js",
"scripts": {
"build": "npm run clean && tsc",
Expand Down
19 changes: 9 additions & 10 deletions packages/sitecore-jss-proxy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ async function renderAppToResponse(
}

async function replyWithRedirect() {
// default redirect is 301 Redirect
const redirectResponse = {
statusCode: proxyResponse.statusCode || 301,
content: proxyResponse.statusMessage || 'Redirect',
Expand All @@ -147,6 +148,7 @@ async function renderAppToResponse(
}

async function replyWithEmptyBody() {
// default empty response is 200 OK
const emptyResponse = {
statusCode: proxyResponse.statusCode || 200,
content: proxyResponse.statusMessage || 'OK',
Expand Down Expand Up @@ -249,16 +251,16 @@ async function renderAppToResponse(
if (!layoutServiceData && proxyResponse.statusCode === 200) {
return replyWithEmptyBody();
}

if (!layoutServiceData && proxyResponse.statusCode !== 301 && proxyResponse.statusCode !== 302) {
// if response status code 301 or 302 then redirect will be executed
if (proxyResponse.statusCode === 301 || proxyResponse.statusCode === 302) {
return replyWithRedirect();
}
// original response: if no 200 without body, no 301/302 and no data from layout service the error will be thrown
if (!layoutServiceData) {
throw new Error(
`Received invalid response ${proxyResponse.statusCode} ${proxyResponse.statusMessage}`
);
}

if (proxyResponse.statusCode === 301 || proxyResponse.statusCode === 302) {
return replyWithRedirect();
}
return renderer(
handleRenderingResult,
(request as any).originalUrl,
Expand Down Expand Up @@ -373,13 +375,10 @@ export function rewriteRequestPath(

if (lang) {
path = buildLayoutServiceUrl(path, lang);
// unsupported languages
// build URL for unsupported language
if (config.supportedLanguages !== undefined && !config.supportedLanguages.includes(lang)) {
path = buildLayoutServiceUrlForLang(config.layoutServiceRoute, lang, config.apiKey);
}
// if (lang !== 'en' && lang !== 'nl') {
// path = buildLayoutServiceUrlForLang(config.layoutServiceRoute, lang, config.apiKey);
// }
}

if (qs) {
Expand Down
4 changes: 2 additions & 2 deletions packages/sitecore-jss-proxy/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ export const buildQueryString = (params: any) =>
.map((k) => `${encodeURIComponent(k)}=${encodeURIComponent(params[k])}`)
.join('&');

/** Builds layout service URL for site resolver with sc_site param */
/** Builds layout service URL for site resolver with sc_site and sc_lang params */
export const buildLayoutServiceUrl = (path: string, lang: string): string => {
let finalLang = 'nl-NL';
let scSiteLang = '';
if (lang === 'en') {
scSiteLang = '_en';
finalLang = 'en';
finalLang = lang;
}
const result = `${path}&sc_lang=${finalLang}&sc_site=weareyou${scSiteLang}`;

Expand Down

0 comments on commit f949b73

Please sign in to comment.