🐛 Fix model loading on github pages
This commit is contained in:
@@ -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())
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user