Skip to content

Commit b322728

Browse files
committed
cache options now optional
1 parent 67f26a5 commit b322728

2 files changed

Lines changed: 70 additions & 67 deletions

File tree

packages/ink/src/cache.ts

Lines changed: 67 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -21,73 +21,76 @@ export default function cache(options: CacheOptions) {
2121
client: options.clientPath,
2222
manifest: options.manifestPath
2323
};
24+
//if there's a server path
25+
if (paths.server) {
26+
//on pre render, try to use cache
27+
emitter.on('render', (event: Event<string>) => {
28+
//extract props and builder from params
29+
const props = (event.params.props || {}) as Hash;
30+
const builder = event.params.builder as Builder;
31+
//get id ie. abc123c
32+
const id = serialize(builder.document.source);
33+
//get cache file path ie. /path/to/docs/build/server/abc123c.js
34+
const cache = path.join(paths.server, `${id}.js`);
35+
//if cache file exists
36+
if (fs.existsSync(cache)) {
37+
//get the build object
38+
const build = require(cache);
39+
const Document = build.default as ServerDocumentClass;
40+
const document = new Document();
41+
//render the document
42+
const html = document.render(props);
43+
//return the cached content
44+
event.set(html);
45+
}
46+
});
2447

25-
//on pre render, try to use cache
26-
emitter.on('render', (event: Event<string>) => {
27-
//extract props and builder from params
28-
const props = (event.params.props || {}) as Hash;
29-
const builder = event.params.builder as Builder;
30-
//get id ie. abc123c
31-
const id = serialize(builder.document.source);
32-
//get cache file path ie. /path/to/docs/build/server/abc123c.js
33-
const cache = path.join(paths.server, `${id}.js`);
34-
//if cache file exists
35-
if (fs.existsSync(cache)) {
36-
//get the build object
37-
const build = require(cache);
38-
const Document = build.default as ServerDocumentClass;
39-
const document = new Document();
40-
//render the document
41-
const html = document.render(props);
42-
//return the cached content
43-
event.set(html);
44-
}
45-
});
46-
47-
//on pre client build, try to use cache
48-
emitter.on('build-client', (event: Event<string>) => {
49-
//extract builder from params
50-
const builder = event.params.builder as Builder;
51-
//get fs and id ie. abc123c
52-
const id = serialize(builder.document.source);
53-
//get cache file path ie. /path/to/docs/build/client/abc123c.js
54-
const cache = path.join(paths.client, `${id}.js`);
55-
//if cache file exists, send it
56-
if (fs.existsSync(cache)) {
57-
event.set(fs.readFileSync(cache, 'utf8'));
58-
}
59-
});
60-
61-
//on pre server build, try to use cache
62-
emitter.on('build-server', (event: Event<string>) => {
63-
//extract builder from params
64-
const builder = event.params.builder as Builder;
65-
//get fs and id ie. abc123c
66-
const id = serialize(builder.document.source);
67-
//get cache file path ie. /path/to/docs/build/server/abc123c.js
68-
const cache = path.join(paths.server, `${id}.js`);
69-
//if cache file exists, send it
70-
if (fs.existsSync(cache)) {
71-
event.set(fs.readFileSync(cache, 'utf8'));
72-
}
73-
});
74-
75-
//on pre styles build, try to use cache
76-
emitter.on('build-styles', (event: Event<string>) => {
77-
//extract builder from params
78-
const builder = event.params.builder as Builder;
79-
//get fs and id ie. abc123c
80-
const id = serialize(builder.document.source);
81-
//get cache file path ie. /path/to/docs/build/client/abc123c.css
82-
const cache = path.join(paths.client, `${id}.css`);
83-
//if cache file exists, send it
84-
if (fs.existsSync(cache)) {
85-
event.set(fs.readFileSync(cache, 'utf8'));
86-
}
87-
});
48+
//on pre server build, try to use cache
49+
emitter.on('build-server', (event: Event<string>) => {
50+
//extract builder from params
51+
const builder = event.params.builder as Builder;
52+
//get fs and id ie. abc123c
53+
const id = serialize(builder.document.source);
54+
//get cache file path ie. /path/to/docs/build/server/abc123c.js
55+
const cache = path.join(paths.server, `${id}.js`);
56+
//if cache file exists, send it
57+
if (fs.existsSync(cache)) {
58+
event.set(fs.readFileSync(cache, 'utf8'));
59+
}
60+
});
61+
}
62+
//if there's a client path
63+
if (paths.client) {
64+
//on pre client build, try to use cache
65+
emitter.on('build-client', (event: Event<string>) => {
66+
//extract builder from params
67+
const builder = event.params.builder as Builder;
68+
//get fs and id ie. abc123c
69+
const id = serialize(builder.document.source);
70+
//get cache file path ie. /path/to/docs/build/client/abc123c.js
71+
const cache = path.join(paths.client, `${id}.js`);
72+
//if cache file exists, send it
73+
if (fs.existsSync(cache)) {
74+
event.set(fs.readFileSync(cache, 'utf8'));
75+
}
76+
});
8877

78+
//on pre styles build, try to use cache
79+
emitter.on('build-styles', (event: Event<string>) => {
80+
//extract builder from params
81+
const builder = event.params.builder as Builder;
82+
//get fs and id ie. abc123c
83+
const id = serialize(builder.document.source);
84+
//get cache file path ie. /path/to/docs/build/client/abc123c.css
85+
const cache = path.join(paths.client, `${id}.css`);
86+
//if cache file exists, send it
87+
if (fs.existsSync(cache)) {
88+
event.set(fs.readFileSync(cache, 'utf8'));
89+
}
90+
});
91+
}
8992
//if there's a manifest
90-
if (fs.existsSync(paths.manifest)) {
93+
if (paths.manifest && fs.existsSync(paths.manifest)) {
9194
//load the manifest file
9295
compiler.manifest.load(
9396
JSON.parse(fs.readFileSync(paths.manifest, 'utf-8'))

packages/ink/src/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -583,9 +583,9 @@ export type ManifestOptions = ComponentOptions & BuilderOptions;
583583
export type InkOptions = ManifestOptions;
584584

585585
export type CacheOptions = {
586-
serverPath: string,
587-
clientPath: string,
588-
manifestPath: string
586+
serverPath?: string,
587+
clientPath?: string,
588+
manifestPath?: string
589589
};
590590

591591
//--------------------------------------------------------------------//

0 commit comments

Comments
 (0)