Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions integration-tests/js-compute/fixtures/app/src/early-hints.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,76 @@ routes.set('/early-hints/manual-response', (event) => {
event.respondWith(new Response('ok'));
});

routes.set('/early-hints/manual-response-late', (event) => {
event.respondWith(new Response('ok'));
assertThrows(() => {
event.respondWith(
new Response(null, {
status: 103,
headers: { link: '</style.css>; rel=preload; as=style' },
}),
);
});
});

routes.set('/early-hints/manual-response-async', async (event) => {
await (async () => {
event.respondWith(
new Response(null, {
status: 103,
headers: { link: '</style.css>; rel=preload; as=style' },
}),
);
event.respondWith(new Response('ok'));
})();
});

routes.set('/early-hints/manual-response-late-async', async (event) => {
await (async () => {
event.respondWith(new Response('ok'));
assertThrows(() => {
event.respondWith(
new Response(null, {
status: 103,
headers: { link: '</style.css>; rel=preload; as=style' },
}),
);
});
})();
});

routes.set('/early-hints/send-early-hints', (event) => {
event.sendEarlyHints({ link: '</style.css>; rel=preload; as=style' });
event.respondWith(new Response('ok'));
});

routes.set('/early-hints/send-early-hints-late', (event) => {
event.respondWith(new Response('ok'));
assertThrows(() => {
event.sendEarlyHints({ link: '</style.css>; rel=preload; as=style' });
});
});

routes.set('/early-hints/send-early-hints-multiple-headers', (event) => {
event.sendEarlyHints([
['link', '</style.css>; rel=preload; as=style'],
['link', '</style2.css>; rel=preload; as=style'],
]);
event.respondWith(new Response('ok'));
});

routes.set('/early-hints/send-early-hints-async', async (event) => {
await (async () => {
event.sendEarlyHints({ link: '</style.css>; rel=preload; as=style' });
event.respondWith(new Response('ok'));
})();
});

routes.set('/early-hints/send-early-hints-late-async', async (event) => {
await (async () => {
event.respondWith(new Response('ok'));
assertThrows(() => {
event.sendEarlyHints({ link: '</style.css>; rel=preload; as=style' });
});
})();
});
54 changes: 54 additions & 0 deletions integration-tests/js-compute/fixtures/app/tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -3142,6 +3142,33 @@
}
}
},
"GET /early-hints/manual-response-late": {
"environments": ["compute"],
"downstream_response": {
"body": "ok",
"status": 200
}
},
"GET /early-hints/manual-response-async": {
"environments": ["compute"],
"downstream_response": {
"body": "ok",
"status": 200
},
"downstream_info": {
"status": 103,
"headers": {
"link": "</style.css>; rel=preload; as=style"
}
}
},
"GET /early-hints/manual-response-late-async": {
"environments": ["compute"],
"downstream_response": {
"body": "ok",
"status": 200
}
},
"GET /early-hints/send-early-hints": {
"environments": ["compute"],
"downstream_response": {
Expand All @@ -3155,6 +3182,13 @@
}
}
},
"GET /early-hints/send-early-hints-late": {
"environments": ["compute"],
"downstream_response": {
"body": "ok",
"status": 200
}
},
"GET /early-hints/send-early-hints-multiple-headers": {
"environments": ["compute"],
"downstream_response": {
Expand All @@ -3169,6 +3203,26 @@
]
}
},
"GET /early-hints/send-early-hints-async": {
"environments": ["compute"],
"downstream_response": {
"body": "ok",
"status": 200
},
"downstream_info": {
"status": 103,
"headers": {
"link": "</style.css>; rel=preload; as=style"
}
}
},
"GET /early-hints/send-early-hints-late-async": {
"environments": ["compute"],
"downstream_response": {
"body": "ok",
"status": 200
}
},
"GET /shielding/invalid-shield": {
"environments": ["compute"]
},
Expand Down
5 changes: 0 additions & 5 deletions runtime/fastly/builtins/fetch-event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -908,11 +908,6 @@ bool FetchEvent::sendEarlyHints(JSContext *cx, unsigned argc, JS::Value *vp) {
METHOD_HEADER(1)
MOZ_RELEASE_ASSERT(state(self) == State::unhandled || state(self) == State::waitToRespond);

if (!is_dispatching(self)) {
JS_ReportErrorUTF8(cx, "FetchEvent#sendEarlyHints must be called synchronously from "
"within a FetchEvent handler");
return false;
}
if (state(self) != State::unhandled && state(self) != State::waitToRespond) {
JS_ReportErrorUTF8(
cx, "FetchEvent#sendEarlyHints can't be called after the main response has been sent");
Expand Down
Loading