By the end of this lesson, you will:
:information_source: What is Svelte? Svelte is a UI layer that helps you build user interfaces. However, it has some limitations:
- No built-in router: You can't split your app into different pages
- No server-side rendering (SSR): All code runs only in the browser
- Single-page limitation: Your entire project exists as one big page
SvelteKit is a powerful framework that solves Svelte's limitations. It lets you build full-stack applications with these amazing features:
:bulb: Tip Think of SvelteKit as Svelte's big brother - it has all of Svelte's powers plus extra superpowers for building complete web applications!
To get started with SvelteKit, access this starter project:
https://glitch.com/edit/#!/talented-malleable-sphere?path=src%2Froutes%2Findex.svelte%3A1%3A0
A SvelteKit project has two main folders you need to know:
:memo: Note System Files (Pink in the image)
The pink files and folders contain Node.js packages and SvelteKit settings. You don't need to edit these files as a beginner!
Setting up your project structure:
Create a lib folder inside the src folder
$lib
shortcutThe routes folder is special
index.svelte
-> homepageabout.svelte
-> about pageREADME.md is your project documentation
Want to use Bootstrap's beautiful styles in your SvelteKit project? Here's how to set it up!
Find the app.html
file in your src folder. Add this stylesheet link inside the <head>
tag:
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3"
crossorigin="anonymous" />
Add this script tag before the closing </body>
tag:
<script
src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p"
crossorigin="anonymous">
</script>
After adding these two code snippets, you can use all Bootstrap 5 classes in your Svelte components!
:bulb: Tip Bootstrap Version Note
This tutorial uses Bootstrap 5 (the latest version). If you learned Bootstrap 4 before, don't worry! Most things work the same way, but check the Bootstrap 5 cheat sheet for any differences.
Let's review what you've learned about SvelteKit:
:white_check_mark: Svelte vs SvelteKit
:white_check_mark: Key SvelteKit Features
:white_check_mark: Project Structure
src/
-> Your code lives herestatic/
-> Images and assets go heresrc/lib/
-> Reusable components (create this folder!)src/routes/
-> Your pages:white_check_mark: Bootstrap Integration
app.html
headTry these exercises to reinforce your learning:
Create Your First Route: Add a new file called contact.svelte
in the routes folder. What URL will it create?
Component Challenge: Create a Button.svelte
component in the lib folder. How would you import it using the $lib
shortcut?
Bootstrap Test: After setting up Bootstrap, try adding a Bootstrap button to your index page. Does it work?
Explore the Structure: Look at your project folders. Can you identify which files are system files (that you shouldn't edit) and which are your working files?