From b2b56480b8136ca1cd82368283a25ae0ef60c873 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Tue, 5 Sep 2023 19:03:59 +0100 Subject: [PATCH] Created JS function to fetch static JSON. Now loads data Asynchronously :) --- rollup.config.js | 10 ++++++++++ src/lib/data.ts | 5 +++++ src/main.svelte | 18 ++++++++++++------ src/routes/+layout.svelte | 1 - 4 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 rollup.config.js create mode 100644 src/lib/data.ts diff --git a/rollup.config.js b/rollup.config.js new file mode 100644 index 0000000..f9251a1 --- /dev/null +++ b/rollup.config.js @@ -0,0 +1,10 @@ +import json from '@rollup/plugin-json'; + +export default { + input: 'src/index.js', + output: { + dir: 'output', + format: 'cjs' + }, + plugins: [json()] +}; \ No newline at end of file diff --git a/src/lib/data.ts b/src/lib/data.ts new file mode 100644 index 0000000..38ef0c9 --- /dev/null +++ b/src/lib/data.ts @@ -0,0 +1,5 @@ +export async function getJson(path: string) { + let response = await fetch(path); + let users = await response.json(); + return users; +} \ No newline at end of file diff --git a/src/main.svelte b/src/main.svelte index 2d3dcbd..9e23912 100644 --- a/src/main.svelte +++ b/src/main.svelte @@ -1,5 +1,5 @@
-

{info.name}

+ {#await getJson('/json/me.json')} +

Loading...

+ {:then info} +

{info.name}

-
- {info.name}'s Profile Photo -

{info.about}

-
+
+ {info.name}'s Profile Photo +

{info.about}

+
+ {:catch error} +

{error.message}

+ {/await}
\ No newline at end of file diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 5bb793c..b7a4abf 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -1,5 +1,4 @@