jameslittle230 / stork

🔎 Impossibly fast web search, made for static sites.
https://stork-search.net
Apache License 2.0
2.73k stars 56 forks source link

API: allow using a local `.wasm` file #317

Closed srid closed 2 years ago

srid commented 2 years ago

Problem: The stork.js generated in releases is hardcoded to use https://files.stork-search.net/releases/v1.5.0/stork.wasm

Desired behaviour: I'd like stork.js to use a local stork.wasm file. i.e.: https://example.com/stork.js should use https://example.com/stork.wasm automatically.

Implementation approach: I could pass the wasm URL (/stork.wasm for the above example) explicitly in the register function by having it take it as an argument; register could, in turn, call initialize with that wasm URL, instead of invoking it with empty arguments.

https://github.com/jameslittle230/stork/blob/8550f1fe13c334f16026d53886d3e17ae0193925/js/main.ts#L40-L45

as initialize already implements this:

https://github.com/jameslittle230/stork/blob/8550f1fe13c334f16026d53886d3e17ae0193925/js/main.ts#L14-L15

Alternative approaches: Or can this desired behavior be achieved in a different way?

jameslittle230 commented 2 years ago

I believe Stork already supports this!

You can initialize Stork with a self-hosted WASM file. Then, any subsequent operations will use that WASM binary to operate.

You can read more about this on the Self-hosting documentation page.

Let me know if you have any more questions, or if that documentation page doesn't work for you.

srid commented 2 years ago

Ah, nice! I should have read the documentation first :-) I'll try this out now ...

srid commented 2 years ago

Okay, so it does work as far as the API goes - but it ends up loading both the local and remote wasm:

image
srid commented 2 years ago

Could this be because register is calling initialize() (note: no args), which loads the default wasm URL (the second one in the screenshot above) in addition to the local one that is loaded when I call register? The code I have:

            window.addEventListener('load', function () {
              stork.initialize(baseUrl + '_emanote-static/stork/stork.wasm');
              stork.register(indexName, indexUrl);
            });
jameslittle230 commented 2 years ago

Interesting! Maybe there's a bug.

If you just call initialize(), does it load both WASM files?

What happens if you call the "split" API:

stork.initialize(yourUrl);
stork.downloadIndex(name, url, options);
stork.attach(name);
srid commented 2 years ago

If you just call initialize(), does it load both WASM files?

If I do that, I see it loading the remote WASM twice:

image

The above behaviour was for this code:

            window.addEventListener('load', function () {
              stork.initialize(); // (baseUrl + '_emanote-static/stork/stork.wasm');
              stork.register(indexName, indexUrl);
            });

What happens if you call the "split" API:

It still loads both local and remove wasm:

image
            window.addEventListener('load', function () {
              stork.initialize(baseUrl + '_emanote-static/stork/stork.wasm');
              // stork.register(indexName, indexUrl);
              stork.downloadIndex(indexName, indexUrl, {});
              stork.attach(indexName);
            });
jameslittle230 commented 2 years ago

Hm - I'm having trouble reproducing it. When I call your original code in a Codepen, I'm only seeing one WASM blob load.

Screenshot 2022-09-26T17 33 52@2x

https://codepen.io/littleguy230/pen/poVpyQm

Are you sure this is the only time your code is interacting with Stork? No other calls to initialize() anywhere else?

srid commented 2 years ago

You are right; the issue is not with stork per se. It is to do with some other library I'm using, specifically, https://github.com/patrick-steele-idem/morphdom (used in route switch to replace HTML fast), which https://ema.srid.ca/ (used in Emanote) uses.

I confirmed it by checking the fully statically generated website, which does work per instructions at https://stork-search.net/docs/self-hosting

Sorry for the false alarm.

srid commented 2 years ago

I'm guessing morphdom is double-loading stork.js which somehow loads the default wasm when initialize/register is not called in time?