Skip to content

Commit 3bbbe99

Browse files
committed
chore: handle fallback case, test for assets
1 parent e9d138a commit 3bbbe99

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

lib/http-outgoing.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,10 @@ export default class PodletClientHttpOutgoing extends PassThrough {
308308
set hintsReceived(value) {
309309
this.#hintsReceived = value;
310310
if (this.#hintsReceived) {
311-
this.#incoming.hints.addReceivedHint(this.#name);
311+
this.#incoming?.hints?.addReceivedHint(this.#name, {
312+
js: this.js,
313+
css: this.css,
314+
});
312315
}
313316
}
314317

@@ -349,6 +352,8 @@ export default class PodletClientHttpOutgoing extends PassThrough {
349352
this.css = this.#manifest._css;
350353
this.push(null);
351354
this.#isFallback = true;
355+
// assume the hints from the podlet have failed and fallback assets will be used
356+
this.hintsReceived = true;
352357
}
353358

354359
writeEarlyHints(cb = () => {}) {

tests/resource.test.js

+36-2
Original file line numberDiff line numberDiff line change
@@ -852,10 +852,42 @@ tap.test(
852852
},
853853
);
854854

855+
tap.test(
856+
'Resource().fetch - hints complete event emitted once all early hints received - resource is failing',
857+
async (t) => {
858+
t.plan(3);
859+
const server = new PodletServer({
860+
version: '1.0.0',
861+
assets: {
862+
js: '/foo/bar.js',
863+
css: '/foo/bar.css',
864+
},
865+
content: '/does/not/exist',
866+
});
867+
const service = await server.listen();
868+
869+
const client = new Client({ name: 'podiumClient' });
870+
const component = client.register(service.options);
871+
872+
const incoming = new HttpIncoming({ headers: {} });
873+
874+
incoming.hints.on('complete', (assets) => {
875+
t.ok(true);
876+
t.equal(assets.js.length, 1);
877+
t.equal(assets.css.length, 1);
878+
t.end();
879+
});
880+
881+
await component.fetch(incoming);
882+
883+
await server.close();
884+
},
885+
);
886+
855887
tap.test(
856888
'Resource().fetch - hints complete event emitted once all early hints received - multiple resource components',
857889
async (t) => {
858-
t.plan(1);
890+
t.plan(3);
859891
const server1 = new PodletServer({
860892
name: 'one',
861893
version: '1.0.0',
@@ -891,7 +923,9 @@ tap.test(
891923

892924
const incoming = new HttpIncoming({ headers: {} });
893925

894-
incoming.hints.on('complete', () => {
926+
incoming.hints.on('complete', (assets) => {
927+
t.equal(assets.js.length, 3);
928+
t.equal(assets.css.length, 3);
895929
t.ok(true);
896930
t.end();
897931
});

0 commit comments

Comments
 (0)