Skip to content

Commit a4a5a81

Browse files
fix: resolve issues with fetching local assets when serving files from the file protocol (#514)
#### What does this PR do? This PR fixes the issue where the assets in locally packaged apps are unresolved and fail to load. Now the local assets are loaded successfully and served to the app. #### How has this been tested? This has been tested locally on various WebOS devices
2 parents 4952c6e + d5ef136 commit a4a5a81

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed

src/core/lib/ImageWorker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ function createImageWorker() {
7373
xhr.responseType = 'blob';
7474

7575
xhr.onload = function () {
76-
if (xhr.status !== 200) {
76+
// On most devices like WebOS and Tizen, the file protocol returns 0 while http(s) protocol returns 200
77+
if (xhr.status !== 200 && xhr.status !== 0) {
7778
return reject(new Error('Failed to load image: ' + xhr.statusText));
7879
}
7980

src/core/text-rendering/font-face-types/SdfTrFontFace/SdfTrFontFace.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
type NormalizedFontMetrics,
2828
type TrFontFaceOptions,
2929
} from '../TrFontFace.js';
30+
import { fetchJson } from '../utils.js';
3031
import type { FontShaper } from './internal/FontShaper.js';
3132
import { SdfFontShaper, type SdfFontData } from './internal/SdfFontShaper.js';
3233

@@ -62,9 +63,9 @@ export class SdfTrFontFace<
6263
* in SDF/vertex units.
6364
*/
6465
public readonly maxCharHeight: number = 0;
65-
public readonly data: SdfFontData | undefined;
6666
public readonly shaper: FontShaper | undefined;
6767
public readonly glyphMap: Map<number, SdfFontData['chars'][0]> = new Map();
68+
public data: SdfFontData | undefined;
6869

6970
constructor(type: FontTypeT, options: SdfTrFontFaceOptions) {
7071
super(options);
@@ -103,10 +104,9 @@ export class SdfTrFontFace<
103104
});
104105

105106
// Set this.data to the fetched data from dataUrl
106-
fetch(atlasDataUrl)
107-
.then(async (response) => {
108-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
109-
(this.data as SdfFontData) = await response.json();
107+
fetchJson(atlasDataUrl)
108+
.then((response) => {
109+
this.data = JSON.parse(response as unknown as string) as SdfFontData;
110110
assertTruthy(this.data);
111111
// Add all the glyphs to the glyph map
112112

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* If not stated otherwise in this file or this component's LICENSE file the
3+
* following copyright and licenses apply:
4+
*
5+
* Copyright 2020 Metrological
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the License);
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
export function fetchJson(
20+
url: string,
21+
responseType: XMLHttpRequestResponseType = '',
22+
): Promise<unknown> {
23+
return new Promise((resolve, reject) => {
24+
const xhr = new XMLHttpRequest();
25+
xhr.responseType = responseType;
26+
xhr.onreadystatechange = function () {
27+
if (xhr.readyState == XMLHttpRequest.DONE) {
28+
// On most devices like WebOS and Tizen, the file protocol returns 0 while http(s) protocol returns 200
29+
if (xhr.status === 0 || xhr.status === 200) {
30+
resolve(xhr.response);
31+
} else {
32+
reject(xhr.statusText);
33+
}
34+
}
35+
};
36+
xhr.open('GET', url, true);
37+
xhr.send(null);
38+
});
39+
}

src/core/textures/ImageTexture.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
} from '../lib/textureCompression.js';
2626
import { convertUrlToAbsolute, isBase64Image } from '../lib/utils.js';
2727
import { isSvgImage, loadSvg } from '../lib/textureSvg.js';
28+
import { fetchJson } from '../text-rendering/font-face-types/utils.js';
2829

2930
/**
3031
* Properties of the {@link ImageTexture}
@@ -220,7 +221,9 @@ export class ImageTexture extends Texture {
220221
);
221222
}
222223

223-
const blob = await fetch(src).then((response) => response.blob());
224+
const blob = await fetchJson(src, 'blob').then(
225+
(response) => response as Blob,
226+
);
224227
return this.createImageBitmap(blob, premultiplyAlpha, sx, sy, sw, sh);
225228
}
226229

0 commit comments

Comments
 (0)