静态服务器函数是在构建时执行的服务器函数,当使用预渲染/静态生成时,它们会被缓存为静态资源。可以通过将 type: 'static' 选项传递给 createServerFn 来将它们设置为“静态”模式
const myServerFn = createServerFn({ type: 'static' }).handler(async () => {
return 'Hello, world!'
})
const myServerFn = createServerFn({ type: 'static' }).handler(async () => {
return 'Hello, world!'
})
此模式流程如下
默认情况下,静态服务器函数缓存实现通过 node 的 fs 模块在构建输出目录中存储和检索静态数据,同样地,在运行时使用对相同静态文件的 fetch 调用来获取数据。
可以通过导入并调用 createServerFnStaticCache 函数来创建自定义缓存实现,然后调用 setServerFnStaticCache 来设置它,从而自定义此接口
import {
createServerFnStaticCache,
setServerFnStaticCache,
} from '@tanstack/react-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/react-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)
您的每周 JavaScript 新闻速递。每周一免费发送给超过 100,000 名开发者。