Skip to content

Commit 8038696

Browse files
committed
Preload fast embed startup media
1 parent f6aa0c4 commit 8038696

3 files changed

Lines changed: 46 additions & 1 deletion

File tree

apps/site/app/embed-fast/[assetId]/route.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ function html(value: unknown) {
107107
.replaceAll('"', """);
108108
}
109109

110+
function linkHeaderValue(value: string) {
111+
return value.replaceAll("\\", "\\\\").replaceAll('"', '\\"');
112+
}
113+
110114
function jsString(value: string) {
111115
return JSON.stringify(value).replaceAll("<", "\\u003c");
112116
}
@@ -223,6 +227,34 @@ function playbackOrigin(bootstrap: WatchPlaybackBootstrapReady | null) {
223227
}
224228
}
225229

230+
function preloadablePlaybackSelection(
231+
selection: ReturnType<typeof playbackSelection>,
232+
) {
233+
if (!selection?.url || selection.contentType !== "video/mp4") return null;
234+
return selection;
235+
}
236+
237+
function appendStartupLinkHeaders(
238+
headers: Headers,
239+
edge: ReturnType<typeof playbackOrigin>,
240+
selection: ReturnType<typeof playbackSelection>,
241+
) {
242+
if (edge) {
243+
headers.append(
244+
"link",
245+
`<${linkHeaderValue(edge.origin)}>; rel=preconnect; crossorigin`,
246+
);
247+
}
248+
249+
const preload = preloadablePlaybackSelection(selection);
250+
if (!preload) return;
251+
const preloadContentType = preload.contentType ?? "video/mp4";
252+
headers.append(
253+
"link",
254+
`<${linkHeaderValue(preload.url)}>; rel=preload; as=video; type="${linkHeaderValue(preloadContentType)}"; crossorigin="use-credentials"; fetchpriority=high`,
255+
);
256+
}
257+
226258
function playerMessage(
227259
bootstrap: WatchPlaybackBootstrapResponse | null,
228260
selection: ReturnType<typeof playbackSelection>,
@@ -246,6 +278,8 @@ export function renderFastEmbedHtml(options: FastEmbedRenderOptions) {
246278
const contentType = selection?.contentType
247279
? ` type="${html(selection.contentType)}"`
248280
: "";
281+
const preload = preloadablePlaybackSelection(selection);
282+
const preloadContentType = preload?.contentType ?? "video/mp4";
249283
const bootstrapMs =
250284
typeof options.bootstrapMs === "number"
251285
? ` data-rend-bootstrap-ms="${Math.max(0, Math.round(options.bootstrapMs))}"`
@@ -261,6 +295,7 @@ export function renderFastEmbedHtml(options: FastEmbedRenderOptions) {
261295
<title>Rend player</title>
262296
${edge ? `<link rel="dns-prefetch" href="${html(edge.dnsPrefetch)}">` : ""}
263297
${edge ? `<link rel="preconnect" href="${html(edge.origin)}" crossorigin>` : ""}
298+
${preload ? `<link rel="preload" as="video" href="${html(preload.url)}" type="${html(preloadContentType)}" crossorigin="use-credentials" fetchpriority="high">` : ""}
264299
<style>
265300
html,body{margin:0;width:100%;height:100%;background:#050505;color:#f7f7f7}
266301
body{overflow:hidden}
@@ -328,6 +363,9 @@ export async function GET(
328363
request,
329364
normalizedAssetId,
330365
);
366+
const ready = bootstrap?.status === "ready" ? bootstrap : null;
367+
const selection = playbackSelection(bootstrap, startup);
368+
const edge = playbackOrigin(ready);
331369
const headers = new Headers({
332370
"cache-control": "no-store",
333371
"content-type": "text/html; charset=utf-8",
@@ -340,6 +378,7 @@ export async function GET(
340378
headers.append("set-cookie", setCookie);
341379
}
342380
}
381+
appendStartupLinkHeaders(headers, edge, selection);
343382

344383
return new Response(
345384
renderFastEmbedHtml({

apps/site/app/embed-fast/route.test.mts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ test("fast embed defaults to progressive fMP4 when startup hints support it", ()
7272
});
7373

7474
assert.match(html, /src="https:\/\/api\.rend\.so\/v\/00000000-0000-0000-0000-000000000001\/hls\/360p\/progressive\.mp4"/);
75+
assert.match(html, /rel="preload" as="video" href="https:\/\/api\.rend\.so\/v\/00000000-0000-0000-0000-000000000001\/hls\/360p\/progressive\.mp4" type="video\/mp4" crossorigin="use-credentials" fetchpriority="high"/);
7576
assert.match(html, /data-rend-player-selected="progressive_mp4"/);
7677
assert.match(html, /data-rend-player-artifact="hls\/360p\/progressive\.mp4"/);
7778
assert.doesNotMatch(html, /playback_token|set-cookie|authorization/i);
@@ -113,6 +114,9 @@ test("fast embed route forwards bootstrap cookies to the document response", asy
113114
assert.equal(response.status, 200);
114115
assert.equal(response.headers.get("cache-control"), "no-store");
115116
assert.equal(response.headers.get("x-rend-fast-embed"), "1");
117+
assert.match(response.headers.get("link") ?? "", /rel=preconnect/);
118+
assert.match(response.headers.get("link") ?? "", /hls\/360p\/progressive\.mp4/);
119+
assert.match(response.headers.get("link") ?? "", /rel=preload; as=video/);
116120
assert.match(response.headers.get("set-cookie") ?? "", /__rend_playback=/);
117121
assert.equal(
118122
fetches[0]?.url,

scripts/run-daytona-provider-benchmark.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,9 @@ function daytonaApiKeyForTarget(env, target) {
303303
function rendAssetIdFromUrl(rawUrl) {
304304
try {
305305
const pathParts = new URL(rawUrl).pathname.split("/").filter(Boolean);
306-
if (pathParts[0] !== "embed" || !pathParts[1]) return "";
306+
if (!["embed", "embed-fast"].includes(pathParts[0]) || !pathParts[1]) {
307+
return "";
308+
}
307309
return decodeURIComponent(pathParts[1]);
308310
} catch {
309311
return "";

0 commit comments

Comments
 (0)