reactjstanstacktanstack-start

Can not use staticFunctionMiddleware to implement Static Server Functions


I am trying to follow docs on TanStack Start, as every other day :) Today I was studying Static Server Functions and there example is given:

import { createServerFn } from '@tanstack/react-start'
import { staticFunctionMiddleware } from '@tanstack/start-static-server-functions'

const myServerFn = createServerFn({ method: 'GET' })
  .middleware([staticFunctionMiddleware])
  .handler(async () => {
    return 'Hello, world!'
  })

However after copying it, I got an error

Type 'FunctionMiddlewareAfterServer<{}, unknown, undefined, undefined, undefined, undefined, undefined>' is not assignable to type 'AnyRequestMiddleware | AnyFunctionMiddleware | AnyServerFn'.
  Property '[TSS_SERVER_FUNCTION_FACTORY]' is missing in type 'FunctionMiddlewareAfterServer<{}, unknown, undefined, undefined, undefined, undefined, undefined>' but required in type 'ServerFnWithTypes<any, any, any, any, any>'.

I thought it may be "transient" issue due to TanStack Start being in prerelease mode, however when I ts-ignored the problem, browser threw this:

import_node_async_hooks.AsyncLocalStorage is not a constructor
    at @tanstack_start-static-server-functions.js?t=1759421434724&v=cb209fed:1096:20

The above error occurred in the <Lazy> component.

React will try to recreate this component tree from scratch using the error boundary you provided, CatchBoundaryImpl.

The question is: how can I correctly use staticFunctionMiddleware?

Version used: "@tanstack/start-static-server-functions": "^1.132.31"

As of time of writing there's new version of the package 1.132.33, but the issue still persists.


Solution

  • Currently this is issue at TanStack start docs and/or in the library itself.

    This is reported under this GitHub issue.