Hello together, I’m struggling now for a couple of days to import/load rhino3dm.js. I’m trying to load rhino3dm into a svelte kit application with dynamic routes. So now copying the .wasm file to all the possible directories seams not feasible option. The best option for me would be, that await rhino3dm() would look for the .wasm file in the route path instead of the current path, but I could not find a way to do that.
I also tried to load rhino3dm from window.script, but there I got now the problem that some functionalities somehow just don’t seam to exist like rhino.CommonObject.decode – I get null…(when trying the same with importing rhino3dm from the node package, it works)
Hi, this is solvable without copying the .wasm into every route. The issue is that rhino3dm.js is an Emscripten module, and by default it resolves rhino3dm.wasm relative to the current document URL. With dynamic routes that “current path” changes, so the relative lookup fails.
The fix is to pass a locateFile function to the module factory and return an absolute URL, so the route depth no longer matters:
Put rhino3dm.wasm in your static/ folder, so it is served at /rhino3dm.wasm (the site root).
Use locateFile: () => '/rhino3dm.wasm'. The leading / resolves from the origin root, not the current route, so /, /a, and /a/b/c all load the same file.
If you would rather not host the file at all, point it at the CDN:
SSR: the wasm cannot be fetched during server-side rendering, so initialize rhino3dm on the client only. In SvelteKit, do it inside onMount, or guard with import { browser } from '$app/environment'.
The window / script-tag (UMD) approach is what is giving you null for things like CommonObject.decode. I would recommend sticking with the npm/ESM import that already works for you. The locateFile option works the same way on the ESM rhino3dm.module.js build, so you do not need the global script tag.
In the end, I used the newish .svelte.ts file acting as a singleton store where I can easily access it. But good to know with the locateFile that helps a lot.