Skip to content

Commit a272fd6

Browse files
gbkwiattConduitrybenmccanngbkwiattRich-Harris
authored
[fix] handle binary data when prerendering (#5497)
* fix: corrupted images when using adapter-static this is to fix issue mentioned [here](#5088) body was always converted using `response.text()` which was always decoded with UTF-8 which then is breaking buffers * fix: lint fix lint fix * chore: lint fix lint fix * always use Buffer * Create popular-plants-begin.md * added test for binary files after prerender * linting * added node fetch and fix linting * changed get to GET as per latest kit * using local images for test instead of urls and fetch * Update packages/kit/test/prerendering/basics/test/test.js * Update packages/kit/test/prerendering/basics/test/test.js Co-authored-by: Conduitry <[email protected]> Co-authored-by: Ben McCann <[email protected]> Co-authored-by: gbkwiatt <[email protected]> Co-authored-by: Rich Harris <[email protected]>
1 parent c9f3e1e commit a272fd6

File tree

7 files changed

+32
-4
lines changed

7 files changed

+32
-4
lines changed

.changeset/popular-plants-begin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sveltejs/kit": patch
3+
---
4+
5+
[fix] handle binary data when prerendering

packages/kit/src/core/prerender/prerender.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ export async function prerender({ config, entries, files, log }) {
136136
}
137137
});
138138

139-
const text = await response.text();
139+
const body = Buffer.from(await response.arrayBuffer());
140140

141-
save('pages', response, text, decoded, encoded, referrer, 'linked');
141+
save('pages', response, body, decoded, encoded, referrer, 'linked');
142142

143143
for (const [dependency_path, result] of dependencies) {
144144
// this seems circuitous, but using new URL allows us to not care
@@ -159,7 +159,7 @@ export async function prerender({ config, entries, files, log }) {
159159
}
160160

161161
if (config.prerender.crawl && response.headers.get('content-type') === 'text/html') {
162-
for (const href of crawl(text)) {
162+
for (const href of crawl(body.toString())) {
163163
if (href.startsWith('data:') || href.startsWith('#')) continue;
164164

165165
const resolved = resolve(encoded, href);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<img src="/fetch-image/image.jpg" alt="" />
2+
<img src="/fetch-image/image.png" alt="" />
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as fs from 'fs';
2+
3+
export async function GET({ params }) {
4+
const slug = params.slug.split('/');
5+
const extension = slug[0].split('.').pop();
6+
7+
const file = fs.readFileSync(`./static/image.${extension}`);
8+
9+
return {
10+
status: 200,
11+
headers: {
12+
'Content-Type': 'image/' + extension
13+
},
14+
body: file
15+
};
16+
}
345 KB
Loading
5.32 KB
Loading

packages/kit/test/prerendering/basics/test/test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as assert from 'uvu/assert';
66
const build = fileURLToPath(new URL('../build', import.meta.url));
77

88
/** @param {string} file */
9-
const read = (file) => fs.readFileSync(`${build}/${file}`, 'utf-8');
9+
const read = (file, encoding = 'utf-8') => fs.readFileSync(`${build}/${file}`, encoding);
1010

1111
test('prerenders /', () => {
1212
const content = read('index.html');
@@ -144,4 +144,9 @@ test('targets the data-sveltekit-hydrate parent node', () => {
144144
);
145145
});
146146

147+
test('prerenders binary data', async () => {
148+
assert.equal(Buffer.compare(read('fetch-image/image.jpg', null), read('image.jpg', null)), 0);
149+
assert.equal(Buffer.compare(read('fetch-image/image.png', null), read('image.png', null)), 0);
150+
});
151+
147152
test.run();

0 commit comments

Comments
 (0)