Skip to content

Commit

Permalink
feat: Added a way to handle 404 error from IlcAppSdk (#33)
Browse files Browse the repository at this point in the history
* feat: handle 404 error

* fix: handling status codes
  • Loading branch information
Volodymyr Makukha authored Dec 9, 2021
1 parent a322c46 commit 2a0439d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ 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 (this.adapter.setStatusCode) {
this.adapter.setStatusCode(404);
} else {
window.dispatchEvent(
new CustomEvent('ilc:404', {
detail: { appId: this.appId },
}),
);
}
};

unmount() {
this.intl.unmount();
Expand Down
4 changes: 4 additions & 0 deletions src/app/interfaces/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ 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;
setStatusCode: (code: number) => void;
getStatusCode: () => number | undefined;
}

export type Render404 = () => void;

export interface IntlConfig {
locale?: string;
currency?: string;
Expand Down
11 changes: 11 additions & 0 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,6 +65,10 @@ export class IlcSdk {
getCurrentPathProps: () => passedProps,
appId,
intl: this.parseIntl(req),
setStatusCode: (code) => {
statusCode = code;
},
getStatusCode: () => statusCode,
};
}

Expand All @@ -72,6 +78,11 @@ 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 {
const statusCode = reqData.getStatusCode();
if (statusCode) {
res.statusCode = statusCode;
}

if (!data) {
return;
}
Expand Down
13 changes: 13 additions & 0 deletions test/server/IlcSdk.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,19 @@ 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.setStatusCode(NotFound);
ilcSdk.processResponse(pRes, res);

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

describe('appAssets', () => {
it('should handle absolute URLs', () => {
const req = new MockReq(merge({}, defReq));
Expand Down

0 comments on commit 2a0439d

Please sign in to comment.