0
0
Svelteframework~20 mins

Middleware patterns with hooks in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Middleware Mastery in Svelte
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Svelte middleware hook?

Consider this SvelteKit handle hook that modifies the response headers. What will the browser receive as the Content-Type header?

Svelte
export async function handle({ event, resolve }) {
  const response = await resolve(event);
  response.headers.set('Content-Type', 'application/json');
  return response;
}
AThe response will have no 'Content-Type' header.
BThe response will keep the original 'Content-Type' header from the resolved response.
CThe response will have 'Content-Type' set to 'application/json'.
DThe code will throw a runtime error because headers are immutable.
Attempts:
2 left
💡 Hint

Think about whether the response.headers object is mutable and if setting headers after resolve works.

lifecycle
intermediate
2:00remaining
In what order do these SvelteKit hooks run during a request?

Given these hooks in hooks.server.js, what is the correct order they execute when a request comes in?

Svelte
export async function handle({ event, resolve }) {
  console.log('handle start');
  const response = await resolve(event);
  console.log('handle end');
  return response;
}

export async function handleFetch({ request, fetch }) {
  console.log('handleFetch');
  return fetch(request);
}

export async function getSession(event) {
  console.log('getSession');
  return { user: null };
}
A3, 1, 2
B3, 2, 1
C1, 3, 2
D2, 3, 1
Attempts:
2 left
💡 Hint

Remember that handle wraps the whole request, handleFetch runs during fetch calls inside handle, and getSession runs after handle.

🔧 Debug
advanced
2:00remaining
Why does this SvelteKit middleware hook cause a runtime error?

Examine this handle hook code. Why does it throw an error when a request is made?

Svelte
export async function handle({ event, resolve }) {
  const response = resolve(event);
  response.headers.set('x-custom', 'value');
  return response;
}
ABecause <code>handle</code> must return a string, not a response object.
BBecause <code>headers</code> is a read-only property and cannot be set.
CBecause <code>event</code> is missing required properties.
DBecause <code>resolve</code> is not awaited, <code>response</code> is a Promise, so <code>headers</code> is undefined.
Attempts:
2 left
💡 Hint

Check if resolve returns a Promise and if you need to wait for it.

📝 Syntax
advanced
2:00remaining
Which option correctly implements a SvelteKit handle hook that adds a cookie?

Choose the code that correctly adds a cookie named session with value abc123 to the response.

A
export async function handle({ event, resolve }) {
  const response = await resolve(event);
  response.headers['set-cookie'] = 'session=abc123; Path=/; HttpOnly';
  return response;
}
B
export async function handle({ event, resolve }) {
  const response = await resolve(event);
  response.headers.set('set-cookie', 'session=abc123; Path=/; HttpOnly');
  return response;
}
C
export async function handle({ event, resolve }) {
  const response = await resolve(event);
  response.headers.append('set-cookie', 'session=abc123; Path=/; HttpOnly');
  return response;
}
D
export async function handle({ event, resolve }) {
  const response = await resolve(event);
  response.headers.add('set-cookie', 'session=abc123; Path=/; HttpOnly');
  return response;
}
Attempts:
2 left
💡 Hint

Check the correct method to set headers on the response object.

state_output
expert
3:00remaining
What is the final value of event.locals.user after this middleware chain?

Given these two handle hooks in hooks.server.js, what will event.locals.user be when the request reaches the page?

Svelte
export async function handle({ event, resolve }) {
  event.locals.user = 'guest';
  const response = await resolve(event);
  return response;
}

export async function handle({ event, resolve }) {
  if (event.request.headers.get('authorization') === 'Bearer token123') {
    event.locals.user = 'admin';
  }
  return resolve(event);
}
AThe code throws an error due to duplicate handle functions.
B"admin"
Cundefined
D"guest"
Attempts:
2 left
💡 Hint

Consider how SvelteKit handles multiple handle exports in the same file.