Sapper Tutorial #5 - Server Code vs Client Code

Net Ninja · Beginner ·🌐 Frontend Engineering ·5y ago

Key Takeaways

Compares server code and client code in a Sapper project using Svelte

Full Transcript

all right there so in this lesson i just want to take a few minutes to talk about the way sapper runs component javascript so if you've used svelt before you know that each component can have at the top a script tag which is going to be any kind of script for that particular component right now does this script tag run in the browser or on the server when you use felt it's always in the browser but in our case when we're using sapper the answer is potentially both it can run on the server and in the browser remember for the initial request to a website using support the component is pre-rendered on the node server at first so if we entered the website at mysite.com forward slash jobs which is this component right here this index component inside jobs then the jobs route component is pre-rendered on the server first right and the resulting html and javascript bundle for the rest of the app is sent to us in the browser so initially for that first request this script right here would always run on the server to help pre-render the component template and inject any dynamic data into it thereafter though when we click on links about the site and go to different pages all of the rendering of the new route components like this is done in the browser like a typical spa a typical single page application so at that point the script tags all run in the browser all these script tags right and not on the server so it's entirely true that the scripts can run on the server and in the browser the first request on the server and thereafter only ever in the browser so let's see this in action so what i'm going to do is just at the top in the script is a simple console log to say what page we're on and in this case it's the jobs one and i'm going to copy this script and i'm going to save this and paste it in every other route component we have so inside about as well let me paste this at the top and open that back up change this to about and then let's go into the contact one again let's go to the top and paste this in and this is going to be contact instead and then finally let's go to the home page and at the very top we'll paste this in again and change this to home okay so if it runs in the browser then it's always going to be logged over here right and you can see that right here on the about page but if it's logged on the server if this script runs on the server then this is the server log down here and we can actually see that as well over here now what i'm going to do is just go to a few different pages right and notice they're all being logged in the console over here but they're not being logged again down here they only logged initially when the server restarted when we saved this project the server restarted and that request was made fresh from the browser automatically that's the only time they run because that was the first request to the server since it restarted for this web page now then if i was to refresh so send a fresh request to the server from this page over here then we should see it logged over here what page we're on or what page we're refreshing so let me do that we see in the browser as well it always happens in the browser when it gets here but we should now see it on the server it runs in both in this case for that first request but again if we go to other pages then we don't see those logged on the server it was just that initial request where this ran on the server let's try it again with a different page i'm going to go to contact and refresh to send a fresh new request to the server and when we come down here we can see now contact is logged in the server terminal and also when we move around we get them here but we don't get them here just that initial request for the contact page so this is something you need to constantly keep in mind when you're writing your code inside these scripts because a user could enter your site from any entry url you have any kind of route you've set up therefore potentially any component can be initially pre-rendered on the server and all the javascript code in that component is going to be run on the server now the only exception to this is if you place your code inside a couple of different lifecycle hooks like on mounts so code inside a hook like that is never going to run on the server because the hook is part of the component life cycle in the browser so it's only ever going to run the code inside the browser let me demo this let me first of all up here import something so i'm going to say import and we're going to import the on mount hook right and that is going to be from svelte all right so this on mount hook fires a function when the component has mounted the dom and that can't happen on the server so it's never going to trigger on the server it's only going to trigger in the browser so if i say for example on mount right here and then passing a function to fire when we've mounted the dom and in here i can say safe for browser only code eg fetch down here i could say console.log only ever runs in the browser okay so this code is never actually going to run on the server and i can demo that if we save this and this is the index page if we come over here now and go to the home page then refresh to send a fresh request we get this logged over here but it's not going to be logged over here only this bit is which is this lock so this will never run on the server okay so then what does all this mean in practical terms well it means since any of this code outside of on mount right here could potentially run on the server we need to be careful about the code that we write for example we couldn't use the window object or method such as fetch because those apis don't exist on the server and that will fail so let me demo this let me try and fetch something i'm just going to pass in an endpoint from jason placeholder right here okay if i save this now and try to send a request to the home page and it says over here fetch is not defined so fetch isn't defined on the server we can't use it on the server all right but however if i place that inside on mounts this will work because right here we're waiting for the component to mount to the dom before we fire this so therefore it has to be in the browser and we don't get an error anymore okay when we refresh there's no error and yeah we don't get any results because we're not doing anything with this fetch but either way it's safe to run this fetch right here so always be mindful that your code could be running on the server and in the browser as well now there is another type of special script that we can add to components which always runs before this script right here and we use that to do things like preload data so we're going to talk about that in the next video

Original Description

🐱‍👤🐱‍👤 JOIN THE GANG - https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join ---------------------------------------- 🐱‍💻 🐱‍💻 My Udemy Courses: + Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript + Vue JS & Firebase - http://www.thenetninja.co.uk/udemy/vue-and-firebase + D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase 🐱‍💻 🐱‍💻 Course Files: https://github.com/iamshaunjp/sapper-tutorial 🐱‍💻 🐱‍💻 Other Related Free Courses & Links: + Svelte Tutorial - https://www.youtube.com/playlist?list=PL4cUxeGkcC9hlbrVO_2QFVqVPhlZmz7tO + Get VS Code - https://code.visualstudio.com/ + JWT Debugger - https://jwt.io/ 🐱‍💻 🐱‍💻 Social Links: Facebook - https://www.facebook.com/thenetninjauk Twitter - https://twitter.com/thenetninjauk Instagram - https://www.instagram.com/thenetninja/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Net Ninja · Net Ninja · 0 of 60

← Previous Next →
1 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related AI Lessons

Had my Frontend Developer interview with Capgemini (Application Developer) today, and I wanted to…
Prepare for a frontend developer interview with Capgemini by reviewing JavaScript fundamentals and practicing common interview questions
Medium · JavaScript
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with 10 essential tools for modern web app development
Medium · Programming
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with top 10 developer tools in 2026
Medium · JavaScript
The US Frontend Engineer Market in 2026: A Data-Driven Reality Check (and the Bias That Stops Us Seeing It)
US frontend engineer hiring demand peaked in 2022 and remains flat-depressed in 2026, contrary to common assumptions
Dev.to AI
Up next
The masks we wear | Zora Krstić | TEDxLuxembourgCity
TEDx Talks
Watch →