spa5k / nextjs_approuter_electron

This is a template for building Electron apps with Next.js App router, SSR and Server Components
https://www.saybackend.com/blog/03-electron-nextjs-ssr
46 stars 4 forks source link

Better compatibility #2

Closed dnnsjsk closed 1 month ago

dnnsjsk commented 1 month ago

Hey,

this boilerplate helped me tremendously with my first exploration into packaging a Next.js app with Electron. However, I always got an error when moving the App to a different location like desktop. (I'm using mac)

I think it has to do with path resolving of modules like sharp, which result in a fatal error if you are using the package in an API route or Server Action. I've done following changes to the setup to fix those issues:

Change webdir path in main.js

const webDir = path.join(app.getAppPath(), "app");

Move extra_files to files:

    "files": [
      "main",
      {
        "from": ".next/standalone",
        "to": "app",
        "filter": [
          "!**/.env",
          "!**/package.json"
        ]
      },
      {
        "from": ".next/static",
        "to": "app/.next/static"
      },
      {
        "from": "public",
        "to": "app/public"
      }
    ]

Unpack next in asarUnpack:

    "asarUnpack": [
      "node_modules/next"
    ],

If you're using sharp:

    "asarUnpack": [
      "node_modules/next",
      "node_modules/@img",
      "node_modules/sharp"
    ],

This also packages the Next export inside the .asar archive.

Maybe this'll help someone else! :-)

spa5k commented 1 month ago

Thanks a lot, I tried packing the Next.js inside asar archive, but it wasn't working well in the past.

If it works nicely, I will update the main branch

spa5k commented 1 month ago

Done, pushed to main.