Statische Serverfunktionen sind Serverfunktionen, die zur Build-Zeit ausgeführt und als statische Assets zwischengespeichert werden, wenn Sie Prerendering/statische Generierung verwenden. Sie können auf den Modus "statisch" gesetzt werden, indem Sie die Option type: 'static' an createServerFn übergeben.
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 Serverfunktionscaches statische Daten im Build-Ausgabeverzeichnis über das fs-Modul von Node ab und ruft die Daten zur Laufzeit mithilfe eines fetch-Aufrufs derselben statischen 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 diese festzulegen.
import {
createServerFnStaticCache,
setServerFnStaticCache,
} from '@tanstack/solid-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/solid-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.