fix: support instrumentation for `vite preview` (#15105)
authorAlois Klink <alois@aloisklink.com>
Tue, 6 Jan 2026 20:21:25 +0000 (05:21 +0900)
committerGitHub <noreply@github.com>
Tue, 6 Jan 2026 20:21:25 +0000 (13:21 -0700)
The `instrumentation.server.js` file is now imported when running
`vite preview`. Unfortunately, since it's not the first thing that's
imported (all of Vite's depedencies are imported first), some
instrumentation that rely on monkey-patching imports might not work
properly, but at least we're importing it before the server code.
However, this matches the existing behaviour of `vite dev`.

See: https://github.com/sveltejs/kit/blob/51214794b40b2fc533b06cb8344e84e661f90be0/packages/kit/src/exports/vite/dev/index.js#L507-L513

.changeset/loud-glasses-enter.md [new file with mode: 0644]
packages/kit/src/exports/vite/preview/index.js

diff --git a/.changeset/loud-glasses-enter.md b/.changeset/loud-glasses-enter.md
new file mode 100644 (file)
index 0000000..645f284
--- /dev/null
@@ -0,0 +1,5 @@
+---
+'@sveltejs/kit': patch
+---
+
+fix: support instrumentation for `vite preview`
index 84b987ad13c2d2a4d2b7ba12a517cfa7e4904209..02a89e9e6dd49c4fcfe85dbc83087b13a3f862e2 100644 (file)
@@ -35,6 +35,11 @@ export async function preview(vite, vite_config, svelte_config) {
                throw new Error(`Server files not found at ${dir}, did you run \`build\` first?`);
        }
 
+       const instrumentation = join(dir, 'instrumentation.server.js');
+       if (fs.existsSync(instrumentation)) {
+               await import(pathToFileURL(instrumentation).href);
+       }
+
        /** @type {import('types').ServerInternalModule} */
        const { set_assets } = await import(pathToFileURL(join(dir, 'internal.js')).href);