#[Closed] Nextjs middleware (edge runtime) compatible with node-appwrite sdk?

20 messages · Page 1 of 1 (latest)

magic lance
#

Why are you using an older node-appwrite? What version is your appwrite instance? Are you using cloud? If so you should update to "node-appwrite": "^14.1.0", since that supports the newer 1.6 appwrite

if you don't know which version of appwrite you are using, check the lower left on the appwrite webpage

sterile vine
magic lance
#

middleware is intercepting server side, so it should work

sterile vine
magic lance
#

I do not have that problem in my middleware with node-appwrite, but perhaps it is because I am using a server action to get the user to validate permission (Using Sessions).

In either case, my middleware does not directly depend on appwrite, but uses an action to get the data which is wrapped in an User domain object.

sterile vine
sterile vine
# magic lance I do not have that problem in my middleware with node-appwrite, but perhaps it i...

Just for your information from the docs "Server Actions inherit the runtime from the page or layout they are used on." I changed my getUser function to a server action with the "use server" directive and during build the warnings disappeared. Im still not sure about the runtime of that server action in that case but seems to work. Most likely they just will be treated as seperate endpoints and run in the Node environment.

near thorn
#

Warnings should be fine. It's not run in the edge runtime

sterile vine
# near thorn Warnings should be fine. It's not run in the edge runtime

Are you sure? There is even code in the node-appwrite sdk that seems like it does. Isnt that code written so that the node sdk can also run on the edge?

function getUserAgent() {
  let ua = "AppwriteNodeJSSDK/13.0.0";
  const platform = [];
  if (typeof process !== "undefined") {
    if (typeof process.platform === "string")
      platform.push(process.platform);
    if (typeof process.arch === "string")
      platform.push(process.arch);
  }
  if (platform.length > 0) {
    ua += ` (${platform.join("; ")})`;
  }
  if (typeof navigator !== "undefined" && typeof navigator.userAgent === "string") {
    ua += ` ${navigator.userAgent}`;
  } else if (typeof globalThis.EdgeRuntime === "string") {
    ua += ` EdgeRuntime`;
  } else if (typeof process !== "undefined" && typeof process.version === "string") {
    ua += ` Node.js/${process.version}`;
  }
  return ua;
}
near thorn
near thorn
magic lance
#

I was mistaken, server action in middleware did not work well.

Everything worked locally (of course it does), but I get the same error as you after building it into a docker image during deployment.

After further research, it seem that middleware and "edge" deployment is a pain in the ass for many Next.js users, and that it might be better to simply throw away middleware and use the layout for access control.

Noted this down in https://discord.com/channels/564160730845151244/1289081479959416852

sterile vine
sterile vine
# magic lance I was mistaken, server action in middleware did not work well. Everything work...

Hey Zynth, from my understanding right now the node-appwrite sdk runs on the edge and has even checks for the Node APIs that arent supported on the edge. When I tested it hosted on vercel it seems to work. When doing auth in a layout for access control you should be aware of that here, so i wouldnt recommend auth in layout:

https://www.youtube.com/watch?v=kbCzZzXTjuw

Add auth to your Next.js app quickly: https://bit.ly/3QOe1Bh (paid sponsorship).
Disclaimer: this is a sponsored video (paid). It's your responsibility to evaluate safety, accuracy and other relevant parts of the reviewed product.
NEW - React & Next.js Course: https://bytegrad.com/courses/professional-react-nextjs

Hi, I'm Wesley. I'm a brand am...

▶ Play video
sterile vine
#

Nextjs middleware (edge runtime) compatible with node-appwrite sdk?

magic lance
sterile vine
#

They had the problem back then that the node sdk wasnt working in the middleware because of edge environment and used a custom workaround via native fetch api which solved the problem for them. Here https://github.com/appwrite/sdk-generator/pull/799 it seems they fixed it and added polyfills for the sdk, so thatswhy it seems to be working now when used in middleware which runs on the edge environment.

GitHub

Migrates node-appwrite SDK to TypeScript.
Removes all dependencies on node modules from main entrypoint.
Adds optional entrypoint conditional import for InputFile module that depends on fs.
Enable...

sterile vine
#

TLDR: Is it possible to use the node-appwrite sdk inside the nextjs middleware which runs on the edge environment?
From what i found, it seems to work, dont take my word for it and test for yourselves. Post closed.

Disclaimer:

The information provided in this post is for general informational purposes only. I do not guarantee the accuracy, completeness, or reliability of the information presented. Any action you take based on the information provided in this post is strictly at your own risk, and I will not be held liable for any losses or damages resulting from the use or reliance on this information. Please ensure you verify the details before making any decisions based on this post.