Skip to content

Commit d762a7c

Browse files
authored
Add method setCache to define Cache-Control of component rendering (GitbookIO#72)
* Add method setCache to define Cache-Control of component rendering * Add changeset
1 parent 5df9eff commit d762a7c

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

Diff for: .changeset/fast-terms-agree.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@gitbook/runtime': minor
3+
---
4+
5+
Add method element.setCache during rendering of component to define the max-age

Diff for: packages/runtime/src/components.ts

+23-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,20 @@ import { RuntimeCallback, RuntimeContext } from './context';
99

1010
type PlainObject = { [key: string]: number | string | boolean | PlainObject | undefined | null };
1111

12+
export interface ComponentRenderCache {
13+
maxAge: number;
14+
}
15+
1216
export interface ComponentInstance<Props extends PlainObject, State extends PlainObject> {
1317
props: Props;
1418
state: State;
1519
context: ContentKitContext;
1620

21+
/**
22+
* Set the cache max-age for the output of this component.
23+
*/
24+
setCache(cache: ComponentRenderCache): void;
25+
1726
/**
1827
* Return an identifier for a dynamic state binding.
1928
*/
@@ -22,7 +31,7 @@ export interface ComponentInstance<Props extends PlainObject, State extends Plai
2231

2332
export interface ComponentDefinition<Context extends RuntimeContext = RuntimeContext> {
2433
componentId: string;
25-
render: RuntimeCallback<[UIRenderEvent], Promise<ContentKitRenderOutput>, Context>;
34+
render: RuntimeCallback<[UIRenderEvent], Promise<Response>, Context>;
2635
}
2736

2837
/**
@@ -74,10 +83,15 @@ export function createComponent<
7483
? component.initialState(props, event.context)
7584
: ((component.initialState || {}) as State));
7685

86+
let cache: ComponentRenderCache | undefined = undefined;
87+
7788
let instance: ComponentInstance<Props, State> = {
7889
state,
7990
props,
8091
context: event.context,
92+
setCache: (newCache) => {
93+
cache = newCache;
94+
},
8195
dynamicState: (key) => ({ $state: key }),
8296
};
8397

@@ -87,11 +101,18 @@ export function createComponent<
87101

88102
const element = await component.render(instance, context);
89103

90-
return {
104+
const output: ContentKitRenderOutput = {
91105
state: instance.state,
92106
props: instance.props,
93107
element,
94108
};
109+
110+
return new Response(JSON.stringify(output), {
111+
headers: {
112+
'Content-Type': 'application/json',
113+
...(cache ? { 'Cache-Control': `max-age=${cache.maxAge}` } : {}),
114+
},
115+
});
95116
},
96117
};
97118
}

Diff for: packages/runtime/src/integrations.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,7 @@ export function createIntegration<Context extends RuntimeContext = RuntimeContex
8080
}
8181

8282
// @ts-ignore
83-
const result = await component.render(event, context);
84-
return new Response(JSON.stringify(result), {
85-
headers: {
86-
'Content-Type': 'application/json',
87-
},
88-
});
83+
return await component.render(event, context);
8984
}
9085

9186
const cb = events[event.type];

0 commit comments

Comments
 (0)