Update: Implementation specs: https://gist.github.com/pi0/76bd2991ebe0b575a0eac08bb7a2e668
Introduction
Introducing a functions/ directory could have a powerful effect for users that want to add server functions but without having to go through the serverMiddleware.
functions/helloWorld.js
export default async function ({ req, res, data, nuxt }) {
// data is parsed from req.body
// nuxt is actual Nuxt instance
return { title: 'Hello world!' }
})
On the app side, it will expose a $functions object with $functions.<functionName> method (asynchronous).
pages/hello.vue
<template>
<h1>{{ title }}</h1>
</template>
<script>
export default {
async asyncData ({ $functions }) {
const { title } = await $functions.helloWorld()
return { title }
}
}
</script>
How does it work?
@nuxt/functions will:
- Create a serverMiddleware on
/_nuxt/functions/:functionName
- Read and watch
${srcDir}/functions/**/*.js|ts files with chokidar and add to nuxt.$functions (kind of inject but for nuxt instance + ssrContext)
- Inject
$functions in the Nuxt app with this.addPlugin (server = call ssrContext.$functions method, client, call in POST /_nuxt/function/${functionName} with fetch)
With this system, we could leverage HMR for functions/ in dev mode. Also, we can imagine a way to export this functions as serverless (for client-side call) when running nuxt build with target: 'serverless' or static``, see target pr. We could imagine some community modules to support transforming theses functions to Now V2, Netlify Functions, AWS Lambda, etc.
I believe this could be an official Nuxt module without having to be installed right away, combined with lmify inside Nuxt, it could be installed automatically if a functions/ directory is detected 🔥
In this area, we can leverage a @nuxt/sse module to expose nuxt.$sse in functions and this.$sse in the Vue app.
PS: 0 breaking change 😇
PS2: We could also add req.nuxt for serverMiddleware to get access to nuxt.$sse
Update: Implementation specs: https://gist.github.com/pi0/76bd2991ebe0b575a0eac08bb7a2e668
Introduction
Introducing a
functions/directory could have a powerful effect for users that want to add server functions but without having to go through the serverMiddleware.functions/helloWorld.jsOn the app side, it will expose a
$functionsobject with$functions.<functionName>method (asynchronous).pages/hello.vueHow does it work?
@nuxt/functionswill:/_nuxt/functions/:functionName${srcDir}/functions/**/*.js|tsfiles with chokidar and add tonuxt.$functions(kind ofinjectbut fornuxtinstance +ssrContext)$functionsin the Nuxt app withthis.addPlugin(server = callssrContext.$functionsmethod, client, call in POST/_nuxt/function/${functionName}withfetch)With this system, we could leverage HMR for
functions/in dev mode. Also, we can imagine a way to export this functions as serverless (for client-side call) when runningnuxt buildwithtarget: 'serverless' orstatic``, see target pr. We could imagine some community modules to support transforming theses functions to Now V2, Netlify Functions, AWS Lambda, etc.I believe this could be an official Nuxt module without having to be installed right away, combined with lmify inside Nuxt, it could be installed automatically if a
functions/directory is detected 🔥In this area, we can leverage a
@nuxt/ssemodule to exposenuxt.$ssein functions andthis.$ssein the Vue app.PS: 0 breaking change 😇
PS2: We could also add
req.nuxtfor serverMiddleware to get access tonuxt.$sse