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

feat: handle 404 error #33

Merged
merged 2 commits into from
Dec 9, 2021
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
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 = () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add JS Doc

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