Skip to content

'use server' fleshing out #6384

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

Merged
merged 8 commits into from
Oct 30, 2023
Merged

'use server' fleshing out #6384

merged 8 commits into from
Oct 30, 2023

Conversation

lunaleaps
Copy link
Contributor

@lunaleaps lunaleaps commented Oct 26, 2023

@github-actions
Copy link

github-actions bot commented Oct 26, 2023

Size changes

📦 Next.js Bundle Analysis for react-dev

This analysis was generated by the Next.js Bundle Analysis action. 🤖

One Page Changed Size

The following page changed size from the code in this PR compared to its base branch:

Page Size (compressed) First Load
/[[...markdownPath]] 78.9 KB (🟢 -15 B) 182.65 KB
Details

Only the gzipped size is provided here based on an expert tip.

First Load is the size of the global bundle plus the bundle for the individual page. If a user were to show up to your website and land on a given page, the first load size represents the amount of javascript that user would need to download. If next/link is used, subsequent page loads would only need to download that page's bundle (the number in the "Size" column), since the global bundle has already been downloaded.

Any third party scripts you have added directly to your app using the <script> tag are not accounted for in this analysis

Next to the size is how much the size has increased or decreased compared with the base branch of this PR. If this percentage has increased by 10% or more, there will be a red status indicator applied, indicating that special attention should be given to this.

Copy link
Contributor

@elicwhite elicwhite left a comment

Choose a reason for hiding this comment

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

Looks great!

Some comments, but not everything needs to be resolved before landing. Could discuss and make more changes to the big things as needed in a follow up.

To read a server action return value, you'll need to `await` the promise returned.
Copy link
Contributor

Choose a reason for hiding this comment

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

I am pretty sure this is right and reasonable, but I'm curious how to explain why you should be using await here and not using the use function. Is it because we are inside an async function already so it is fine, and also we don't want to make this component suspend while waiting for the server response, you handle that with the pending flag instead of suspense?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm that's a good callout. Yea I don't think use should be used in this callback..but I'm going off intuition. @sophiebits is that right? Would we ever want to call a server action in render? I guess they're always called in transitions..

Edit: Trying out use, I get the following error

Error: Hooks are not supported inside an async component. This error is often caused by accidentally adding `'use client'` to a module that was originally written for the server.

Copy link
Member

Choose a reason for hiding this comment

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

use() is only to unwrap a resource during the render pass (i.e. in a component body or in a setstate updater). Not in nested functions like the imperative one here.

You shouldn't call a server action in render; refactor your code so you don't have to (plus, SSR won't let you).

Copy link
Member

@sophiebits sophiebits left a comment

Choose a reason for hiding this comment

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

there is one big error in this which is that progressive enhancement 100% works with client components! as long as it's a server action on the form, then SSR of the client component will output the right thing

and useFormState is a client hook! it cannot be imported from a server component. the only hooks that can be used in server components are use and useId (and technically useContext which is being removed, useCallback useDebugValue useMemo which are noops)

@lunaleaps
Copy link
Contributor Author

Updated and ready for another review!

@lunaleaps lunaleaps requested a review from sophiebits October 27, 2023 21:10
Copy link
Member

@sophiebits sophiebits left a comment

Choose a reason for hiding this comment

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

looking great!! thanks for the hard work on this

lunaleaps and others added 2 commits October 30, 2023 10:58
Co-authored-by: Sophie Alpert <git@sophiebits.com>
Co-authored-by: Sophie Alpert <git@sophiebits.com>
Copy link
Member

@sophiebits sophiebits left a comment

Choose a reason for hiding this comment

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

both errors mine! sorry :)

Co-authored-by: Sophie Alpert <git@sophiebits.com>
@@ -203,4 +201,15 @@ function LikeButton() {
}
```

```js
// actions.js
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm fleshing out the action as I want to illustrate that you need the directive on the module level

@lunaleaps lunaleaps merged commit 49c2d78 into main Oct 30, 2023
@lunaleaps lunaleaps deleted the use-server branch October 30, 2023 21:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants