Skip to content

Commit

Permalink
fix: handling status codes
Browse files Browse the repository at this point in the history
  • Loading branch information
nightnei committed Dec 8, 2021
1 parent cbd13ab commit 934d615
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
12 changes: 8 additions & 4 deletions src/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,20 @@ export default class IlcAppSdk implements IIlcAppSdk {
const intlAdapter = this.adapter.intl ? this.adapter.intl : defaultIntlAdapter;
this.intl = new IlcIntl(this.appId, intlAdapter);
}

/**
* Isomorphic method to render 404 page.
* At SSR in processResponse it sets 404 status code to response.
* At CSR it triggers global event which ILC listens and renders 404 page.
*/
render404 = () => {
if (typeof window !== 'undefined') {
if (this.adapter.setStatusCode) {
this.adapter.setStatusCode(404);
} else {
window.dispatchEvent(
new CustomEvent('ilc:404', {
detail: { appId: this.appId },
}),
);
} else {
this.adapter._is404 = true;
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/app/interfaces/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export interface AppSdkAdapter {
/** Unique application ID, if same app will be rendered twice on a page - it will get different IDs */
appId: string;
intl: IntlAdapter | null;
/** Uses in "processResponse" to set 404 status code */
_is404: boolean;
setStatusCode: (code: number) => void;
getStatusCode: () => number | undefined;
}

export type Render404 = () => void;
Expand Down
12 changes: 9 additions & 3 deletions src/server/IlcSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export class IlcSdk {
originalUri = '/';
}

let statusCode: number | undefined;

return {
getCurrentReqHost: () => host,
getCurrentReqUrl: () => requestedUrls.requestUrl,
Expand All @@ -63,7 +65,10 @@ export class IlcSdk {
getCurrentPathProps: () => passedProps,
appId,
intl: this.parseIntl(req),
_is404: false,
setStatusCode: (code) => {
statusCode = code;
},
getStatusCode: () => statusCode,
};
}

Expand All @@ -73,8 +78,9 @@ export class IlcSdk {
* **WARNING:** this method should be called before response headers were send.
*/
public processResponse(reqData: types.RequestData, res: ServerResponse, data?: types.ResponseData): void {
if (reqData._is404) {
res.statusCode = 404;
const statusCode = reqData.getStatusCode();
if (statusCode) {
res.statusCode = statusCode;
}

if (!data) {
Expand Down
6 changes: 4 additions & 2 deletions test/server/IlcSdk.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,16 @@ describe('IlcSdk', () => {
});

it('should set 404 status code', () => {
const NotFound = 404;

const req = new MockReq(merge({}, defReq));
const res = new MockRes();

const pRes = ilcSdk.processRequest(req);
pRes._is404 = true;
pRes.setStatusCode(NotFound);
ilcSdk.processResponse(pRes, res);

expect(res.statusCode).to.eq(404);
expect(res.statusCode).to.eq(NotFound);
});

describe('appAssets', () => {
Expand Down

0 comments on commit 934d615

Please sign in to comment.