Skip to content

Commit 6a8cd0e

Browse files
committed
prerender emitted assets
1 parent 370e9f9 commit 6a8cd0e

File tree

9 files changed

+83
-1
lines changed

9 files changed

+83
-1
lines changed

.changeset/chilled-humans-roll.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: detect assets emitted by vite plugins in prerenderer `fetch`.

packages/kit/src/core/generate_manifest/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,16 @@ export function generate_manifest({ build_data, prerendered, relative_path, rout
8686
mime_types[ext] ??= mime.lookup(ext) || '';
8787
}
8888

89+
const asset_prefix = path.resolve('_app', 'immutable', 'assets');
90+
const emitted_asset_dir = path.resolve(build_data.out_dir, 'server', asset_prefix);
91+
if (fs.existsSync(emitted_asset_dir)) {
92+
for (const file of fs.readdirSync(emitted_asset_dir)) {
93+
assets.push(path.join(asset_prefix, file).replaceAll(path.sep, '/'));
94+
const ext = path.extname(file);
95+
mime_types[ext] ??= mime.lookup(ext) || '';
96+
}
97+
}
98+
8999
// prettier-ignore
90100
// String representation of
91101
/** @template {import('@sveltejs/kit').SSRManifest} T */

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ async function prerender({ hash, out, manifest_path, metadata, verbose, env }) {
236236
const filepath = saved.get(file);
237237
if (filepath) return readFileSync(filepath);
238238

239+
// Static assets emitted during build
240+
if (file.startsWith(config.appDir))
241+
return readFileSync(join(config.outDir, 'output', 'server', file));
242+
239243
// stuff in `static`
240244
return readFileSync(join(config.files.assets, file));
241245
},
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
4+
export default function () {
5+
let config;
6+
const ext = '.csv';
7+
return {
8+
name: 'vite-plugin-sveltekit-custom-asset',
9+
resolveId(id) {
10+
if (id.endsWith(ext)) {
11+
return id;
12+
}
13+
},
14+
configResolved(resolvedConfig) {
15+
config = resolvedConfig;
16+
},
17+
load(id) {
18+
if (!id.endsWith(ext)) return;
19+
if (config.command === 'serve') {
20+
const res = path.relative(config.root, id).replaceAll(path.sep, '/');
21+
return `export default "${config.base}${res}";`;
22+
}
23+
24+
const outName = config.build.rollupOptions.output.assetFileNames
25+
.replace('[name]', path.basename(id, ext))
26+
.replace('[hash]', '000000')
27+
.replace('[extname]', ext);
28+
29+
if (config.build?.ssr) {
30+
const ref = this.emitFile({
31+
type: 'asset',
32+
fileName: outName
33+
});
34+
fs.readFile(id, (err, data) => {
35+
if (err) this.error(err);
36+
this.setAssetSource(ref, data);
37+
});
38+
}
39+
40+
return `export default "${config.base}${outName}";`;
41+
}
42+
};
43+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @ts-ignore
2+
import url from './message.csv';
3+
4+
export async function load({ fetch }) {
5+
const response = await fetch(url);
6+
const asset = await response.text();
7+
return { asset };
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
const { data } = $props();
3+
</script>
4+
5+
<p>{data.asset}</p>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
A custom asset emitted by a vite plugin during build.

packages/kit/test/prerendering/paths-base/test/tests.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,8 @@ test('prerenders /path-base/assets', () => {
2929
const content = read('assets.html');
3030
assert.match(content, /<img[^>]+src="\/path-base\//u);
3131
});
32+
33+
test('prerenders /path-base/assets/emitted', () => {
34+
const content = read('assets/emitted.html');
35+
assert.ok(content.includes('<p>A custom asset emitted by a vite plugin during build.</p>'));
36+
});

packages/kit/test/prerendering/paths-base/vite.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as path from 'node:path';
22
import { sveltekit } from '@sveltejs/kit/vite';
3+
import csvasset from './custom_asset_plugin.js';
34

45
/** @type {import('vite').UserConfig} */
56
const config = {
@@ -13,7 +14,7 @@ const config = {
1314

1415
logLevel: 'silent',
1516

16-
plugins: [sveltekit()],
17+
plugins: [csvasset(), sveltekit()],
1718

1819
server: {
1920
fs: {

0 commit comments

Comments
 (0)