mirror of
https://github.com/stackblitz-labs/bolt.diy
synced 2025-06-26 18:26:38 +00:00
Fix starter template import failure by adding required User-Agent headers
- Add User-Agent header to GitHub API requests (required by GitHub) - Fix Cloudflare Workers environment context handling - Add comprehensive logging for template import debugging - Improve error handling with detailed error messages - Verify fix works for all 13 starter templates Resolves template import 500 errors where users saw "Failed to import starter template" 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
72ae0bf3b7
commit
6156b84019
@ -1,7 +1,7 @@
|
||||
import { json } from '@remix-run/cloudflare';
|
||||
import JSZip from 'jszip';
|
||||
|
||||
export async function loader({ request }: { request: Request }) {
|
||||
export async function loader({ request, context }: { request: Request; context?: any }) {
|
||||
const url = new URL(request.url);
|
||||
const repo = url.searchParams.get('repo');
|
||||
|
||||
@ -12,39 +12,60 @@ export async function loader({ request }: { request: Request }) {
|
||||
try {
|
||||
const baseUrl = 'https://api.github.com';
|
||||
|
||||
// Get GitHub token from Cloudflare context env or fallback to process.env
|
||||
const env = context?.cloudflare?.env || context?.env || process.env || {};
|
||||
const githubToken = env.GITHUB_TOKEN;
|
||||
|
||||
console.log(`Processing GitHub template for repo: ${repo}`);
|
||||
|
||||
// Get the latest release
|
||||
const releaseResponse = await fetch(`${baseUrl}/repos/${repo}/releases/latest`, {
|
||||
headers: {
|
||||
Accept: 'application/vnd.github.v3+json',
|
||||
|
||||
// Add GitHub token if available in environment variables
|
||||
...(process.env.GITHUB_TOKEN ? { Authorization: `Bearer ${process.env.GITHUB_TOKEN}` } : {}),
|
||||
'User-Agent': 'Buildify-App/1.0',
|
||||
// Add GitHub token if available
|
||||
...(githubToken ? { Authorization: `Bearer ${githubToken}` } : {}),
|
||||
},
|
||||
});
|
||||
|
||||
if (!releaseResponse.ok) {
|
||||
throw new Error(`GitHub API error: ${releaseResponse.status}`);
|
||||
const errorText = await releaseResponse.text();
|
||||
console.error(`GitHub API error for repo ${repo}: ${releaseResponse.status} ${releaseResponse.statusText}`);
|
||||
console.error(`Error details: ${errorText}`);
|
||||
throw new Error(`GitHub API error: ${releaseResponse.status} - ${errorText}`);
|
||||
}
|
||||
|
||||
const releaseData = (await releaseResponse.json()) as any;
|
||||
const zipballUrl = releaseData.zipball_url;
|
||||
|
||||
if (!zipballUrl) {
|
||||
throw new Error('No zipball URL found in release data');
|
||||
}
|
||||
|
||||
console.log(`Fetching zipball from: ${zipballUrl}`);
|
||||
|
||||
// Fetch the zipball
|
||||
const zipResponse = await fetch(zipballUrl, {
|
||||
headers: {
|
||||
...(process.env.GITHUB_TOKEN ? { Authorization: `Bearer ${process.env.GITHUB_TOKEN}` } : {}),
|
||||
'User-Agent': 'Buildify-App/1.0',
|
||||
...(githubToken ? { Authorization: `Bearer ${githubToken}` } : {}),
|
||||
},
|
||||
});
|
||||
|
||||
if (!zipResponse.ok) {
|
||||
throw new Error(`Failed to fetch release zipball: ${zipResponse.status}`);
|
||||
const errorText = await zipResponse.text();
|
||||
console.error(`Failed to fetch zipball: ${zipResponse.status} ${zipResponse.statusText}`);
|
||||
console.error(`Error details: ${errorText}`);
|
||||
throw new Error(`Failed to fetch release zipball: ${zipResponse.status} - ${errorText}`);
|
||||
}
|
||||
|
||||
// Get the zip content as ArrayBuffer
|
||||
const zipArrayBuffer = await zipResponse.arrayBuffer();
|
||||
console.log(`Zip ArrayBuffer size: ${zipArrayBuffer.byteLength} bytes`);
|
||||
|
||||
// Use JSZip to extract the contents
|
||||
const zip = await JSZip.loadAsync(zipArrayBuffer);
|
||||
console.log('JSZip loaded successfully');
|
||||
|
||||
// Find the root folder name
|
||||
let rootFolderName = '';
|
||||
@ -88,6 +109,8 @@ export async function loader({ request }: { request: Request }) {
|
||||
const results = await Promise.all(promises);
|
||||
const fileList = results.filter(Boolean) as { name: string; path: string; content: string }[];
|
||||
|
||||
console.log(`Successfully extracted ${fileList.length} files from ${repo}`);
|
||||
|
||||
return json(fileList);
|
||||
} catch (error) {
|
||||
console.error('Error processing GitHub template:', error);
|
||||
|
Loading…
Reference in New Issue
Block a user