Minimal boilerplate SvelteKit set-up made deployable to GitHub Pages or static hosting
Use the SvelteKit static adapter to prerender the app.
package.json
"devDependencies": {
+ "@sveltejs/adapter-static": "next",
"@sveltejs/kit": "next",
"svelte": "^3.46.0",
+ "vite": "3.0.0"
}
svelte.config.js
import adapter from "@sveltejs/adapter-static";
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
+ adapter: adapter(),
+ prerender: {
+ default: true,
+ },
},
};
export default config;
paths.base
in the configkit.paths.base
should be your repo URL subpath (see the Vite docs)import adapter from "@sveltejs/adapter-static";
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
adapter: adapter(),
prerender: {
default: true,
},
+ /*
+ // enable this base url for publish in github pages
+ paths: {
+ base: process.env.NODE_ENV === "production" ? "/sveltekit-static-site" : "",
+ },
+ */
},
};
export default config;
Note: You will also need to prepend relative paths with the SvelteKit base
path so that your app can build successfully for production.
<script>
import { base } from '$app/paths';
</script>
<a href="{base}/about">About</a>
.nojekyll
file to the buildThe last step is to add a .nojekyll
file to the build folder to bypass Jekyll on GitHub Pages.
package.json
{
"scripts": {
"dev": "vite dev",
"build": "vite build",
"deploy-gh-pages": "rm -rf docs && vite build && touch build/.nojekyll && cp -r build docs"
}
}
Use degit to quickly scaffold a new project:
npx degit antonielbordin/sveltekit-static-site my-app
cd my-app && npm install
If you want to use static hosting first,
build the application by running npm run build
.
Then just upload the content of your build folder.
Now if you want to host on github pages follow the steps below:
kit: {
+ paths: {
+ base: process.env.NODE_ENV === "production" ? "/sveltekit-static-site" : "",
+ }
}
in the svelte.config.js
file
npm run deploy-gh-pages