🐛 Testing out the new SSR cookie forward

This commit is contained in:
2025-10-04 15:49:09 +08:00
parent 453319bac1
commit cad3b38432
5 changed files with 17 additions and 9 deletions

View File

@@ -75,7 +75,7 @@ if (
console.log("\nnpx nuxi module add icon\n")
}
const apiBaseServer = useSolarNetworkUrl(true)
const apiBaseServer = useSolarNetworkUrl()
function toAbsoluteUrl(url: string | undefined) {
if (!url) return undefined

View File

@@ -1,9 +1,16 @@
// Solar Network aka the api client
import { keysToCamel, keysToSnake } from "~/utils/transformKeys"
export const useSolarNetwork = (withoutProxy = false) => {
const apiBase = useSolarNetworkUrl(withoutProxy)
const headers = process.server ? useRequestHeaders(["cookie"]) : {}
export const useSolarNetwork = () => {
const apiBase = useSolarNetworkUrl()
// Access SSR request event only on the server
const event = import.meta.server ? useRequestEvent() : null
// Forward cookies from the incoming request
const headers: HeadersInit = import.meta.server && event
? { cookie: event.headers.get('cookie') ?? '' }
: {}
return $fetch.create({
baseURL: apiBase,
@@ -11,7 +18,7 @@ export const useSolarNetwork = (withoutProxy = false) => {
headers,
// Add Authorization header with Bearer token
onRequest: ({ request, options }) => {
const side = process.server ? "SERVER" : "CLIENT"
const side = import.meta.server ? "SERVER" : "CLIENT"
console.log(`[useSolarNetwork] onRequest for ${request} on ${side}`)
// Transform request data from camelCase to snake_case
@@ -28,7 +35,7 @@ export const useSolarNetwork = (withoutProxy = false) => {
})
}
export const useSolarNetworkUrl = (withoutProxy = false) => {
export const useSolarNetworkUrl = () => {
const config = useRuntimeConfig()
return config.public.apiBase
}

View File

@@ -203,7 +203,7 @@ const username = computed(() => {
// Use useFetch with the correct API URL to avoid router conflicts
const apiBase = useSolarNetworkUrl()
const apiBaseServer = useSolarNetworkUrl(true)
const apiBaseServer = useSolarNetworkUrl()
try {
const { data, error } = await useFetch(

View File

@@ -27,7 +27,7 @@ function loadTheme(mode: string) {
}
}
const apiBase = useSolarNetworkUrl(true)
const apiBase = useSolarNetworkUrl()
onMounted(() => {
const ui = SwaggerUIBundle({

View File

@@ -7,7 +7,8 @@ export default withNuxt(
rules: {
"vue/multi-word-component-names": "off",
"vue/no-v-html": "off",
"vue/html-self-closing": "off"
"vue/html-self-closing": "off",
"@typescript-eslint/ban-ts-comment": "off"
}
}
)