create-nuxt-app icon indicating copy to clipboard operation
create-nuxt-app copied to clipboard

Nuxt blocks Fastify routes

Open Liumkattan opened this issue 4 years ago • 8 comments

It appears that Nuxt nuxt.render method blocks Fastify routes.

Reproduction

Use npx create-nuxt-app <project-name> with the following configuration

create-nuxt-app v2.15.0
✨  Generating Nuxt.js project in Test
? Project name Test
? Project description My awe-inspiring Nuxt.js project
? Author name 
? Choose programming language JavaScript
? Choose the package manager Yarn
? Choose UI framework Vuetify.js
? Choose custom server framework Fastify
? Choose Nuxt.js modules Axios, Progressive Web App (PWA) Support, DotEnv
? Choose linting tools ESLint, Prettier
? Choose test framework AVA
? Choose rendering mode Universal (SSR)
? Choose development tools jsconfig.json (Recommended for VS Code)
yarn run v1.22.4

Then add a simple fastify route to /server/index.js

fastify.get('/test', (request, reply) => {
  reply.send({ hello: 'world' })
})

Result

Fastify routes returns 404

What i have tried

  • Disabling Nuxt render (fastify.use(nuxt.render)) does allow Fastify routes to be exposed

I guess adding a whitelist for Fastify routes and handling that with Nuxt.render would work, but that would be inconvenient.

Thank you

Liumkattan avatar Apr 27 '20 02:04 Liumkattan

Yeah I noticed the same thing. I don't think nuxt's render method is calling next.

For anyone else, I followed the whitelist approach as noted:

fastify.use(
    (_req, res, next) => {
      if (_req.url.startsWith("/api") > 0) {
        next()
      } else {
        nuxt.render(_req, res, next)
      }
    }
  )

kdeenanauth avatar May 08 '20 05:05 kdeenanauth

Having exactly same problem here.

paulogr avatar Jun 03 '20 18:06 paulogr

@kdeenanauth 's solution is correct, if we need extra routes other than nuxt render, we may have to exclude nuxt.render from specific route namespace for not intercept those requests.

BTW, we'll remove all server templates from next major release of create-nuxt-app as Nuxt has published new programmatic API and nuxt/functions.

clarkdo avatar Jun 03 '20 19:06 clarkdo

@clarkdo what is nuxt/functions?

paulogr avatar Jun 03 '20 20:06 paulogr

It’s a new way to setup server functions.

RFC is here https://github.com/nuxt/rfcs/issues/35

clarkdo avatar Jun 03 '20 20:06 clarkdo

Uow looks awesome!

Do you know with nuxt/funcions we can have access to installed modules like $axios for example?

That's is my main concern about serverMiddleware today, we can't have any access to nuxt instance.

Thank you!

paulogr avatar Jun 03 '20 20:06 paulogr

That’s a good point, I think it’s not in current implementation spec yet. cc @pi0

clarkdo avatar Jun 03 '20 21:06 clarkdo

@paulogr Unfortunately it is not universally possible because this means we need to load/init the entire SSR bundle even for client-side calls to functions. But the good news is that functions supporting a similar feature called services so we can have axios service.

pi0 avatar Jun 03 '20 21:06 pi0