Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nextjs ui throws exception on error ts file #208

Merged

Conversation

Sayvai
Copy link
Contributor

@Sayvai Sayvai commented Oct 24, 2023

fix: Next.js UI throws exception on error.ts file

Occasionally (not all the time), whenever I start up the local frontend full stack Next.js webapp, and then attempt to load the frontend UI after the Next.js servers are up and ready, the browser displays an exception thrown by Next.js.

The exception which is occasionally thrown, throws with the error message Attempted to call getErrorMessage() from the server, but getErrorMessage is on the client. It's not possible to invoke a client function from the server, it can only be rendered as a Component or passed to props of a client component..

馃挕 It seems that the error.ts file from which the getErrorMessage() function is defined conflicts with the reserved Next.js app router keyword for error.tsx components, and so Next.js (mistakenly) assumes error.ts is client component file, and hence why use client was placed at the top of the error.ts file in the first place to overcome the initial error. However, in hindsight this is not the correct solution as the function should be called on the server-side, and so the solution is to instead rename error.ts to errors.ts, which also makes more logical sense, and can be extended with additional error handling functions in future.

This change resolves issue #207.

Related Issue

#207

Description

  • Renamed error.ts to errors.ts to avoid Next.js reserved keyword conflict for Error.tsx boundary client component files.
  • Re-reference imports to the new errors.ts file
  • Removed "use client"; declaration in renamed errors.ts file, to correctly allow the functions defined within to be called on the server.

Type

  • Bug Fix
  • Feature Enhancement
  • Documentation Update
  • Code Refactoring
  • Other (please specify):

Proposed Changes

  • error.ts
  • errors.ts
  • third-party-services.tsx
  • useGlobalStore.ts

Screenshots / Code Snippets (if applicable)

Exception message rendered on the frontend UI of the full stack Next.js web app.

screenshot of exception thrown on browser UI, with the message "Attempted to call getErrorMessage() from the server, but getErrorMessage is on the client. It's not possible to invoke a client function from the server, it can only be rendered as a Component or passed to props of a client component."

A separate and different exception message thrown by Next.js before further refactoring to remove "use client"; declaration on the previously incorrectly named error.ts file.

Next.js excpetion message thrown on a server function, where it incorrectly requested for it to be declared as a client component

How to Test

Please follow steps outlined in issue #207

Checklist

  • The code compiles successfully without any errors or warnings
  • The changes have been tested and verified
  • The documentation has been updated (if applicable)
  • The changes follow the project's coding guidelines and best practices
  • The commit messages are descriptive and follow the project's guidelines
  • All tests (if applicable) pass successfully
  • This pull request has been linked to the related issue (if applicable)

Additional Information

N/A

Occasionally (not all the time), whenever I start up the local frontend full stack Next.js webapp, and then attempt to load the frontend UI after the Next.js servers are up and ready, the browser displays an exception thrown by Next.js.

The exception which is occasionally thrown, throws with the error message `Attempted to call getErrorMessage() from the server, but getErrorMessage is on the client. It's not possible to invoke a client function from the server, it can only be rendered as a Component or passed to props of a client component.`.

馃挕 It seems that the `error.ts` file from which the `getErrorMessage()` function is defined conflicts with the reserved Next.js app router keyword for `error.tsx` components, and so Next.js (mistakenly) assumes `error.ts` is client component file, and hence why `use client` was placed at the top of the `error.ts` file in the first place to overcome the initial error. However, in hindsight this is not the correct solution as the function should be called on the server-side, and so the solution is to instead rename `error.ts` to `errors.ts`, which also makes more logical sense, and can be extended with additional error handling functions in future.

This change resolves issue srbhr#207.
@@ -1,4 +1,3 @@
"use client";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed "use client"; declaration within the now renamed errors.ts file, to correctly allow the functions defined within to be called on the server.

Copy link
Owner

@srbhr srbhr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this @Sayvai

@srbhr srbhr merged commit b77473a into srbhr:main Oct 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants