🐛 Fix model loading on github pages

This commit is contained in:
Rune Harlyk
2025-10-20 20:17:57 +02:00
parent 72a288145d
commit b14f005b22
2 changed files with 18 additions and 7 deletions
+4 -3
View File
@@ -28,14 +28,15 @@ export const cacheModelFiles = async () => {
const files = uzip.parse(await data.arrayBuffer())
for (const [path, data] of Object.entries(files) as [path: string, data: Uint8Array][]) {
const url = new URL(path, window.location.href)
fileService?.saveFile(url.toString(), data)
const normalizedPath = path.startsWith('/') ? path : '/' + path
const resolvedUrl = resolve(normalizedPath as any)
fileService?.saveFile(resolvedUrl, data)
fileService?.saveFile(normalizedPath, data)
}
}
export const loadModel = async (url: string): Promise<Result<[URDFRobot, string[]], string>> => {
const urdfLoader = new URDFLoader()
urdfLoader.workingPath = resolve('/')
let xml =
url.endsWith('.xacro') ? await loadXacro(url) : await fetch(url).then(res => res.text())
+14 -4
View File
@@ -6,10 +6,20 @@ const registerFetchIntercept = async () => {
const fileService = (await import('$lib/services/file-service')).default
window.fetch = async (resource, config) => {
const url = resource instanceof Request ? resource.url : resource.toString()
const file = await fileService?.getFile(url)
return file?.isOk() && file.inner ?
new Response(new Uint8Array(file.inner))
: originalFetch(resource, config)
let file = await fileService?.getFile(url)
if (file?.isOk() && file.inner) return new Response(new Uint8Array(file.inner))
if (url.startsWith('http')) {
try {
const urlObj = new URL(url)
const pathOnly = urlObj.pathname
file = await fileService?.getFile(pathOnly)
if (file?.isOk() && file.inner) return new Response(new Uint8Array(file.inner))
} catch {}
}
return originalFetch(resource, config)
}
}