woodser / xmr-sample-next

Sample NextJS app using monero-ts
1 stars 2 forks source link

next config standalone output not working #12

Closed 0-don closed 1 month ago

0-don commented 1 month ago

would be nice to make output: "standalone"working to decrease the bundle size in docker images in next.config.mjs

/** @type {import('next').NextConfig} */
const nextConfig = {
  output: "standalone",
  webpack: (config, { isServer }) => {
    config.experiments = { ...config.experiments, topLevelAwait: true };

    if (!isServer) {
      config.resolve.alias.fs = "memfs";
      config.resolve.fallback.child_process = false;
    }

    config.externals = [
      ...config.externals,
      ({ request }, callback) => {
        if (/^web-worker$/.test(request)) {
          return callback(null, request);
        }
        callback();
      },
    ];

    return config;
  },
};

export default nextConfig;
0-don commented 1 month ago

I think the issue comes from using app router instead of pages router while calling the api rpc methods

Error: Cannot find module '/home/don/Desktop/xmr-sample-next/.next/dist/src/main/js/common/MoneroWebWorker.js'
Require stack:
- /home/don/Desktop/xmr-sample-next/node_modules/web-worker/cjs/node.js
    at Function._resolveFilename (node:internal/modules/cjs/loader:1225:15)
    at Function._load (node:internal/modules/cjs/loader:1051:27)
    at Module.require (node:internal/modules/cjs/loader:1311:19)
    at require (node:internal/modules/helpers:179:18)
    at workerThread (/home/don/Desktop/xmr-sample-next/node_modules/web-worker/cjs/node.js:189:9)
    at Object.<anonymous> (/home/don/Desktop/xmr-sample-next/node_modules/web-worker/cjs/node.js:67:56)
    at Module._compile (node:internal/modules/cjs/loader:1469:14)
    at Object..js (node:internal/modules/cjs/loader:1548:10)
    at Module.load (node:internal/modules/cjs/loader:1288:32)
    at Function._load (node:internal/modules/cjs/loader:1104:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/home/don/Desktop/xmr-sample-next/node_modules/web-worker/cjs/node.js'
  ]
}

this error doesn't appear when using pages router

woodser commented 1 month ago

Ok, just confirming it's working for you by default on the main branch?

woodser commented 1 month ago

Are these 2 different issues then, one is about enabling standalone output, and one is about using a different router?

0-don commented 1 month ago

its the same issue and I think I got a fix for it, this way standalone works and app router aswell

/** @type {import('next').NextConfig} */
const nextConfig = {
  output: "standalone",
  webpack: (config, { isServer }) => {
    config.experiments = { ...config.experiments, topLevelAwait: true };

    if (!isServer) {
      config.resolve.alias.fs = "memfs";
      config.resolve.fallback.child_process = false;
    }

    config.externals = [
      ...config.externals,
      ({ request }, callback) => {
        if (/^web-worker$/.test(request)) {
          return callback(null, request);
        }
        callback();
      },
    ];

    if (isServer) {
      config.externals.push("monero-ts");
    }

    return config;
  },
};

export default nextConfig;
woodser commented 1 month ago

Ok great, a PR is welcome :)

0-don commented 1 month ago

I will test this further and make one if everything looks fine