Skip to content

Commit

Permalink
refactor: custom 404 page
Browse files Browse the repository at this point in the history
  • Loading branch information
nightnei committed Dec 16, 2021
1 parent f618320 commit 503ada9
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 25 deletions.
12 changes: 1 addition & 11 deletions src/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import { IlcIntl } from './IlcIntl';
import defaultIntlAdapter from './defaultIntlAdapter';
import { IIlcAppSdk } from './interfaces/IIlcAppSdk';
import { Render404 } from './interfaces/common';
import ResponseStatus from './interfaces/ResponseStatus';

export * from './types';
export * from './GlobalBrowserApi';
Expand Down Expand Up @@ -87,16 +86,7 @@ export default class IlcAppSdk implements IIlcAppSdk {
render404: Render404 = (isCustomPage) => {
// SSR
if (this.adapter.setStatus) {
const status: ResponseStatus = {
code: 404,
};

if (isCustomPage) {
status.headers = {
['X-ILC-Override']: 'error-page-content',
};
}
this.adapter.setStatus(status);
this.adapter.setStatus(404, isCustomPage);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/interfaces/ResponseStatus.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type ResponseStatus = {
code?: number;
code: number;
headers?: Record<string, string>;
};

Expand Down
2 changes: 1 addition & 1 deletion src/app/interfaces/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ 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;
setStatus: (data: ResponseStatus) => void;
setStatus: (code: number, isCustomPage?: boolean) => void;
getStatus: () => ResponseStatus | undefined;
}

Expand Down
10 changes: 8 additions & 2 deletions src/server/IlcSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,14 @@ export class IlcSdk {
getCurrentPathProps: () => passedProps,
appId,
intl: this.parseIntl(req),
setStatus: (status) => {
responseStatus = status;
setStatus: (code, isCustomPage) => {
responseStatus = { code };

if (isCustomPage) {
responseStatus.headers = {
['X-ILC-Override']: 'error-page-content',
};
}
},
getStatus: () => responseStatus,
};
Expand Down
13 changes: 3 additions & 10 deletions test/server/IlcSdk.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,31 +299,24 @@ describe('IlcSdk', () => {
const res = new MockRes();

const pRes = ilcSdk.processRequest(req);
pRes.setStatus({ code: NotFound });
pRes.setStatus(NotFound);
ilcSdk.processResponse(pRes, res);

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

it('should set 404 status code and header for custom error', () => {
const NotFound = 404;
const NotFoundHeaderKey = 'X-ILC-Override';
const NotFoundHeaderValue = 'error-page-content';

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

const pRes = ilcSdk.processRequest(req);
pRes.setStatus({
code: NotFound,
headers: {
[NotFoundHeaderKey]: NotFoundHeaderValue,
},
});
pRes.setStatus(NotFound, true);
ilcSdk.processResponse(pRes, res);

expect(res.statusCode).to.eq(NotFound);
expect(res.getHeader(NotFoundHeaderKey)).to.eq(NotFoundHeaderValue);
expect(res.getHeader('X-ILC-Override')).to.eq('error-page-content');
});

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

0 comments on commit 503ada9

Please sign in to comment.