Statische Serverfunktionen sind Serverfunktionen, die zur Build-Zeit ausgeführt und als statische Assets zwischengespeichert werden, wenn Prerendering/statische Generierung verwendet wird. Sie können in den Modus "statisch" versetzt werden, indem die Option type: 'static' an createServerFn übergeben wird.
const myServerFn = createServerFn({ type: 'static' }).handler(async () => {
return 'Hello, world!'
})
const myServerFn = createServerFn({ type: 'static' }).handler(async () => {
return 'Hello, world!'
})
Dieses Muster folgt diesem Ablauf
Standardmäßig speichert und ruft die Implementierung des statischen Caches von Serverfunktionen statische Daten im Build-Ausgabeverzeichnis über das fs-Modul von Node ab und ruft die Daten zur Laufzeit ebenfalls mit einem fetch-Aufruf an dieselbe statische Datei ab.
Diese Schnittstelle kann angepasst werden, indem die Funktion createServerFnStaticCache importiert und aufgerufen wird, um eine benutzerdefinierte Cache-Implementierung zu erstellen, und dann setServerFnStaticCache aufgerufen wird, um sie festzulegen.
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)
Ihre wöchentliche Dosis JavaScript-Nachrichten. Jeden Montag kostenlos an über 100.000 Entwickler geliefert.