- Svelte 55.2%
- TypeScript 35.6%
- JavaScript 6.4%
- HTML 2.8%
| assets | ||
| e2e | ||
| src | ||
| static | ||
| .gitignore | ||
| .npmrc | ||
| .prettierignore | ||
| .prettierrc | ||
| package-lock.json | ||
| package.json | ||
| playwright.config.ts | ||
| README.md | ||
| svelte.config.js | ||
| tsconfig.json | ||
| vite.config.ts | ||
| vitest-setup-client.ts | ||
Lab 3: More SvelteKit
Enda Lee 2025
Getting Started
- Download or clone this repository.
- Open the app folder in VS Code
- In a terminal run
npm install - Start the application using
npm run dev
Introduction
This lab will cover the following topics:
-
Server-Side rendering (SSR) vs Client-Side Rendering.
-
Environment variables and keeping secrets safe.
-
Passing parameters via the URL.
-
Handling events.
-
Exercises.
1. Client-side Rendering (CSR) vs Server-side Rendering (SSR)
Client-side
The current product page, http://localhost:5173/product, loads data from https://dummyjson.com/products on the client-side. All of the product data is loaded after the page loads. This can be verified by logging the products after they are loaded in the page load() function: src/routes/product/+page.ts:
After adding the console.log and reloading the page, the array of products received from dummyjson will be displayed in the browser console - proving that the data is loaded client-side:
Server-side
Changing to server-side rendering is easy - rename src/routes/product/+page.ts to src/routes/product/+page.server.ts
This change marks the script as server-side only meaning that it will be executed and used to render the page, before it is sent to the browser - Server-side Rendering (SSR).
Before testing the page again, there are a few errors to fix. You should see some keywords underlined red, and the filename also in red:
The errors are displayed as PageLoad only works client-side. The error can be resolved by using PageServerLoad instead.
After saving the file, reload the application and check he console for log messages. The page should load as before but the browser console will not display the log message as before. Instead, you will find that the message has been logged on the server-side - check the server terminal in VS Code:
Also check the Network tab in the browser developer tools and compare with client-side loading.
Server and Client Product loading
In this example, the the initial data will be loaded server-side but updates loaded client-side. A user case for this might be filtering products, for example if a user wanted to see products from just one category.
The products page includes a menu on the left side with some placeholders. We will use these to filter products by category. The dummyjson docs show how to request data by category, see: https://dummyjson.com/docs/products#products-category.
1. Modify product/+page.svelte
This page always runs client-side in the browser. The changes are as follows:
-
declare the variable
productsand inititialise it withdata.productsfrom theload()function when it executes server-side. -
Add a new function
getProducts().-
This function accepts a parameter,
catNamewith a default value (this will be useful later).
-
-
If
catNamehas a value then modifyproductURLso that it requests products from a category matching the value ofcatName -
The function
filterByCat()will be used as an event hander function in the next step.
2. Use a button in the left menu to filter products
The first button will be modified so that when it is clicked, only products from the smartphones category will be displayed. To achieve this, add an onclick={} event hander on the first button in the list and use it to call the filterByCat() function and pass the category name as a parameter.
<button class="list-group-item list-group-item-action" onclick={()=> (filterByCat('smartphones'))}> Smartphones </button>
-
When clicked, the button calls
filterByCat('smartphones') -
Products are retrieved via
getProducts('smartphones') -
The result updates
productsand the page. -
This all happens client-side.
3. Test and check where console.log messages are displayed.
You should see that the initial products load happens server-side but subsequent loads, triggered by the filter, happen client-side and are logged in the browser.
2. Passing Parameters via the URL
Dynamic routes provide a way to pass paramters (values) to a page at runtime in order to generate dynamic content.
In this example, the /about route will be modified so that if the URL includes a name a greeting will be displayed.
For example a route to /about/Bob will display:
These dynamic routes are often refered to as slugs. To try it it:
-
Add a subfolder to the about route, named
[name](note the square brackets are required).If VS Code displays the path as
/about/[name]that can be fixed by disabling Compact Folders in VS Code Preference or Settings. -
Then add
+page.tsand+page.svelteto the new folder so that it looks like this: -
Add code to read the
nameparameter then the page loads inabout/[name]/+page.tsNote passing ofparamsto theload()function. -
Display the greeting in
about/[name]/+page.svelte
3. More event handling
Handling page events is useful as it allows content to be dynamically uopdated depending on events such as clicks, data entry, etc.
This example displays a running total. Each time a value is entered and the button clicked, the new value is added to the running total.
-
Start by adding a new
routenamedrunningtotalto the application including a nav menu entry so that it can be loaded. -
Open
+page.svelteand add code below - read the comments for details
Exercises
-
Show all of the categories in the left menu and attach event handlers to allow filtering by each. Do not hard code!
-
Add an extra button to display all products when it is clicked
-
Use the dummyjson docs, e.g. https://dummyjson.com/docs/products#products-category_list
-
-
Modify the parameter example to accept a second parameter to define the number of times to display the welcome message, for example
/Bob/3should display the Welcome mesage 3 times -
Add functionality to allow the running total in part 3 to be reset (via a button click)
-
Build a simple calculator with the following functionality:
-
Allow the user to enter 2 numbers
-
Allow user to choose Add, Subtract, Multiply, or Divide
-
Validate the inputs
-
Display the input values and result (style appropriatly)
-
-
Add a new route which accepts two values and an operator (add, sub, multi, div). The perform the calculation and display the result.
Enda Lee 2025













