Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/viewer/src/lib/texture-reference.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { describe, expect, test } from 'bun:test'
import * as THREE from 'three'
import { getPascalTextureRef, stampPascalTextureRef } from './texture-reference'

// The module reads the storage origin lazily on first use, so setting the env
// here (before any stamp call) pins it for the whole test file.
// The module reads the storage origin from the env, so a stamp in another file
// run earlier in the process does not decide it for this one.
process.env.NEXT_PUBLIC_SUPABASE_URL ??= 'https://test-storage.supabase.co'
const STORAGE_ORIGIN = new URL(process.env.NEXT_PUBLIC_SUPABASE_URL).origin

Expand Down
18 changes: 10 additions & 8 deletions packages/viewer/src/lib/texture-reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,18 @@ const STORAGE_BUCKET_BY_KIND = {
'project-asset': 'project-assets',
} as const

let cachedStorageOrigin: string | null | undefined
// Keyed by the URL it was read from, so a texture stamped before the env is set never pins a
// null origin for the rest of the process.
let cachedStorageOrigin: { url: string | undefined; origin: string | null } | undefined
function pascalStorageOrigin(): string | null {
if (cachedStorageOrigin !== undefined) return cachedStorageOrigin
const url = process.env.NEXT_PUBLIC_SUPABASE_URL
if (cachedStorageOrigin && cachedStorageOrigin.url === url) return cachedStorageOrigin.origin
let origin: string | null = null
try {
const url = process.env.NEXT_PUBLIC_SUPABASE_URL
cachedStorageOrigin = url ? new URL(url).origin : null
} catch {
cachedStorageOrigin = null
}
return cachedStorageOrigin
origin = url ? new URL(url).origin : null
} catch {}
cachedStorageOrigin = { url, origin }
return origin
}

/** Static catalog materials ship in the app's public dir and resolve through
Expand Down
Loading