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

use-server-directive

v0.4.3

Published

Universal "use-server" functions

Downloads

12

Readme

use-server-directive

Universal use server functions

NPM JavaScript Style Guide

Install

npm i use-server-directive
yarn add use-server-directive
pnpm add use-server-directive

Features

Server functions

Like the original "use server" directive, the compiler supports functions.

async function doStuff(x, y) {
  "use server";
  await foo(x);
  await bar(y);
}
// also works for arrow functions

const doStuff = async (x, y) => {
  "use server";
  await foo(x);
  await bar(y);
};

The compiler also supports async generators


async function* doStuff(x, y) {
  "use server";
  yield foo(x);
  yield bar(y);
}

NOTE Server functions are only valid for async functions.

Server blocks

The original "use server" is limited to functions, but what if you could mark block statements with the same directives?

if (someCond()) {
  'use stuff';
  await doStuff();
}

use-server-directive supports server blocks in almost all statements that supports it:

  • if-else
  • try-catch-finally
  • for
  • for-in
  • for-of
  • for await
  • while
  • do-while
  • labeled statements

Server blocks also supports break, continue, return and throw statements, as well as yield expressions and delegations.

for (const item of items) {
  'use server';
  await processItem(item);
}

NOTE Server blocks are only supported within async functions and at top-level scope (since modules now support top-level await)

Closure extraction

use-server-directive supports closure extraction

async function foo() {
  const prefix = 'Message: ';

  async function postMessage(message) {
    'use server';
    await addMessage(prefix + message);
  }
}

Streaming server functions

If a server function returns a value with a Promise, ReadableStream or AsyncIterable, those instances' values are going to be streamed through the response.

async function getMessage() {
  'use server';
  return {
    // `getAsyncData` returns a Promise
    // On the client-side, this object is going to
    // be received immedatiely, but the value
    // to which the Promise resolves into
    // is going to be streamed after.
    message: getAsyncData(),
  };
}

Advanced serialization

use-server-directive supports a wide range of data types, you can check the compatibility table here

Customizable directive

Integrations

Examples

Preloading

There are instances where a server function is only imported through a dynamic import, which causes unspecified registration timing, wherein the function might be available on the client but not on the server.

To allow registration of server functions immediately, you can import use-server-directive/preload on any server entrypoints that will load immediately when the server runs.

import 'use-server-directive/preload';

Sponsors

Sponsors

License

MIT © lxsmnsyc