npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hebilicious/server-block-nuxt

v0.3.4

Published

[![CI](https://github.com/Hebilicious/server-block-nuxt/actions/workflows/ci.yaml/badge.svg)](https://github.com/Hebilicious/server-block-nuxt/actions/workflows/ci.yaml) [![npm version][npm-version-src]][npm-version-href] [![npm downloads][npm-downloads-s

Downloads

180

Readme

Server Block Nuxt

CI npm version npm downloads License Nuxt

🚀 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

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

🫴 Contributing

Feedback, issues and PRs are welcomed.