Cone
Cone
SSolidJS
Created by Cone on 12/11/2024 in #support
Run server code only once
I'm trying to run an embedded db in SolidStart, but every time a new route is visited the db initialization is re-run and the db data gets corrupted. Is there a way to only run the db code once and reuse it for all routes? I saw this issue in nitro which seems like it would solve it, but the code still runs for every route https://github.com/nitrojs/nitro/issues/711#issuecomment-1415819402 My db initialization in utils/db.ts looks like:
import { Database } from "db"

const DATA_DIR = "./.data"

let _db: Database
export const useDB = () => {
if (!_db) {
_db = new Database(DATA_DIR)
}
return _db
}
import { Database } from "db"

const DATA_DIR = "./.data"

let _db: Database
export const useDB = () => {
if (!_db) {
_db = new Database(DATA_DIR)
}
return _db
}
3 replies
SSolidJS
Created by Cone on 12/6/2024 in #support
Throwing a redirect inside a query
When throwing a redirect from a query, the throw gets caught and returns undefined instead. The inferred return type is wrong in this case, but when throwing other values the throw is not caught. Is the query function typing wrong, or am I using query wrong?
import { query, redirect } from "@solidjs/router"

const queryThatThrows = query(async () => {
"use server"
const condition = true
if (condition) {
throw redirect("/foo")
}
return true
}, "query-that-throws")

const otherQuery = query(async () => {
"use server"
const v = await queryThatThrows()
type V = typeof v // boolean
// this still runs because the throw gets caught
console.log(v) // undefined
}, "other-query")
import { query, redirect } from "@solidjs/router"

const queryThatThrows = query(async () => {
"use server"
const condition = true
if (condition) {
throw redirect("/foo")
}
return true
}, "query-that-throws")

const otherQuery = query(async () => {
"use server"
const v = await queryThatThrows()
type V = typeof v // boolean
// this still runs because the throw gets caught
console.log(v) // undefined
}, "other-query")
3 replies