Giter Site home page Giter Site logo

server-block-nuxt's Introduction

Server Block Nuxt

CI npm version npm downloads License Nuxt

image

๐Ÿš€ 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>
<template></template>
<style></style>

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: [
    "@hebilicious/server-block-nuxt"
  ]
})

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 }))
</server>

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

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

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

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

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."
})
</server>

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

<template>
  <h1>Hello</h1>
  <div> {{ data }} </div>
</template>

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 https://github.com/Hebilicious/form-actions-nuxt 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.

๐Ÿ“ TODO

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

๐Ÿซด Contributing

Feedback, issues and PRs are welcomed.

server-block-nuxt's People

Contributors

hebilicious avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

server-block-nuxt's Issues

Why only in page components?

Describe the feature

"Server blocks are only available in pages components."

It will export the block also from components/* and generate server/.generated/* files. And <server path="..."> works with it as well.

They only seem not to be "activated.

What is missing / why won't it work with non-page components?

Additional information

  • Would you be willing to help implement this feature?

"server-block", "form-actions" sort

will throw error if not correct sort
ERROR Cannot start nuxt: ENOENT: no such file or directory, scandir '...\server\.generated\.loader'

Can you note it on README.md
in nuxt config

export default defineNuxtConfig({
  modules: [
    //correct
    "@hebilicious/server-block-nuxt",
    "@hebilicious/form-actions-nuxt",
    //...
  ]
})

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): update actions/checkout digest to 0ad4b8f
  • chore(deps): update devdependency eslint to v9
  • chore(deps): update pnpm to v9
  • ๐Ÿ” Create all rate-limited PRs at once ๐Ÿ”

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/ci.yaml
  • actions/checkout v4@b4ffde65f46336ab88eb53be808477a3936bae11
  • actions/setup-node v3
  • actions/checkout v4@b4ffde65f46336ab88eb53be808477a3936bae11
  • actions/setup-node v3
.github/workflows/release.yaml
  • actions/checkout v4@b4ffde65f46336ab88eb53be808477a3936bae11
  • actions/setup-node v3
npm
docs/package.json
  • vitepress 1.0.0-rc.40
package.json
  • @antfu/ni ^0.21.12
  • @hebilicious/eslint-config 0.0.3-beta.3
  • @nuxt/test-utils ^3.11.0
  • @types/node ^18.19.10
  • bumpp ^9.3.0
  • conventional-changelog-cli ^4.1.0
  • eslint 8.56.0
  • esno ^0.17.0
  • fast-glob ^3.3.2
  • lint-staged ^14.0.1
  • pnpm 8.15.1
  • prettier ^3.2.4
  • rimraf ^5.0.5
  • simple-git-hooks ^2.9.0
  • taze ^0.13.1
  • turbo ^1.11.3
  • typescript ^5.3.3
  • unbuild ^2.0.0
  • vitest ^0.34.6
  • pnpm 8.15.1
packages/extract-sfc-block/package.json
  • @vitejs/plugin-vue ^4.6.2
  • @vue/shared ^3.4.15
  • consola ^3.2.3
  • unplugin ^1.6.0
  • vite ^4.5.0
  • vue ^3.4.15
packages/server-block-nuxt/package.json
  • @nuxt/kit 3.9.3
  • consola ^3.2.3
  • defu ^6.1.4
  • esbuild ^0.20.0
  • magicast ^0.3.3
  • @nuxt/module-builder ^0.5.5
  • h3 *
  • nitropack *
  • nuxt *
  • vite *
packages/sfc-server-volar/package.json
  • @vue/language-core ^1.8.27
playgrounds/basic/package.json
  • @nuxt/devtools ^0.8.5
playgrounds/prisma/package.json
  • @prisma/client ^5.8.1
  • @types/node ^18.19.10
  • esno ^0.17.0
  • nuxt ^3.9.3
  • prisma ^5.8.1

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.