-
-
Notifications
You must be signed in to change notification settings - Fork 96
Static routes ignore registration prefix #358
Description
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Fastify version
5.6.2
Plugin version
8.4.0
Node.js version
24.12.0
Operating system
Linux
Operating system version (i.e. 20.04, 11.3, 10)
Debian GNU/Linux 12
Description
Following up from #297.
This is a unique requirement, but one that I'm sure would make sense to include. I use this plugin for developing self-hosted apps where users need full control over their setup/routing, and basePath support is a top requested feature that I have spent a lot of time figuring out how to piece together without requiring rebuilding the container.
The Issue
When registering a Fastify plugin with a prefix (e.g., { prefix: '/someprefix' }), all routes respect that prefix. However, @fastify/vite static assets are always served at /assets/* instead of /someprefix/assets/*. This breaks reverse proxy setups where only /someprefix* routes are forwarded to the app.
Root Cause
@fastify/vite hardcodes the static prefix from vite.base instead of using opts.prefix. In mode/production.js:
await scope.register(FastifyStatic, {
root,
prefix: join(
URL.canParse(vite.base) ? new URL(vite.base).pathname : vite.base || '/',
assetsDir,
).replace(/\\/g, '/'),
})Reproduction
opts.prefix is available - @fastify/vite just doesn't use it.
Proposed Fix
Prepend opts.prefix to the static route paths:
// Current
prefix: join(vite.base, assetsDir)
// Result: /assets
// Fixed
prefix: join(opts.prefix || '', vite.base, assetsDir)
// Result: /someprefix/assetsThis would need to be applied to both the assetFiles and publicFiles registrations.
Happy to submit a PR for this. A few options on how to approach it:
- Automatic - Inherit from
opts.prefixautomatically (most consistent with Fastify behavior) - Opt-in - Add a config option like
useRegistrationPrefix: true - Explicit - Accept a separate
staticPrefixoption
Let me know which approach you'd prefer, or if you have something else in mind.
Link to code that reproduces the bug
Expected Behavior
No response