Using SSR.
This commit is contained in:
parent
03c3386c0c
commit
e3fbd12366
2 changed files with 37 additions and 20 deletions
31
frontend/src/routes/articles/+page.js
Normal file
31
frontend/src/routes/articles/+page.js
Normal file
|
@ -0,0 +1,31 @@
|
|||
// Server Side Rendering
|
||||
|
||||
export async function load(context) {
|
||||
async function fetchData(data) {
|
||||
try {
|
||||
const resTemp = await context.fetch(`http://0.0.0.0:8000/${data}`);
|
||||
if (resTemp.ok == false) {
|
||||
return {
|
||||
status: resTemp.status,
|
||||
}
|
||||
}
|
||||
return {
|
||||
status: 0, data: await resTemp.json(),
|
||||
}
|
||||
} catch (error) {
|
||||
return {
|
||||
status: 500,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const res = (await fetchData(`get_simple_posts/4`));
|
||||
if (res.status == 500) return {
|
||||
status: res.status
|
||||
}
|
||||
|
||||
return {
|
||||
status: 0,
|
||||
content: res.data,
|
||||
};
|
||||
}
|
|
@ -1,34 +1,20 @@
|
|||
<script>
|
||||
import '$lib/css/base.css';
|
||||
import '$lib/css/article.css';
|
||||
|
||||
import { onMount } from 'svelte';
|
||||
import { load } from '$lib/js/apicall.js';
|
||||
|
||||
import CarouselVertical from '$lib/components/carousel-vertical.svelte';
|
||||
|
||||
$: isLoaded = false;
|
||||
$: hasLoadFailed = false;
|
||||
let post_min; // slug, author, title, date
|
||||
|
||||
onMount(async () => {
|
||||
const loaded_post = await load("get_simple_posts/4");
|
||||
if (loaded_post.status == 200) {
|
||||
post_min = loaded_post.data;
|
||||
isLoaded = true;
|
||||
} else hasLoadFailed = true;
|
||||
});
|
||||
export let data;
|
||||
// Database
|
||||
const article = data.status == 0 ? data : undefined;
|
||||
</script>
|
||||
|
||||
<div>
|
||||
<h1>Etheryo Blog</h1>
|
||||
{#if isLoaded}
|
||||
<CarouselVertical posts={post_min} />
|
||||
{#if data != undefined}
|
||||
<CarouselVertical posts={article.content} />
|
||||
<!-- debug purpose -->
|
||||
<div style="height: 100rem;"></div>
|
||||
{:else if hasLoadFailed}
|
||||
<p>Loading failed :c</p>
|
||||
{:else}
|
||||
<p>Loading...</p>
|
||||
<p>Loading failed :c</p>
|
||||
{/if}
|
||||
</div>
|
||||
|
|
Loading…
Add table
Reference in a new issue