This project is work-in-progress, then it can introduce any breaking changes. Please, use it with caution.
Welcome to the SvelteKit Boilerplate! This project provides a solid foundation for building web applications with SvelteKit, along with security features and compatibility with Cloudflare services.
To get started, clone the repository and install the dependencies:
git clone https://github.com/bchainhub/dapp-sveltekit-boilerplate.git
cd dapp-sveltekit-boilerplate
npm install
Start the development server:
npm run dev
Build the project for production:
npm run build
Preview the production build:
npm run preview
Update the site.config.ts
file located in /src
to customize the project settings.
You can customize application settings in wrangler.toml
file:
AUTH_SECRET
: Secret key for authentication.LOGIN_MAX_AGE
: Login expiration time. Default is 24 hours (86400).DB_INIT
: Enable the functionality to initialize the database using token value. Append tk
to the URL to define custom token. (Example: …?tk=yourtoken
).NODE_VERSION
: Node.js version.Environment variables are stored in the .env
file. You can add your own environment variables to this file.
CLOUDFLARE_ACCOUNT_ID
: Cloudflare account ID.CLOUDFLARE_API_TOKEN
: Cloudflare API token.ENABLE_API
: Enable or disable the API.ENABLE_AUTH
: Enable or disable authentication.REG_COREID
: Valid Core ID for WebAuthn provider registration. Default is true.VERIFIED_ONLY
: Enable or disable only verified users. This prevents not KYC verified users to register.VERIFIED_EXPIRATION_DAYS
: Verified expiration time. (Value in days).ENABLE_FILE_ACCESS
: Enable or disable file access.KV_NAME
: Cloudflare KV namespace name.R2_NAME
: Cloudflare R2 namespace name.PASSKEY_DURATION
: Passkey expiration time. Default is 2 minutes (120000).CAPTURE_COUNTRY
: Capture country from CF pages, Netlify, Vercel. If enabled.CAPTURE_CITY
: Capture city from CF pages, Netlify, Vercel. If enabled.Environment variables for database setup:
DB_TYPE
: Database type: D1, PRISMA. Default is no database (empty string).DB_NAME
: Database name.PRISMA_PROVIDER
: Prisma provider.PRISMA_API_KEY
: Prisma API key.Generate the authentication secret key, cleaning token, JWT secret in secure way or using the following command:
openssl rand -base64 33
Api is disabled by default. You can enable it by setting ENABLE_API=true
in the .env
filed. If you don't need the API, you can remove the functions/
directory. It is good practice to disable the API in production environment if you don't need it as well as removing the folder.
Api is using Hono API framework. You can find more information in the Hono API documentation.
Api is versioned and the version is defined in the functions/api/${version}/
folder, where ${version}
is the number of version. It is good practice to divide the versions in the folders.
The project uses TailwindCSS for styling. You can customize the styles by editing the tailwind.config.js
file.
Custom styles and variables are defined in the src/css
directory. You can use customization tool to edit variables. We are using prefix skc
for custom classes.
You can use tool to generate TailwindCSS colors: TailwindCSS Color Generator or TailwindCSS Color Shades.
You can connect your application to Cloudflare D1 by setting the DB_NAME
variable in the .env
file. You can find the D1 namespace in the Cloudflare dashboard and bind it with your DB_NAME
setup.
Make sure the wrangler.toml
file is properly configured with the correct DB binding information.
You can connect your application to Cloudflare KV by setting the KV_NAME
variable in the .env
file. You can find the KV namespace in the Cloudflare dashboard and bind it with your KV_NAME
setup.
Make sure the wrangler.toml
file is properly configured with the correct KV binding information.
You can connect your application to Cloudflare R2 by setting the R2_NAME
variable in the .env
file or dashboard. You can find the R2 namespace in the Cloudflare dashboard and bind it with your R2_NAME
setup.
Make sure the wrangler.toml
file is properly configured with the correct R2 binding information.
Deploying your SvelteKit application on Cloudflare Pages is straightforward.
You can follow Cloudflare dashboard deployment or use Wrangler CLI for deployment.
Follow these steps to get started with Wrangler CLI:
First, ensure that you have the Wrangler CLI installed. If you don't have it installed, you can install it using npm:
npm install -g wrangler
Next, authenticate Wrangler with your Cloudflare account:
wrangler login
In your project root, you have a wrangler.toml
file that contains the configuration for deploying your application.
Customize the configuration to match your project settings.
Default Node.js version is 20 (Iron LTS). You can specify a different version in the package.json
and wrangler.toml
file.
To deploy your application, run:
wrangler pages publish ./build
For more details on configuring and using Wrangler with Cloudflare Pages, visit the official Wrangler documentation.
The project uses Auth.js for authentication together with Passkey & CorePass.
Dependencies:
Before first run of your application you need to:
DB_NAME
in the wrangler.toml
file or dashboard.DB_INIT=yourtoken
in the wrangler.toml
file or dashboard.yoururl.com/db/init?tk=yourtoken
to initialize the seeding.DB_INIT=false
in the wrangler.toml
file or dashboard. This is important to disable db initialization.There is difference between access local environment variables and environment variables in Cloudflare.
Key points:
platform.env
in Cloudflare Workers is not directly accessible to SvelteKit components or server-side logic.import { env } from '$env/dynamic/private';
are provided by the deployment environment's configuration. These could come from services like Vercel, Netlify, or other hosting providers that support environment variable management.wrangler.toml
. Similarly, Cloudflare Workers cannot directly access environment variables set up for a SvelteKit deployment.About Platform Environment Variables:
wrangler.toml
file.PUBLIC_
.We can access local environment variables using the env
function. This function is used to read the environment variables from the .env
file. Any value is returned as string
.
import { env } from '$env/dynamic/private';
const secret = env.SECRET;
This is SvelteKit way of reading the environment variables.
We have helper function genv
to read Cloudflare's environment variables. This function is used to read the environment variables directly from Cloudflare instead of the classic .env
variables. true
and false
(not case sensitive) is returned as boolean value, all other values are returned as string
.
import { genv } from from '$lib/helpers/genv';
const secret = genv(platform).SECRET;
GET /db/init
Err ID | Err Code | Category | Description |
---|---|---|---|
400 | 400.01 | Validation | Invalid request |
Contributions are welcome! For feature requests, bug reports, or questions, please open an issue.
This project is open source and available under the Core License.