Skip to content

Commit

Permalink
feat(Next.JS): Support server-side Next.JS usage
Browse files Browse the repository at this point in the history
Next.JS disallows relative imports since 12.0.1 (see
https://nextjs.org/docs/messages/middleware-relative-urls for more
information).  So, we need to sniff out if we are in the NEXTJS
environment and if so, use the fallback URL.

This should resolve:
#174

[ Testing: deno task build ]
  • Loading branch information
thaddeusdiamond committed Sep 8, 2023
1 parent 0c8fec0 commit a125e98
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ async function unsafeInstantiate(module: any, url: string) {
});
} catch (_e) {
// This only ever happens during SSR rendering
console.error(_e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3806,12 +3806,25 @@ export function isInstantiated() {
return instanceWithExports != null;
}

function requiresFallbackUrl() {
if (import.meta.url.includes("_frsh")) {
return true;
}
if (globalThis.process?.env?.VERCEL_ENV === 'production') {
return true;
}
if (globalThis.process?.env?.NEXT_RUNTIME === 'nodejs') {
return true;
}
return false;
}

/**
* @param {InstantiateOptions} opts
*/
async function instantiateModule(opts) {
// Temporary exception for fresh framework
const wasmUrl = import.meta.url.includes("_frsh")
const wasmUrl = requiresFallbackUrl()
? opts.url
: new URL("cardano_message_signing_bg.wasm", import.meta.url);
const decompress = opts.decompress;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28591,12 +28591,25 @@ export function isInstantiated() {
return instanceWithExports != null;
}

function requiresFallbackUrl() {
if (import.meta.url.includes("_frsh")) {
return true;
}
if (globalThis.process?.env?.VERCEL_ENV === 'production') {
return true;
}
if (globalThis.process?.env?.NEXT_RUNTIME === 'nodejs') {
return true;
}
return false;
}

/**
* @param {InstantiateOptions} opts
*/
async function instantiateModule(opts) {
// Temporary exception for fresh framework
const wasmUrl = import.meta.url.includes("_frsh")
const wasmUrl = requiresFallbackUrl()
? opts.url
: new URL("cardano_multiplatform_lib_bg.wasm", import.meta.url);
const decompress = opts.decompress;
Expand Down

0 comments on commit a125e98

Please sign in to comment.