Loving SvelteKit! Beginner Question #10760
-
I found myself diving in full speed and it made me realize I may be skipping some of the beautiful aspects of SvelteKit by rushing to make things work as I have in the past with other older ways of doing things. In +layout.server.js at the root of the routes folder, I am writing a bunch of if/else statements to detect if params.exist and respond accordingly. Then a light bulb went off in my head and realized that I should (or maybe I should, that's what I am asking you folks!) be using +page.server.js files inside the route-particular folders and forego any logic being needed in the root level as each routes load function would be loading that which that route needs. Am I on to something here and it's maybe clicking for me? Like I have a home page and a leagues route and a league (no s) route. The home is general welcome, the localhost/leagues route is a "view all leagues" and the /leagues/[league_id] route is the singular league profile page. I was loading +layout.server.js at the root and then doing things like if(params.league_id) { // load he profile league_id stuff // } When I realized I should just put the +page.server.js file inside the [league_id] folder and the load function could just directly load the league_id particular things without needing the logic at that point. Would that be the standard mode of operation with SvelteKit, to always put the server loads inside the routes they are needed for only and not at the root? (I just figured I want to store the league_id information and load other routes with it's store value of the league_id on those subsequent pages, but then realized I could just do /league/[league_id]/weekly_matchups/[match_up_id] and it all started to click!! Before I change my +layout.server.js I just wanted to ask this to the experts. THANKS!!! APOLOGIES for such a rudimentary straight forward question, but I am an old man who hasn't coded since Macromedia Flash with ActionScript 2.0 and getting back into things!!! THANKS FOR such a wonderful thing this SVELTEKIT!!!! I love it all so far. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
You are 100% correct :) That's what these files are used for. The main idea is: put the data as close to the ui/page that needs it. |
Beta Was this translation helpful? Give feedback.
You are 100% correct :) That's what these files are used for.
When you create the route
leages/[league_id]
sveltekit checks if the param exists on it´s own. If so, it runs the load function inside+page.server.ts
in that folder to load the data you need in your+page.svelte
file next to it.The main idea is: put the data as close to the ui/page that needs it.
Global state like authentication should go into the root layout, but even that can be optimized with advanced layouts.
Try to always use the the closest
load
function to your page you are rendering and move the data up towards the root layout when you have to.