Skip to content

Static routes ignore registration prefix #358

@jamcalli

Description

@jamcalli

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

https://github.com/jamcalli/fastify-vite-prefix-repro

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/assets

This 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:

  1. Automatic - Inherit from opts.prefix automatically (most consistent with Fastify behavior)
  2. Opt-in - Add a config option like useRegistrationPrefix: true
  3. Explicit - Accept a separate staticPrefix option

Let me know which approach you'd prefer, or if you have something else in mind.

Link to code that reproduces the bug

https://github.com/jamcalli/fastify-vite-prefix-repro

Expected Behavior

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions