fastify / fastify-vite

Fastify plugin for Vite integration
MIT License
891 stars 75 forks source link

Method already declared issue with SPA mode in production #184

Open Teddy-Schmitz opened 1 month ago

Teddy-Schmitz commented 1 month ago

Prerequisites

Fastify version

4.28.1

Plugin version

6.0.7

Node.js version

20.9.0

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

Arch

Description

Hello, I'm having issues getting fastify-vite to run in production mode and from what I can tell it has to do with how fastify-static is setup. Basically I have setup it very simply to grab all routes and return the client side application, this has worked fine in development mode but when I build and run it in production mode I get the below error.

I'm not sure if there is something I can do to fix this on my side, all the examples use "/" as the main route but that doesn't work properly for a client side application I want all routes (not already defined) to load the client side app. I saw this config referenced in another issue so I copied it from there.

await server.register(FastifyVite, {
  root: import.meta.url,
  dev: process.argv.includes('--dev'),
  spa: true,
});

server.get('*', (req, reply) => {
  return reply.html();
});
app-1  | /app/node_modules/.pnpm/fastify@4.28.1/node_modules/fastify/lib/route.js:371
app-1  |             throw new FST_ERR_DUPLICATED_ROUTE(opts.method, opts.url)
app-1  |                   ^
app-1  | FastifyError [Error]: Method 'HEAD' already declared for route '/*'
app-1  |     at Object.addNewRoute (/app/node_modules/.pnpm/fastify@4.28.1/node_modules/fastify/lib/route.js:371:19)
app-1  |     at Object.route (/app/node_modules/.pnpm/fastify@4.28.1/node_modules/fastify/lib/route.js:265:19)
app-1  |     at Object.prepareRoute (/app/node_modules/.pnpm/fastify@4.28.1/node_modules/fastify/lib/route.js:167:18)
app-1  |     at Object._head [as head] (/app/node_modules/.pnpm/fastify@4.28.1/node_modules/fastify/fastify.js:265:34)
app-1  |     at fastifyStatic (/app/node_modules/.pnpm/@fastify+static@6.12.0/node_modules/@fastify/static/index.js:120:15)
app-1  |     at Plugin.exec (/app/node_modules/.pnpm/avvio@8.4.0/node_modules/avvio/lib/plugin.js:125:28)
app-1  |     at Boot._loadPlugin (/app/node_modules/.pnpm/avvio@8.4.0/node_modules/avvio/boot.js:432:10)
app-1  |     at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
app-1  |   code: 'FST_ERR_DUPLICATED_ROUTE',
app-1  |   statusCode: 500
app-1  | }

Link to code that reproduces the bug

No response

Expected Behavior

Fastify starts normally

AnikaGieringerQC commented 3 days ago

We are also trying to use fastify-vite in production and are experiencing the exact same problem with both, version 6 and version 7 of fastify-vite. We currently fallback to version 5.. of fastify-vite with which this issue doesn't happen. However this started to block other major version updates (e.g. updating fastify to the latest major version). It would be great if someone could take a look at this!

galvez commented 3 days ago

I'll take a look now.