Skip to content
Merged
5 changes: 5 additions & 0 deletions .changeset/popular-plants-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/kit": patch
---

[fix] handle binary data when prerendering
6 changes: 3 additions & 3 deletions packages/kit/src/core/prerender/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ export async function prerender({ config, entries, files, log }) {
}
});

const text = await response.text();
const body = Buffer.from(await response.arrayBuffer());

save('pages', response, text, decoded, encoded, referrer, 'linked');
save('pages', response, body, decoded, encoded, referrer, 'linked');

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

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

const resolved = resolve(encoded, href);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<img src="/fetch-image/image.jpg" alt="" />
<img src="/fetch-image/image.png" alt="" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as fs from 'fs';

export async function GET({ params }) {
const slug = params.slug.split('/');
const extension = slug[0].split('.').pop();

const file = fs.readFileSync(`./static/image.${extension}`);

return {
status: 200,
headers: {
'Content-Type': 'image/' + extension
},
body: file
};
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion packages/kit/test/prerendering/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as assert from 'uvu/assert';
const build = fileURLToPath(new URL('../build', import.meta.url));

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

test('prerenders /', () => {
const content = read('index.html');
Expand Down Expand Up @@ -141,4 +141,9 @@ test('targets the data-sveltekit-hydrate parent node', () => {
);
});

test('prerenders binary data', async () => {
assert.equal(Buffer.compare(read('fetch-image/image.jpg', null), read('image.jpg', null)), 0);
assert.equal(Buffer.compare(read('fetch-image/image.png', null), read('image.png', null)), 0);
});

test.run();