-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Getting weird error when using runWithAmplifyServerContext
#12856
Comments
Hello, @jrwpatterson 👋 and sorry to hear you're running into these errors. To make sure this isn't an easy fix first, can you first try deleting your If it doesn't, can you check to see if there are multiple versions of the |
I have started seeing the same issue recently. when using the latest
Deleting the
|
Hi @grasdal looking at the output of Could you share the content of the |
I normally would also would have This is how the full Installing with results in
|
When troubleshooting further the cryptic error in our project seems related to a child component of a client component importing a components tagged with Specifically we had a package that contained mixed client and server named exports in its Old code for a 'useServerAuthentication.tsx' (react hook pattern on server side) 🤷
Anytime this ends up somewhere in the child tree of another
In order to resolve it the line
So now
and now the build error is gone :) |
Thanks for the follow-up. @grasdal So it sounds like when mix-matched client and service-specific code, the underlying bundler of Next.js cannot resolve the correct module to build the bundles, respectively, for client and server. That makes sense. Glad that you were able to locate the cause. Is there anything else we can assist you here? |
I am in the same situation with mixed server and client components in a form action as @grasdal however I'm following the recommended practice of exporting the server context runner from utils (e.g. |
Hey @gavacq everything under Can you try to move the utils out of the |
Hi @HuiSF , I moved utils out of the app directory and the error persists: I understand your troubleshooting approach, but I want to highlight that NextJS does support safe colocation of project files in the |
Hi @gavacq thanks for pointing that out. I did some testing but I couldn't reproduce this error. Here's my set up for testing:
The "use server";
import { cookies } from "next/headers";
import { runWithAmplifyServerContext } from "@/amplifyUtils";
import { fetchAuthSession } from "aws-amplify/auth/server";
export const submitForm = async (formData: FormData) => {
const session = await runWithAmplifyServerContext({
nextServerContext: { cookies },
operation: (contextSpec) => fetchAuthSession(contextSpec),
});
console.log("🚀 ~ SubmitForm ~ session:", session);
};
Could you try to create a minimum app that can reproduce this issue and share with us? |
I want to help with this but I've already transitioned off AmplifyUI and Cognito for unrelated reasons and I doubt I'll have time to reproduce this. I really appreciate your time debugging this, thank you. |
@gavacq we'll close this issue then for now, but let us know if you get time to reproduce this so we can dig deeper. Thanks! |
Getting the same issue, tried switching to require instead of import of @aws-amplify/adapter-nextjs library as suggested here: https://www.sanity.media/p/65a2421194f7a8782500f775-how-i-fixed-aws-crypto-build-error The build error is gone but now I am getting this error while calling the fetchAuthSession method Attempted to get the Amplify Server Context that may have been destroyed. Please suggest if there is any solution |
Hi @bhaveshabuild Could you provide a minimal sample repo that can reproduce this error you saw so I can do some digging? |
@HuiSF For your reference, here is what have tried: What I've done
package.json{
"name": "my-app",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"@aws-amplify/adapter-nextjs": "^1.0.18",
"next": "14.1.1",
"react": "^18",
"react-dom": "^18"
},
"devDependencies": {
"typescript": "^5",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"autoprefixer": "^10.0.1",
"postcss": "^8",
"tailwindcss": "^3.3.0",
"eslint": "^8",
"eslint-config-next": "14.1.1"
}
} Directory
and there is Code
|
|
I've found a solution here worked for me: vercel/next.js#59344 (comment) |
@harryhaibojiang |
Oh my, my apologies missing all the conversation here. Thanks @harryhaibojiang and @sasumasa for digging into this further. Your workaround fully makes sense to me @sasumasa, that creates a clear boundary between the server and client bundles. I will evaluate the workaround posted in the linked Next.js issue, and watch this experimental feature status, might be worth to add it into a troubleshooting section in the documentation. |
@HuiSF
Yes, it seem that creates clear boundary, but Server Actions are executed on the server regardless of whether they are passed from Server Component or imported in Client Component. Wouldn't this mean that both are included in the Server Bundle? This point is what I don't understand yet. |
Hi all, thanks for continually providing information about this issue. I was able to reproduce this build error when I was attempting import a server action implementation into a client side component that uses Looking at the dependencies tree, While @harryhaibojiang 's workaround is viable, I found that with listing entire E.g. /** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
serverComponentsExternalPackages: ['@aws-crypto'],
},
};
export default nextConfig; I will add this information into Amplify public documentation. |
Before opening, please confirm:
JavaScript Framework
Next.js
Amplify APIs
Authentication
Amplify Version
v6
Amplify Categories
auth
Backend
Other
Environment information
Describe the bug
When running this code on the server
When I run the nextjs build script I get the following error
Expected behavior
The application should build
Reproduction steps
add this to a server side function
run next build
Code Snippet
// Put your code below this line.
Log output
aws-exports.js
No response
Manual configuration
No response
Additional configuration
No response
Mobile Device
No response
Mobile Operating System
No response
Mobile Browser
No response
Mobile Browser Version
No response
Additional information and screenshots
No response
The text was updated successfully, but these errors were encountered: