Introduction
Supabase is not just a backend-as-a-service platform; it also provides a powerful hosting solution for your static websites. In this guide, we'll walk through the process of deploying a website using Supabase hosting, from setup to deployment.
Prerequisites
- A Supabase account
- Node.js installed on your local machine
- Basic knowledge of HTML, CSS, and JavaScript
- Your website files ready for deployment
Step 1: Setting Up Supabase
-
Create a new project in Supabase:
- Go to Supabase Dashboard
- Click "New Project"
- Fill in your project details
- Wait for the project to be created
Install Supabase CLI:
npm install -g supabase
- Initialize Supabase in your project:
supabase init
Step 2: Configuring Your Project
- Create a
supabase.json
file in your project root:
{
"public": {
"bucket": "publicbucket"
}
}
- Set up environment variables:
Create a
.env
file with your Supabase credentials:
SUPABASE_URL=your-project-url
SUPABASE_KEY=your-anon-key
Step 3: Preparing Your Files
-
Organize your website files:
- Place all static files (HTML, CSS, JS, images) in a directory
- Ensure your main entry point is
index.html
Create a deployment script (
deploy.js
):
const { createClient } = require('@supabase/supabase-js');
const fs = require('fs');
const path = require('path');
const supabaseUrl = process.env.SUPABASE_URL;
const supabaseKey = process.env.SUPABASE_KEY;
const supabase = createClient(supabaseUrl, supabaseKey);
async function deployFiles() {
const publicDir = path.join(__dirname, 'public');
const files = fs.readdirSync(publicDir);
for (const file of files) {
const filePath = path.join(publicDir, file);
const fileContent = fs.readFileSync(filePath);
const { data, error } = await supabase
.storage
.from('publicbucket')
.upload(file, fileContent, {
contentType: 'auto',
upsert: true
});
if (error) {
console.error(`Error uploading ${file}:`, error);
} else {
console.log(`Successfully uploaded ${file}`);
}
}
}
deployFiles();
Step 4: Deploying Your Website
- Run the deployment script:
node deploy.js
- Access your website: Your website will be available at:
https://[your-project-ref].supabase.co/storage/v1/object/public/publicbucket/index.html
Step 5: Setting Up Custom Domain (Optional)
- Go to your project settings in Supabase
- Navigate to the "Custom Domains" section
- Add your domain and follow the DNS configuration instructions
- Wait for DNS propagation (can take up to 48 hours)
Best Practices
-
File Organization:
- Keep your project structure clean and organized
- Use meaningful file names
- Group related files in directories
-
Performance Optimization:
- Minify your CSS and JavaScript files
- Optimize images before uploading
- Enable gzip compression
-
Security:
- Never commit your
.env
file - Use environment variables for sensitive data
- Set appropriate CORS headers
- Never commit your
-
Version Control:
- Use Git for version control
- Create a
.gitignore
file to exclude sensitive files - Commit your deployment scripts
Troubleshooting
-
Upload Errors:
- Check file permissions
- Verify file sizes (Supabase has limits)
- Ensure correct MIME types
-
Access Issues:
- Verify bucket permissions
- Check CORS settings
- Ensure correct URL structure
-
Performance Issues:
- Check file sizes
- Verify caching headers
- Monitor CDN performance
Conclusion
Supabase hosting provides a simple yet powerful way to deploy your static websites. With its integration with other Supabase services, you can easily add backend functionality to your static sites. The platform's CDN ensures fast delivery worldwide, and the straightforward deployment process makes it an excellent choice for developers of all levels.