fix(core): Safari keeps a small file's preload and leaves a playing clip's source alone (#7498)

* fix(core): Safari keeps a small file's authored preload and never holds a clip already playing

* fix(core): Safari still holds a long file's later clips that were parsed before the runtime

The sweep sets preload none on them again and gives a clip its authored preload back once its file turns out not to be long.

* fix(core): Safari leaves the src of a long file's clip that is already playing or seeking

Its file's size can arrive after the clip started; writing an in-point src then restarted its load and sent it back to the in-point.
This commit is contained in:
Miguel Ángel
2026-11-10 15:18:22 +00:00
committed by GitHub
parent b0835262ae
commit cda84222aa
2 changed files with 48 additions and 6 deletions
@@ -341,6 +341,41 @@ describe("a clip of a long file opening at its in-point on Safari", () => {
expect(v.preload).not.toBe("none");
});
it("leaves the preload of a small file's clip parsed before the runtime as authored", async () => {
sizeOf(5_600_000);
stopWatching();
document.body.insertAdjacentHTML("beforeend", later());
const v = document.body.lastElementChild as HTMLVideoElement;
const authored = v.preload;
deferMediaUntilDue();
await vi.waitFor(() => expect(fetch).toHaveBeenCalled());
await new Promise((r) => setTimeout(r, 10));
expect(v.preload).toBe(authored);
});
it("holds a later clip of a long file parsed before the runtime", async () => {
stopWatching();
document.body.insertAdjacentHTML("beforeend", later());
const v = document.body.lastElementChild as HTMLVideoElement;
deferMediaUntilDue();
await vi.waitFor(() => expect(v.getAttribute("src")).toBeNull());
});
it.each([
["playing", "paused", false],
["seeking", "seeking", true],
])(
"leaves the src of a later clip already %s when its file's size arrives",
async (_n, prop, value) => {
document.body.insertAdjacentHTML("beforeend", later());
const v = document.body.lastElementChild as HTMLVideoElement;
const authored = v.getAttribute("src");
Object.defineProperty(v, prop, { value, configurable: true });
await new Promise((r) => setTimeout(r, 10));
expect(v.getAttribute("src")).toBe(authored);
},
);
it("holds a later clip again when it is released after playing", async () => {
const v = await added(later());
await vi.waitFor(() => expect(v.getAttribute("src")).toBeNull());
+13 -6
View File
@@ -187,11 +187,17 @@ const holdable = (el: HTMLMediaElement, src: string) =>
!el.loop &&
!el.querySelector("source");
function openLongSourceAtInPoint(el: HTMLMediaElement, later: boolean, opened: () => void): void {
function openLongSourceAtInPoint(
el: HTMLMediaElement,
later: boolean,
opened: () => void,
notLong?: () => void,
): void {
const src = el.getAttribute("src");
if (!isWebKit() || !inPlayerFrame(document) || !src) return;
if (!isWebKit() || !inPlayerFrame(document) || !src) return notLong?.();
void bytesOfSource(src).then((bytes) => {
if (bytes === null || bytes < LONG_SOURCE_BYTES || el.getAttribute("src") !== src) return;
if (bytes === null || bytes < LONG_SOURCE_BYTES) return notLong?.();
if (el.getAttribute("src") !== src || !el.paused || el.seeking) return;
if (later && holdable(el, src)) return holdUntilDue(el);
openAtInPoint(el);
opened();
@@ -207,14 +213,15 @@ export function deferMediaUntilDue(): void {
win.__hfMediaDeferral?.disconnect();
win.__hfMediaDeferral = undefined;
if (!defersMedia()) return;
const defer = (el: Element, startInFilm: (el: Element) => number) => {
const defer = (el: Element, startInFilm: (el: Element) => number, parsedEarly = false) => {
const start = parseStrictFiniteTimingNumber(el.getAttribute("data-start"));
if (start == null || !lengthIsAuthored(el)) return;
const media = el as HTMLMediaElement;
const authoredPreload = media.preload;
const later = startInFilm(el) > 0;
if (later) media.preload = "none";
openLongSourceAtInPoint(media, later, () => (media.preload = authoredPreload));
const restore = () => (media.preload = authoredPreload);
openLongSourceAtInPoint(media, later, restore, later && parsedEarly ? restore : undefined);
};
win.__hfMediaDeferral = new MutationObserver((records) => {
const startInFilm = createRuntimeStartTimeResolver({}).resolveMediaStartForElement;
@@ -229,6 +236,6 @@ export function deferMediaUntilDue(): void {
win.__hfMediaDeferral.observe(document.documentElement, { childList: true, subtree: true });
if (isWebKit()) {
const startInFilm = createRuntimeStartTimeResolver({}).resolveMediaStartForElement;
for (const media of document.querySelectorAll("video, audio")) defer(media, startInFilm);
for (const media of document.querySelectorAll("video, audio")) defer(media, startInFilm, true);
}
}