Static server functions are server functions that are executed at build time and cached as static assets when using prerendering/static-generation. They can be set to "static" mode by passing the type: 'static' option to createServerFn:
const myServerFn = createServerFn({ type: 'static' }).handler(async () => {
return 'Hello, world!'
})
const myServerFn = createServerFn({ type: 'static' }).handler(async () => {
return 'Hello, world!'
})
This pattern goes as follows:
By default, the static server function cache implementation stores and retrieves static data in the build output directory via node's fs module and likewise fetches the data at runtime using a fetch call to the same static file.
This interface can be customized by importing and calling the createServerFnStaticCache function to create a custom cache implementation and then calling setServerFnStaticCache to set it:
import {
createServerFnStaticCache,
setServerFnStaticCache,
} from '@tanstack/start/client'
const myCustomStaticCache = createServerFnStaticCache({
setItem: async (ctx, data) => {
// Store the static data in your custom cache
},
getItem: async (ctx) => {
// Retrieve the static data from your custom cache
},
fetchItem: async (ctx) => {
// During runtime, fetch the static data from your custom cache
},
})
setServerFnStaticCache(myCustomStaticCache)
import {
createServerFnStaticCache,
setServerFnStaticCache,
} from '@tanstack/start/client'
const myCustomStaticCache = createServerFnStaticCache({
setItem: async (ctx, data) => {
// Store the static data in your custom cache
},
getItem: async (ctx) => {
// Retrieve the static data from your custom cache
},
fetchItem: async (ctx) => {
// During runtime, fetch the static data from your custom cache
},
})
setServerFnStaticCache(myCustomStaticCache)
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.