#[Closed] Nextjs middleware (edge runtime) compatible with node-appwrite sdk?
20 messages · Page 1 of 1 (latest)
Im using self hosted appwrite version 1.5.11 which is compatible with node-appwrite sdk version 13.0.0
Also my question is not about the appwrite cloud, my question is if the node-appwrite sdk supports running in the middleware in next js because as per their docs the edge runtime doesnt support some Node.js APIs and as you can see in my build process, it even states that.
middleware is intercepting server side, so it should work
I just hosted it on vercel to test it and put in some console.logs, seems to be working but i have my doubts still, should work is not really an answer, because of the unsupported Node.js APIs
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.
Thank you for your help. So i guess calling an API endpoint/server action from the middleware for session validation is also an option. I will do that in case my current setup will break.
The called endpoint then has full Node API support.
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.
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;
}
FYI, it's best to wrap code in backticks to format a bit nicer. You can use 1 backtick for inline code (https://www.markdownguide.org/basic-syntax/#code) and 3 backticks for multiline code (https://www.markdownguide.org/extended-syntax/#syntax-highlighting).
yes, it checks if process is defined and only uses process.platform or process.version if process is defined (which would not be defined in edge runtimes)
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
So that means the sdk runs on the edge when called directly in the middleware.
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:
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...
Nextjs middleware (edge runtime) compatible with node-appwrite sdk?
Thanks for sharing this - I did not realize the layout.tsx only runs once, which means if I wanted to authenticate pages the "old fashioned" way, I would have to add that on each page manually 👍
I will keep playing around with middleware. I realize I am fighting several Next.js related battles at once 😅
Hey Zynth, i found a discord posts for my question here
https://discord.com/channels/564160730845151244/1220084607953797140
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.
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.