Static adapter deployment helps you turn your Svelte app into simple files that any web server can serve. It makes your app fast and easy to share without needing a special server.
Static adapter deployment in Svelte
Introduction
Syntax
Svelte
import adapter from '@sveltejs/adapter-static'; export default { kit: { adapter: adapter({ // options here }) } };
The adapter is added in the
svelte.config.js file inside the kit.adapter property.You can pass options like
pages and assets to control output folders.Examples
Svelte
import adapter from '@sveltejs/adapter-static'; export default { kit: { adapter: adapter() } };
build and no fallback page for SPA routing.Svelte
import adapter from '@sveltejs/adapter-static'; export default { kit: { adapter: adapter({ pages: 'build', assets: 'build', fallback: null }) } };
Sample Program
This config sets up the static adapter to output all files into the build folder. It uses index.html as a fallback page for client-side routing, so your app works well on static hosts.
Svelte
import adapter from '@sveltejs/adapter-static'; export default { kit: { adapter: adapter({ pages: 'build', assets: 'build', fallback: 'index.html' }) } };
Important Notes
Static adapter does not support server-side code like API routes.
Use fallback option to enable SPA style routing on static hosts.
Check your hosting service docs for how to upload static files.
Summary
Static adapter turns your Svelte app into simple files for easy hosting.
Configure it in svelte.config.js under kit.adapter.
Great for blogs, portfolios, and sites without backend needs.
Practice
1. What is the main purpose of the static adapter in a SvelteKit project?
easy
Solution
Step 1: Understand what static adapter does
The static adapter converts your SvelteKit app into static files like HTML, CSS, and JS.Step 2: Identify the hosting benefit
These static files can be hosted easily on any static hosting service without needing a backend server.Final Answer:
To generate static files for hosting without a backend server -> Option DQuick Check:
Static adapter = static files for hosting [OK]
Hint: Static adapter means no backend, just static files [OK]
Common Mistakes:
- Confusing static adapter with server-side rendering
- Thinking it connects to databases
- Assuming it adds dynamic real-time features
2. Which code snippet correctly sets up the static adapter in
svelte.config.js?easy
Solution
Step 1: Check import statement
The static adapter must be imported from '@sveltejs/adapter-static' and assigned to a variable.Step 2: Confirm adapter usage
The adapter must be called as a function: adapter() inside the kit.adapter property.Final Answer:
Correct import and adapter() call in svelte.config.js -> Option BQuick Check:
Import + adapter() call = correct setup [OK]
Hint: Import adapter and call it as a function in config [OK]
Common Mistakes:
- Forgetting to call adapter() as a function
- Importing wrong adapter package
- Assigning adapter without calling it
3. Given this
svelte.config.js snippet, what will be the output folder after building?
import adapter from '@sveltejs/adapter-static';
export default {
kit: {
adapter: adapter({
pages: 'build',
assets: 'build',
fallback: null
})
}
};medium
Solution
Step 1: Analyze adapter options
The 'pages' and 'assets' options set the output folder to 'build'.Step 2: Understand fallback null
Fallback null means no SPA fallback page, but static files still output to 'build'.Final Answer:
The build output will be in the 'build' folder containing static files -> Option AQuick Check:
pages and assets = 'build' folder output [OK]
Hint: pages and assets options define output folder [OK]
Common Mistakes:
- Confusing fallback with output folder
- Assuming default output folder is 'public'
- Thinking fallback null prevents output
4. You configured the static adapter but your site shows a 404 error on refresh of a nested route. What is the likely cause?
medium
Solution
Step 1: Understand static hosting limitations
Static hosting cannot handle nested routes on refresh without a fallback page.Step 2: Identify fallback usage
Setting a fallback page (like 'index.html') allows SPA routing to work on refresh.Final Answer:
You forgot to set a fallback page for SPA routing -> Option AQuick Check:
Missing fallback causes 404 on nested route refresh [OK]
Hint: Set fallback page to fix refresh 404 on nested routes [OK]
Common Mistakes:
- Assuming import errors cause 404 on refresh
- Confusing adapter type with routing issues
- Forgetting to build before testing
5. You want to deploy a SvelteKit blog as a static site with client-side routing fallback. Which
svelte.config.js adapter config is correct?hard
Solution
Step 1: Identify fallback for SPA routing
Setting fallback to 'index.html' enables client-side routing fallback for nested routes.Step 2: Confirm output folders
Using 'build' for pages and assets is common and correct for static output.Final Answer:
adapter: adapter({ pages: 'build', assets: 'build', fallback: 'index.html' }) -> Option CQuick Check:
Fallback 'index.html' + build folders = SPA static deploy [OK]
Hint: Use fallback 'index.html' for SPA routing on static deploy [OK]
Common Mistakes:
- Setting fallback to null disables SPA fallback
- Using wrong folder names for output
- Omitting fallback causes refresh 404 errors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
