Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: detect build-emitted assets in prerenderer fetch #12201

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chilled-humans-roll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/kit": patch
---

fix: detect assets emitted by vite plugins in prerenderer `fetch`.
9 changes: 9 additions & 0 deletions packages/kit/src/core/generate_manifest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ export function generate_manifest({ build_data, relative_path, routes }) {
mime_types[ext] ??= mime.lookup(ext) || '';
}

const emitted_asset_dir = path.resolve(build_data.out_dir, 'server', build_data.asset_prefix);
if (fs.existsSync(emitted_asset_dir)) {
for (const file of fs.readdirSync(emitted_asset_dir)) {
assets.push(path.join(build_data.asset_prefix, file).replaceAll(path.sep, '/'));
const ext = path.extname(file);
mime_types[ext] ??= mime.lookup(ext) || '';
}
}

// prettier-ignore
// String representation of
/** @template {import('@sveltejs/kit').SSRManifest} T */
Expand Down
5 changes: 5 additions & 0 deletions packages/kit/src/core/postbuild/prerender.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ async function prerender({ out, manifest_path, metadata, verbose, env }) {
const filepath = saved.get(file);
if (filepath) return readFileSync(filepath);

// Static assets emitted during build
if (file.startsWith(config.appDir)) {
return readFileSync(join(config.outDir, 'output', 'server', file));
}

// stuff in `static`
return readFileSync(join(config.files.assets, file));
},
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/exports/vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ async function kit({ svelte_config }) {
app_path: `${kit.paths.base.slice(1)}${kit.paths.base ? '/' : ''}${kit.appDir}`,
manifest_data,
out_dir: out,
asset_prefix: `${kit.appDir}/immutable/assets`,
service_worker: service_worker_entry_file ? 'service-worker.js' : null, // TODO make file configurable?
client: null,
server_manifest
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/types/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export interface BuildData {
app_path: string;
manifest_data: ManifestData;
out_dir: string;
asset_prefix: string;
service_worker: string | null;
client: {
start: string;
Expand Down
41 changes: 41 additions & 0 deletions packages/kit/test/prerendering/paths-base/custom_asset_plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import fs from 'node:fs';
import path from 'node:path';

export default function () {
let config;
const ext = '.csv';
return {
name: 'vite-plugin-sveltekit-custom-asset',
resolveId(id) {
if (id.endsWith(ext)) {
return id;
}
},
configResolved(resolvedConfig) {
config = resolvedConfig;
},
load(id) {
if (!id.endsWith(ext)) return;
if (config.command === 'serve')
return `export default "${path.relative(process.cwd(), id).replaceAll(path.sep, '/')}";`;

const outName = config.build.rollupOptions.output.assetFileNames
.replace('[name]', path.basename(id, ext))
.replace('[hash]', '000000')
.replace('[extname]', ext);

if (config.build?.ssr) {
const ref = this.emitFile({
type: 'asset',
fileName: outName
});
fs.readFile(id, (err, data) => {
if (err) this.error(err);
this.setAssetSource(ref, data);
});
}

return `export default "${config.base}${outName}";`;
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @ts-ignore
import url from './message.csv';

export async function load({ fetch }) {
const response = await fetch(url);
const asset = await response.text();
return { asset };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
export let data;
</script>

<p>{data.asset}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A custom asset emitted by a vite plugin during build.
5 changes: 5 additions & 0 deletions packages/kit/test/prerendering/paths-base/test/tests.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ test('prerenders /path-base/assets', () => {
const content = read('assets.html');
assert.match(content, /<img[^>]+src="\/path-base\//u);
});

test('prerenders /path-base/assets/emitted', () => {
const content = read('assets/emitted.html');
assert.ok(content.includes('<p>A custom asset emitted by a vite plugin during build.</p>'));
});
3 changes: 2 additions & 1 deletion packages/kit/test/prerendering/paths-base/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as path from 'node:path';
import { sveltekit } from '@sveltejs/kit/vite';
import csvasset from './custom_asset_plugin.js';

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

logLevel: 'silent',

plugins: [sveltekit()],
plugins: [csvasset(), sveltekit()],

server: {
fs: {
Expand Down
1 change: 1 addition & 0 deletions packages/kit/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1587,6 +1587,7 @@ declare module '@sveltejs/kit' {
app_path: string;
manifest_data: ManifestData;
out_dir: string;
asset_prefix: string;
service_worker: string | null;
client: {
start: string;
Expand Down
Loading