Handle inlined images correctly

This commit is contained in:
Yuri Papouski 2022-09-06 13:01:21 +00:00
parent fe86d602e9
commit af62018424
2 changed files with 13 additions and 1 deletions

View File

@ -270,4 +270,14 @@ describe('cache-storage', () => {
fail('Expected result to timeout'); fail('Expected result to timeout');
} catch (e) {} } catch (e) {}
}); });
it('addImage should add an inlined image', async () => {
const {cache} = createMockContext('http://example.com', {imageTimeout: 10});
const inlinedImg = `data:image/gif;base64,R0lGODlhEAAOALMAAOazToeHh0tLS/7LZv/0jvb29t/f3//Ub/
/ge8WSLf/rhf/3kdbW1mxsbP//mf///yH5BAAAAAAALAAAAAAQAA4AAARe8L1Ekyky67QZ1hLnjM5UUde0ECwLJoExKcpp
V0aCcGCmTIHEIUEqjgaORCMxIC6e0CcguWw6aFjsVMkkIr7g77ZKPJjPZqIyd7sJAgVGoEGv2xsBxqNgYPj/gAwXEQA7`;
cache.addImage(inlinedImg);
await cache.match(inlinedImg);
});
}); });

View File

@ -98,7 +98,9 @@ export class Cache {
img.crossOrigin = 'anonymous'; img.crossOrigin = 'anonymous';
} }
img.src = src; img.src = src;
if (img.complete === true) { if (/^data:/.test(src)) {
resolve(img);
} else if (img.complete === true) {
// Inline XML images may fail to parse, throwing an Error later on // Inline XML images may fail to parse, throwing an Error later on
setTimeout(() => resolve(img), 500); setTimeout(() => resolve(img), 500);
} }