create-nuxt-app
create-nuxt-app copied to clipboard
Nuxt blocks Fastify routes
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
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)
}
}
)
Having exactly same problem here.
@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 what is nuxt/functions?
It’s a new way to setup server functions.
RFC is here https://github.com/nuxt/rfcs/issues/35
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!
That’s a good point, I think it’s not in current implementation spec yet. cc @pi0
@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.