0
0
Svelteframework~10 mins

Hooks (handle, handleError, handleFetch) in Svelte - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Hooks (handle, handleFetch, handleError)
Request received
handle hook runs
Fetch request?
YeshandleFetch hook runs
Fetch response returned
Route processing
Error occurs?
YeshandleError hook runs
Error handled or rethrown
Response sent back to client
This flow shows how SvelteKit hooks run in order: handle runs first on every request, handleFetch wraps fetch calls, and handleError runs if an error happens.
Execution Sample
Svelte
export const handle = async ({ event, resolve }) => {
  console.log('handle start');
  const response = await resolve(event);
  console.log('handle end');
  return response;
};
This handle hook logs before and after the request is processed.
Execution Table
StepHookActionOutput/Effect
1handleLogs 'handle start'Console: handle start
2handleCalls resolve(event) to process requestRequest processing continues
3handleFetchIntercepts fetch call inside resolveCan modify request or response
4handleFetchReturns fetch responseResponse passed back to resolve
5handleLogs 'handle end'Console: handle end
6handleReturns responseResponse sent to client
7handleErrorIf error thrown during resolveLogs or modifies error response
8handleErrorReturns error response or rethrowsError response sent or propagated
9-No more hooks, request cycle endsRequest complete
💡 Request cycle ends after response is sent or error handled.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
eventRequest infoPassed to resolveSameSameSame
responseundefinedundefinedFetch responseResponse returnedResponse sent
errorundefinedundefinedundefinedSet if errorHandled or rethrown
Key Moments - 3 Insights
Why does handle hook call resolve(event)?
Because resolve(event) continues processing the request and returns the response. Without calling resolve, the request would not proceed (see execution_table step 2).
When does handleFetch run during the request?
handleFetch runs whenever a fetch call happens inside resolve(event), allowing interception of fetch requests (see execution_table steps 3 and 4).
What happens if an error occurs during resolve(event)?
handleError runs to catch and handle the error, allowing logging or custom error responses before sending back to the client (see execution_table steps 7 and 8).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does the handle hook do at step 5?
AIntercepts fetch request
BCalls resolve(event)
CLogs 'handle end'
DHandles an error
💡 Hint
Check the 'Action' column at step 5 in the execution_table.
At which step does handleFetch return the fetch response?
AStep 2
BStep 4
CStep 6
DStep 7
💡 Hint
Look for 'Returns fetch response' in the 'Action' column.
If no error occurs during resolve, which hook does NOT run?
AhandleError
Bhandle
ChandleFetch
Dresolve
💡 Hint
See execution_table steps 7 and 8 about error handling.
Concept Snapshot
SvelteKit hooks run in this order:
1. handle: runs on every request, calls resolve(event) to continue.
2. handleFetch: wraps fetch calls inside resolve for interception.
3. handleError: runs if an error occurs during resolve.
Use handle to modify requests/responses, handleFetch to modify fetch, handleError to catch errors.
Full Transcript
In SvelteKit, hooks let you run code during the request lifecycle. The handle hook runs first on every request and must call resolve(event) to continue processing. Inside resolve, fetch calls trigger handleFetch, which can modify fetch requests or responses. If an error happens during resolve, handleError runs to catch and handle it. This flow ensures you can customize requests, fetches, and errors in one place.