Add fallback to offline data in case API not ready
This commit is contained in:
parent
ea07766fd8
commit
1624d9eb7f
5 changed files with 28 additions and 14 deletions
|
|
@ -1,11 +1,11 @@
|
|||
import {cmsBase} from '$lib/data/CMSDataType'
|
||||
import { error } from '@sveltejs/kit';
|
||||
import type { PageServerLoad } from './$types'
|
||||
export const load: PageServerLoad = async ({fetch}) => {
|
||||
const res = await fetch(cmsBase+"/home")
|
||||
if(!res.ok)
|
||||
throw error(500, 'ไม่สามารถอ่านข้อมูลตั้งต้นของเวปได้');
|
||||
|
||||
const info = await res.json()
|
||||
return info
|
||||
{
|
||||
console.log("load offline data instead")
|
||||
return (await import('$lib/data/info.json')).default
|
||||
}
|
||||
return await res.json()
|
||||
}
|
||||
|
|
@ -4,20 +4,33 @@ import type { PageServerLoad } from './$types';
|
|||
import type {Exam} from '$lib/data/CMSDataType'
|
||||
import dayjs from 'dayjs'
|
||||
export const load: PageServerLoad = async ({fetch}) => {
|
||||
let res = await fetch("/api/info")
|
||||
let res = await fetch(cmsBase+"/home")
|
||||
let info
|
||||
if(!res.ok)
|
||||
throw error(500, 'ไม่สามารถอ่านข้อมูลตั้งต้นของหน้าหลักได้');
|
||||
const {title,subtitle} = await res.json()
|
||||
{
|
||||
console.log("load offline CMS data instead")
|
||||
info = (await import('$lib/data/info.json')).default
|
||||
}else{
|
||||
info = await res.json()
|
||||
}
|
||||
const {title,subtitle} = info
|
||||
|
||||
res = await fetch(cmsBase+"/content/home")
|
||||
if(!res.ok)
|
||||
throw error(500, 'ไม่สามารถอ่านเนื้อหาหน้าหลักได้');
|
||||
const {content,image} = await res.json()
|
||||
if(!res.ok){
|
||||
console.log("load offline home content instead")
|
||||
const content = (await import('$lib/data/content/home.html?raw')).default
|
||||
info = {content,image:"/images/pattern.png"}
|
||||
}else{
|
||||
info = await res.json()
|
||||
}
|
||||
//throw error(500, 'ไม่สามารถอ่านเนื้อหาหน้าหลักได้');
|
||||
const {content,image} = info
|
||||
//Inner function reduce code and able to throw here
|
||||
async function loadExam(api_path:string,err_msg:string){
|
||||
const res = await fetch(api_path)
|
||||
if(!res.ok)
|
||||
if(!res.ok){
|
||||
throw error(500, err_msg);
|
||||
}
|
||||
const exams:Exam[] = await res.json()
|
||||
exams.forEach((e)=>{
|
||||
e.date = e.start? dayjs(e.start).format("DD MMM BBBB"):""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue