Skip to content

Commit

Permalink
feat: Configurable Request Cache (#220)
Browse files Browse the repository at this point in the history
* Configurable Request Cache

* requestFromDynamicRoute test

* Synax

* Label

* Remove ?. for set

* Phrasing

* snapshot
  • Loading branch information
Catgroove authored Nov 16, 2021
1 parent b61aa14 commit 64bdc70
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions src/routes/__tests__/prepareRouter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,22 @@ describe('#prepareRouter', () => {
);
expect(requestCache.has('/dev/reports/hereitis/')).toBeTruthy();
});
it('Parses a dynamic route into a request properly and skips the cache', () => {
expect(
requestFromDynamicRoute({
req: { path: '/dev/reports/without-cache/' },
requestCache: undefined,
dynamicRoutes,
}),
).toEqual({
permalink: '',
report: 'without-cache',
req: { path: '/dev/reports/without-cache/', query: undefined, search: undefined },
route: 'reports',
type: 'server',
});
expect(requestCache.has('/dev/reports/without-cache/')).toBeFalsy();
});
it('correctly adds in different search and query params', () => {
expect(
requestFromDynamicRoute({
Expand Down
10 changes: 6 additions & 4 deletions src/routes/prepareRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ export const initialRequestIsWellFormed = (request: RequestOptions) =>
interface IRequestFromDynamicRoute {
req: Req;
dynamicRoutes: RouteOptions[];
requestCache: Map<string, RequestOptions>;
requestCache: Map<string, RequestOptions> | undefined;
}

export function requestFromDynamicRoute({
req,
dynamicRoutes,
requestCache,
}: IRequestFromDynamicRoute): RequestOptions | false {
if (requestCache.has(req.path)) {
if (requestCache && requestCache.has(req.path)) {
const request = requestCache.get(req.path);
request.req = req;
return request;
Expand All @@ -96,7 +96,9 @@ export function requestFromDynamicRoute({
type: 'server',
...params,
};
requestCache.set(req.path, request);
if (requestCache) {
requestCache.set(req.path, request);
}
request.req = { ...req };
return request;
}
Expand All @@ -105,7 +107,7 @@ export function requestFromDynamicRoute({

function prepareRouter(Elder) {
const { routes, serverLookupObject, settings, ...elder } = Elder;
const requestCache = new Map();
const requestCache = settings.server.cacheRequests ? new Map() : undefined;

// sort the routes in order of specificity
const dynamicRoutes: RouteOptions[] = routeSort(
Expand Down
28 changes: 28 additions & 0 deletions src/utils/__tests__/__snapshots__/validations.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,7 @@ Object {
"_exclusive": Object {},
"_mutate": undefined,
"_nodes": Array [
"cacheRequests",
"prefix",
],
"_options": Object {
Expand All @@ -802,6 +803,33 @@ Object {
"refs": Map {},
},
"fields": Object {
"cacheRequests": Object {
"_blacklist": Object {
"list": Set {},
"refs": Map {},
},
"_conditions": Array [],
"_default": true,
"_deps": Array [],
"_exclusive": Object {},
"_label": "If Elder.js should cache requests when using dynamic routing. It may be useful to turn off the cache when combining Elder.js with another caching solution to reduce the resources consumed by the server.",
"_mutate": undefined,
"_options": Object {
"abortEarly": true,
"recursive": true,
},
"_type": "boolean",
"_typeError": [Function],
"_whitelist": Object {
"list": Set {},
"refs": Map {},
},
"tests": Array [],
"transforms": Array [
[Function],
],
"type": "boolean",
},
"prefix": Object {
"_blacklist": Object {
"list": Set {},
Expand Down
1 change: 1 addition & 0 deletions src/utils/__tests__/validations.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ describe('#validations', () => {
},
server: {
prefix: '',
cacheRequests: true,
},
shortcodes: {
closePattern: '}}',
Expand Down
1 change: 1 addition & 0 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Page from './Page';

type ServerOptions = {
prefix: string;
cacheRequests?: boolean;
};

type BuildOptions = {
Expand Down
7 changes: 7 additions & 0 deletions src/utils/validations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ const configSchema = yup.object({
.notRequired()
.default('')
.label(`If Elder.js should serve all pages with a prefix. (deprecated)`),
cacheRequests: yup
.boolean()
.notRequired()
.default(true)
.label(
`If Elder.js should cache requests when using dynamic routing. It may be useful to turn off the cache when combining Elder.js with another caching solution to reduce the resources consumed by the server.`,
),
}),
prefix: yup
.string()
Expand Down

0 comments on commit 64bdc70

Please sign in to comment.