Nuxt 2 is reaching End-of-Life on June 30th, 2024.


Use <server> tags in your Nuxt pages components.

Server Block Nuxt

CInpm versionnpm downloadsLicenseNuxt


๐Ÿš€ Welcome to Server Block Nuxt!

๐Ÿงช This module is experimental ๐Ÿงช

Nuxt Module that adds server block supports in your pages components.

<server lang="ts"></server>
<script lang="ts" setup></script>

You can think of server block as a convenient way to write API handlers in your pages components.

๐Ÿ“ฆ Install

Install the module and the volar extension :

npm i -D @hebilicious/server-block-nuxt @hebilicious/sfc-server-volar

Add the module to your Nuxt config :

export default defineNuxtConfig({
  modules: [

That's it ! The volar extension will be automatically installed by the nuxt module.

๐Ÿ“– Usage

  • Server blocks are only available in pages components.
  • default exports are not available in server blocks. Use named exports.

Add a server block in a page component :

<server lang="ts">
const message = "Hello World!!!"
const bye = "bye!"
export const GET = defineEventHandler(() =>({ message }))
export const POST = defineEventHandler(() =>({ message: bye }))

<script setup lang="ts">
const { data } = useFetch("/api/message")

  <div> Hello Message, {{ data }} </div>

This will generate 2 handlers in server/.generated/api :

  • GET : server/.generated/api/message.get.ts
  • POST : server/.generated/api/

All HTTP methods are supported.

Custom route

You can override the default route convention with the path attribute :

<server lang="ts" path="/not-api/this/is/cool">
export const GET = defineEventHandler((event) => {
  return "We're here now."

<script setup lang="ts">
const { data } = useFetch("/not-api/this/is/cool")

  <div> {{ data }} </div>

A server/.generated/not-api/this/is/cool.get.ts get handler will be generated.

๐Ÿ’ก FAQ

Why <server> and not <script server> ?

  • <script server> causes issues with the current behaviour of additional script tags in SFCs (notably with import/exports)
  • <server> blocks are completely removed from the SFC and don't interfere with <template> or <script>, they create a clear boundary.
  • The syntax highlighting work in environments that uses the lang attribute. I would like github support too.

Why no defineServerProps or loaders ?

You can combine this with another library such as if you want to use form actions and loaders.

Should I commit the generated files to my repository? No. A .gitignore file will be generated for you.


  • Integrates with form-actions & loaders
  • Add useFetch typings
  • Support multiple server blocks in a single file

๐Ÿซด Contributing

Feedback, issues and PRs are welcomed.