0
0
Svelteframework~20 mins

Redirect from actions in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Redirect Mastery in SvelteKit
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens after a successful redirect in a SvelteKit action?

Consider a SvelteKit form action that returns a redirect. What will the user see immediately after the form submission?

Svelte
import { redirect } from '@sveltejs/kit';

export const actions = {
  default: async ({ request }) => {
    // some processing
    throw redirect(303, '/success');
  }
};
AThe form data is displayed on the same page without navigation.
BAn error message is shown because redirect is not supported.
CThe page reloads but stays on the current URL.
DThe browser navigates to the '/success' page immediately.
Attempts:
2 left
💡 Hint

Think about what a redirect response does in a web app.

📝 Syntax
intermediate
2:00remaining
Which is the correct way to return a redirect from a SvelteKit action?

Choose the valid syntax to redirect the user to '/dashboard' after an action completes.

Areturn { redirect: '/dashboard' };
Bthrow redirect(303, '/dashboard');
Creturn { status: 303, redirect: '/dashboard' };
Dreturn { status: 303, headers: { Location: '/dashboard' } };
Attempts:
2 left
💡 Hint

Check the SvelteKit documentation for the redirect helper function.

🔧 Debug
advanced
2:00remaining
Why does this SvelteKit action not redirect as expected?

Given this action code, the user is not redirected after form submission. What is the cause?

Svelte
import { redirect } from '@sveltejs/kit';

export const actions = {
  default: async ({ request }) => {
    // some logic
    throw redirect(303, '/home');
  }
};
AThe action should throw the redirect instead of returning it.
BThe redirect URL must be absolute, not relative.
CThe redirect function is not imported correctly.
DThe action must return a status code with the redirect.
Attempts:
2 left
💡 Hint

Think about how SvelteKit expects redirects to be handled in actions.

state_output
advanced
2:00remaining
What is the state of the page after throwing a redirect in an action?

When an action throws redirect(303, '/profile'), what happens to the current page's state and data?

AThe action returns data to update the current page without navigation.
BThe current page state is preserved and the URL changes to '/profile'.
CThe current page state is discarded and the browser navigates to '/profile'.
DThe page reloads but stays on the same URL with updated data.
Attempts:
2 left
💡 Hint

Consider what a redirect response does in HTTP.

🧠 Conceptual
expert
3:00remaining
Why use throw redirect() instead of return in SvelteKit actions?

Explain why SvelteKit requires throwing redirect() in actions instead of returning an object with redirect info.

AThrowing interrupts the action and sends a special HTTP response to the browser immediately.
BReturning redirect info would cause the action to run twice, causing errors.
CThrowing redirect() allows SvelteKit to cache the redirect response for performance.
DReturning redirect info is deprecated and no longer supported in SvelteKit.
Attempts:
2 left
💡 Hint

Think about how exceptions control flow in JavaScript and HTTP responses.