Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method already declared issue with SPA mode in production #184

Open
2 tasks done
Teddy-Schmitz opened this issue Oct 28, 2024 · 4 comments
Open
2 tasks done

Method already declared issue with SPA mode in production #184

Teddy-Schmitz opened this issue Oct 28, 2024 · 4 comments

Comments

@Teddy-Schmitz
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

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/[email protected]/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/[email protected]/node_modules/fastify/lib/route.js:371:19)
app-1  |     at Object.route (/app/node_modules/.pnpm/[email protected]/node_modules/fastify/lib/route.js:265:19)
app-1  |     at Object.prepareRoute (/app/node_modules/.pnpm/[email protected]/node_modules/fastify/lib/route.js:167:18)
app-1  |     at Object._head [as head] (/app/node_modules/.pnpm/[email protected]/node_modules/fastify/fastify.js:265:34)
app-1  |     at fastifyStatic (/app/node_modules/.pnpm/@[email protected]/node_modules/@fastify/static/index.js:120:15)
app-1  |     at Plugin.exec (/app/node_modules/.pnpm/[email protected]/node_modules/avvio/lib/plugin.js:125:28)
app-1  |     at Boot._loadPlugin (/app/node_modules/.pnpm/[email protected]/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
Copy link

AnikaGieringerQC commented Nov 28, 2024

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
Copy link
Member

galvez commented Nov 28, 2024

I'll take a look now.

@zanmato
Copy link
Contributor

zanmato commented Dec 2, 2024

It's due to the public/ folder route in production. IMO it should be removed: https://github.com/fastify/fastify-vite/blob/dev/packages/fastify-vite/mode/production.js#L66-L73 or at least use wildcard: false

@galvez
Copy link
Member

galvez commented Dec 2, 2024

Indeed — evaluating the best fix for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants