Skip to content

Commit 943af16

Browse files
authored
Check before registering @fastify/middie (#355)
1 parent 2fbb52c commit 943af16

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

.changeset/jolly-roses-write.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@fastify/vite": patch
3+
---
4+
5+
Check decorators before registering `@fastify/middie` internally. This should allow middie to be registered separately if users want to configure specific options.

docs/guide/known-limitations.md

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
11
# Known Limitations
22

3-
- It's currently impossible to run **`@fastify/vite`** in an encapsulated Fastify plugin. You'll need to register **`@fastify/vite`** at the root context, or if you want to use it in a plugin, that plugin must be wrapped in [`fastify-plugin`](https://github.com/fastify/fastify-plugin) so it's also registered at the root context.
3+
## Encapsulated Plugin Registration
44

5-
> Investigation on this has started but is still pending — everything points to **`@fastify/middie`**, the Express compatibility layer needed to run the Vite development server middleware. It's just considered low-prio at the moment as in most setups, only a single instance is needed anyway.
5+
It's currently impossible to run `@fastify/vite` in an encapsulated Fastify plugin. You'll need to register `@fastify/vite` at the root context, or if you want to use it in a plugin, that plugin must be wrapped in [`fastify-plugin`](https://github.com/fastify/fastify-plugin) so it's also registered at the root context.
66

7-
- It's currently impossible to run **multiple** Vite development server middleware in your Fastify server, which means `@fastify/vite` can **only be registered once**. Configuration for this is somewhat tricky and there isn't documentation on how to do it. Once [#108](https://github.com/fastify/fastify-vite/pull/108) is completed and merged, it will open the path to have a Vite development server factory that can create instances on-demand, but that approach still remains to be tested.
7+
::: info
8+
Investigation on this has started but is still pending — everything points to `@fastify/middie`, the Express compatibility layer needed to run the Vite development server middleware. It's just considered low-prio at the moment as in most setups, only a single instance is needed anyway.
9+
:::
810

9-
If you're looking into a microfrontend setup, consider [this approach](https://dev.to/getjv/react-micro-frontends-with-vite-5442).
11+
## Multiple Vite Instances
12+
13+
It's currently impossible to run **multiple** Vite development server middleware in your Fastify server, which means `@fastify/vite` can **only be registered once**. Configuration for this is somewhat tricky and there isn't documentation on how to do it. Once [#108](https://github.com/fastify/fastify-vite/pull/108) is completed and merged, it will open the path to have a Vite development server factory that can create instances on-demand, but that approach still remains to be tested.
14+
15+
If you're looking into a microfrontend setup, consider [this approach](https://dev.to/getjv/react-micro-frontends-with-vite-5442).
16+
17+
## Using @fastify/middie Directly
18+
19+
`@fastify/vite` internally registers `@fastify/middie` to run Vite's development server middleware. If you need to register `@fastify/middie` yourself (e.g., to pass custom options), register it **before** `@fastify/vite`:
20+
21+
```js
22+
import Fastify from 'fastify'
23+
import middie from '@fastify/middie'
24+
import FastifyVite from '@fastify/vite'
25+
26+
const server = Fastify()
27+
28+
// Register middie first with your options
29+
await server.register(middie, { /* your options */ })
30+
await server.register(FastifyVite, { /* options */ })
31+
```
32+
33+
`@fastify/vite` checks if middie is already registered and skips its own registration if so.

packages/fastify-vite/mode/development.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ async function setup(config) {
4646
const { createServer, createServerModuleRunner, mergeConfig, defineConfig } = await import('vite')
4747

4848
// Middie seems to work well for running Vite's development server
49-
// Unsure if fastify-express is warranted here
50-
await this.scope.register(middie)
49+
// Check if middie is already registered (allows users to register it themselves)
50+
if (!this.scope.hasDecorator('use')) {
51+
await this.scope.register(middie)
52+
}
5153

5254
// Create and enable Vite's Dev Server middleware
5355
const devServerOptions = mergeConfig(

0 commit comments

Comments
 (0)