Add workaround for Chromium range request caching bug

This commit is contained in:
Vanilagy
2025-08-11 18:14:02 +02:00
parent 6d447660c2
commit 1492fd5c6f
5 changed files with 40 additions and 10 deletions
+22 -2
View File
@@ -164,11 +164,13 @@ export type UrlSourceOptions = {
*/
export class UrlSource extends Source {
/** @internal */
private _url: string | URL;
private _url: URL;
/** @internal */
private _options: UrlSourceOptions;
/** @internal */
private _fullData: ArrayBuffer | null = null;
/** @internal */
private _nextUrlVersion: number | null = null;
constructor(
url: string | URL,
@@ -189,7 +191,7 @@ export class UrlSource extends Source {
super();
this._url = url;
this._url = url instanceof URL ? url : new URL(url);
this._options = options;
}
@@ -203,6 +205,11 @@ export class UrlSource extends Source {
headers['Range'] = `bytes=${range.start}-${range.end - 1}`;
}
if (this._nextUrlVersion !== null) {
this._url.searchParams.set('mediabunny_version', this._nextUrlVersion.toString());
this._nextUrlVersion++;
}
const response = await retriedFetch(
this._url,
mergeObjectsDeeply(this._options.requestInit ?? {}, {
@@ -218,6 +225,19 @@ export class UrlSource extends Source {
const buffer = await response.arrayBuffer();
if (
response.status === 206
&& range
&& buffer.byteLength !== range.end - range.start
&& this._nextUrlVersion === null
) {
// We did a range request but it resolved with the wrong range; in Chromium, this can be due to a caching
// bug (https://issues.chromium.org/issues/436025873). Let's circumvent the cache for the rest of the
// session by appending a version to the URL.
this._nextUrlVersion = 1;
return this._makeRequest(range);
}
if (response.status === 200) {
// The server didn't return 206 Partial Content, so it's not a range response
this._fullData = buffer;