Skip to content
This repository has been archived by the owner on Jul 10, 2023. It is now read-only.

server (api) example? #22

Open
YuriGor opened this issue Jul 25, 2022 · 5 comments
Open

server (api) example? #22

YuriGor opened this issue Jul 25, 2022 · 5 comments
Labels
enhancement New feature or request

Comments

@YuriGor
Copy link

YuriGor commented Jul 25, 2022

Hello, thanks for template.
Could you share some example how to import/use wasm in api?
I tried to add /pages/api/test.js:
async loading as you do in context is failing due to fetch issue:

export default async (req, res) => {
  const wasm = await import('wasm')
  await wasm.default();
  res.json('test',wasm.add(3,4));
  res.end();
};
/* TypeError: Only absolute URLs are supported
    at getNodeRequestOptions (/home/gor/Development/test-wasm/node_modules/next/dist/compiled/node-fetch/index.js:1:63538)
*/

direct import also doesn't work:

import {add} from 'wasm';
export default async (req, res) => {
  res.json('test',add(34));
  res.end();
};
/* wasm is not loaded in pkg/wasm.js */
@satelllte
Copy link
Owner

satelllte commented Jul 25, 2022

Hi @YuriGor!
Thanks for visiting this repo and creating the issue.

That's a good point which I wasn't aware when I was creating this repository. My main purpose was to bring it to client side (browser).

If I'll have time in the coming weeks, I'll dig in and prepare some example for server side.

I think the main reason why it doesn't work at this point is because wasm-bingen's target is set to web, ref:

"build:wasm": "wasm-pack build wasm --release --target web",

There are other options available including nodejs, here's the doc: https://rustwasm.github.io/wasm-bindgen/reference/deployment.html

In case you urgently need it on server, that's a good starting point

@satelllte satelllte added the enhancement New feature or request label Jul 25, 2022
@YuriGor
Copy link
Author

YuriGor commented Jul 25, 2022

Thanks a lot!
Here is some of my findings to save you some time.

@YuriGor
Copy link
Author

YuriGor commented Jul 25, 2022

Ok, finally, by using this example
I was able to run api with assembly compiled by wasm-pack with target web.

Main trick was to feed js wrapper produced by wasm-pack with wasm module manually imported in edge api instead of letting him fail to fetch wasm automatically.

It's still not perfect, because works only with edge runtime which has significant limitations.

I'll maybe fork and enhance that example with second wasm prepared by wasm-pack, so people will have more options, because currently wasm+nextjs is still shadow zone.

If you will find a way to do the same with nodejs runtime on backend - would be great. With edge runtime a lot of questions arise about session, db, auth etc.

@satelllte
Copy link
Owner

Thank you @YuriGor.
Sure, feel free to contribute 🛰️

@kachkaev
Copy link

Another example / more findings here: vercel/next.js#29362 (comment)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants