···512512 }
513513};
514514515515+export const upsertSiteCache = async (did: string, rkey: string, recordCid: string, fileCids: Record<string, string> = {}) => {
516516+ try {
517517+ await db`
518518+ INSERT INTO site_cache (did, rkey, record_cid, file_cids, cached_at, updated_at)
519519+ VALUES (${did}, ${rkey}, ${recordCid}, ${JSON.stringify(fileCids)}, EXTRACT(EPOCH FROM NOW()), EXTRACT(EPOCH FROM NOW()))
520520+ ON CONFLICT (did, rkey)
521521+ DO UPDATE SET
522522+ record_cid = EXCLUDED.record_cid,
523523+ file_cids = EXCLUDED.file_cids,
524524+ updated_at = EXTRACT(EPOCH FROM NOW())
525525+ `;
526526+ return { success: true };
527527+ } catch (err) {
528528+ console.error('Failed to upsert site cache', err);
529529+ return { success: false, error: err };
530530+ }
531531+};
532532+515533// Get all domains (wisp + custom) mapped to a specific site
516534export const getDomainsBySite = async (did: string, rkey: string) => {
517535 const domains: Array<{
+78-1
apps/main-app/src/routes/wisp.ts
···2222 extractSubfsUris
2323} from '@wispplace/atproto-utils'
2424import { createManifest } from '@wispplace/fs-utils'
2525-import { upsertSite } from '../lib/db'
2525+import { upsertSite, getDomainByDid, updateWispDomainSite, getWispDomainSite, upsertSiteCache } from '../lib/db'
2626import { createLogger } from '@wispplace/observability'
2727// import { validateRecord, type Directory } from '@wispplace/lexicons/types/place/wisp/fs'
2828import { type Directory } from '@wispplace/lexicons/types/place/wisp/fs'
···311311 });
312312313313 await upsertSite(did, rkey, siteName);
314314+315315+ // Cache the empty site for the hosting service
316316+ try {
317317+ await upsertSiteCache(did, rkey, record.data.cid, {});
318318+ } catch (err) {
319319+ // Don't fail the upload if caching fails
320320+ logger.warn('Failed to cache site', err);
321321+ }
322322+323323+ // Auto-map claimed domain to this site if user has a claimed domain with no rkey
324324+ try {
325325+ const existingDomain = await getDomainByDid(did);
326326+ if (existingDomain) {
327327+ const currentSiteMapping = await getWispDomainSite(did);
328328+ if (!currentSiteMapping) {
329329+ // Domain is claimed but not mapped to any site, map it to this new site
330330+ await updateWispDomainSite(existingDomain, rkey);
331331+ logger.info(`Auto-mapped domain ${existingDomain} to new site ${siteName}`);
332332+ }
333333+ }
334334+ } catch (err) {
335335+ // Don't fail the upload if domain mapping fails
336336+ logger.warn('Failed to auto-map domain to new site', err);
337337+ }
314338315339 completeUploadJob(jobId, {
316340 success: true,
···854878 // Store site in database cache
855879 await upsertSite(did, rkey, siteName);
856880881881+ // Cache the site files for the hosting service
882882+ try {
883883+ const fileCids: Record<string, string> = {};
884884+ for (const blob of uploadedBlobs) {
885885+ const normalizedPath = blob.filePath.replace(/^[^\/]*\//, '');
886886+ fileCids[normalizedPath] = blob.result.hash;
887887+ }
888888+ await upsertSiteCache(did, rkey, record.data.cid, fileCids);
889889+ } catch (err) {
890890+ // Don't fail the upload if caching fails
891891+ logger.warn('Failed to cache site files', err);
892892+ }
893893+894894+ // Auto-map claimed domain to this site if user has a claimed domain with no rkey
895895+ try {
896896+ const existingDomain = await getDomainByDid(did);
897897+ if (existingDomain) {
898898+ const currentSiteMapping = await getWispDomainSite(did);
899899+ if (!currentSiteMapping) {
900900+ // Domain is claimed but not mapped to any site, map it to this new site
901901+ await updateWispDomainSite(existingDomain, rkey);
902902+ logger.info(`Auto-mapped domain ${existingDomain} to new site ${siteName}`);
903903+ }
904904+ }
905905+ } catch (err) {
906906+ // Don't fail the upload if domain mapping fails
907907+ logger.warn('Failed to auto-map domain to new site', err);
908908+ }
909909+857910 // Clean up old subfs records if we had any
858911 if (oldSubfsUris.length > 0) {
859912 console.log(`Cleaning up ${oldSubfsUris.length} old subfs records...`);
···10761129 });
1077113010781131 await upsertSite(auth.did, rkey, siteName);
11321132+11331133+ // Cache the empty site for the hosting service
11341134+ try {
11351135+ await upsertSiteCache(auth.did, rkey, record.data.cid, {});
11361136+ } catch (err) {
11371137+ // Don't fail the upload if caching fails
11381138+ logger.warn('Failed to cache site', err);
11391139+ }
11401140+11411141+ // Auto-map claimed domain to this site if user has a claimed domain with no rkey
11421142+ try {
11431143+ const existingDomain = await getDomainByDid(auth.did);
11441144+ if (existingDomain) {
11451145+ const currentSiteMapping = await getWispDomainSite(auth.did);
11461146+ if (!currentSiteMapping) {
11471147+ // Domain is claimed but not mapped to any site, map it to this new site
11481148+ await updateWispDomainSite(existingDomain, rkey);
11491149+ logger.info(`Auto-mapped domain ${existingDomain} to new site ${siteName}`);
11501150+ }
11511151+ }
11521152+ } catch (err) {
11531153+ // Don't fail the upload if domain mapping fails
11541154+ logger.warn('Failed to auto-map domain to new site', err);
11551155+ }
1079115610801157 return {
10811158 success: true,