SvelteKit is an application framework for building high performance web application using svelte.
npm create svelte@latest learn-sveltekit
cd learn-sveltekit
npm install
npm run dev -- --open
routes
folder within the src
folder/about
. then just create a folder about
. And inside that create +page.svelte to write needed code which shows on about page./about/company
. Do same things for /about
part. then create a folder under about
folder called company
. then in company
folder create +page.svelte
file./items/<itemId>
. Do same things for /items
part. then create a folder under items
folder called [itemId]
. then in [itemId]
folder create +page.svelte
file./docs/feature1/section2/example1
. Here nesting routing can be added frequently. For this svelteKit provide catch all route feature. for the user docs
folder create a folder like [...slug]
. folder name can be anything. but three dot must be maintained. And get it using $page.params.slug
. here user can get the all routes url followed by docs like feature1/section2/example1
.[[optionalParam]]
<script>
import { page } from '$app/stores';
const itemId = $page.params.itemId;
</script>
+layout.svelte
as a sibling of +page.svelte
.(groupName)
. under the folder create what user need to create group layout. that folder doesn't valid in route URL.+page@(auth).svelte
(this for show auth layout) or [email protected]
(this for root).just use <a href="/about">about</a>
to navigate
<script>
import { goto } from '$app/navigation';
const gotoHome = () => {
/**
* @param {String} - path string
* @param {Object} - { replaceState: true | false }
*/
goto('/', { replaceState: true });
};
</script>
<button on:click="{gotoHome}">goto home</button>
$app/navigation
functions:goto
- navigate dynamicallybeforeNavigate
- a callback function which call before navigation initiateafterNavigate
- a callback function which call after coming back to that page by clicking back button of a browser.params
in the src
foldermatch
and export it.[paramKey=fileName]
which need to validate. for example: dynamic folder name is [itemId]
. and the validate file name is integer.js
. So renamed folder name should be [itemId=integer]
In SvelteKit there is builtin system to fetch data. the process is given bellow.
+page.js
in parallel with which page data need to fetch.load
. function body is given bellow.export const load = async (loadEvent) => {
const { fetch } = loadEvent;
const title = 'users list';
const response = await fetch('https://jsonplaceholder.typicode.com/users');
const users = await response.json();
return { title, users };
};
+page.svelte
. calling process is given bellow<script>
export let data;
const users = data.users;
</script>
client side | server side |
---|---|
run in both | only run in server |
// +page.js
export const load = async (loadEvent) => {
/**
* @property{fetch} - fetch function from the loadEvent
* @property{params} - params object from the loadEvent
* @property{url} - url object from the loadEvent
* @property{route} - route object from the loadEvent
*/
const { fetch, params, url, route } = loadEvent;
// call api
};
// +page.server.js
export const load = async (serverLoadEvent) => {
/**
* @property{fetch} - fetch function from the serverLoadEvent
* @property{params} - params object from the serverLoadEvent
* @property{url} - url object from the serverLoadEvent
* @property{route} - route object from the serverLoadEvent
*/
const { fetch, params, url, route } = serverLoadEvent;
// call api
};
load
function:When api fetching gonna fail or manually errors need to handle, then SvelteKit
provide a feature to throw error. By throwing error shows the default error page. Otherwise user need to create a +error.svelte
page as a sibling of data visualizing page. The process is given bellow.
// +page.svelte
export const load = async (loadEvent) => {
const { fetch } = loadEvent;
const response = await fetch(`https://jsonplaceholder.typicode.com/users`);
if (response.statusText === "OK") {
const user = await response.json();
return { user };
}
else {
throw error(404, "User not found");
}
}
For custom error page
<!-- +error.svelte -->
<script>
import {page} from "$app/stores";
</script>
<div>
<h1>
{$page.status}: {$page.error.message}
</h1>
</div>