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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow server-only in server targets and client-only in client components targets to be available #55394

Merged
merged 20 commits into from Sep 18, 2023

Conversation

huozhi
Copy link
Member

@huozhi huozhi commented Sep 14, 2023

Users want to use server-only to restrict the middleware / app routes / pages api, but now it's failing as we're treating them as different webpack layers, but validating the server-only only with server components layers.

Here we modify the rules a bit to let everyone can use "server-only" for the bundles that targeting server-side.

For next-swc transformer, we introduce the new option bundleType which only has "server" | "client" | "default" 3 values:

    • server for server-side targets, like server components, app routes, pages api, middleware
    • client for client components targets such as client components app pages, or page routes under pages directory.
    • default for environment like jest, we don't validate module graph with swc, replaced the disable_checks introduced #54891.

Refactor a bit webpack-config to adapt to the new rules, after that server-only will be able to used in the server-side targets conventions like middleware and pages/api

Fixes #43700
Fixes #54549
Fixes #52833

Closes NEXT-1616
Closes NEXT-1607
Closes NEXT-1385

Copy link

@orca-security-us orca-security-us bot left a comment

Choose a reason for hiding this comment

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

Orca Security Scan Summary

Status Check Issues by priority
Passed Passed Secrets high 0   medium 1   low 0   info 0 View in Orca

Copy link

@orca-security-us orca-security-us bot left a comment

Choose a reason for hiding this comment

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

Orca Security Scan Summary

Status Check Issues by priority
Passed Passed Secrets high 0   medium 0   low 0   info 0 View in Orca

@ijjk
Copy link
Member

ijjk commented Sep 14, 2023

Tests Passed

@huozhi huozhi changed the title [WIP] Alias server-only and client-only in bundled layers Allow server-only in server targets and client-only in client components targets to be available Sep 15, 2023
@huozhi huozhi marked this pull request as ready for review September 16, 2023 00:38
packages/next/src/build/webpack-config.ts Outdated Show resolved Hide resolved
packages/next/src/build/webpack-config.ts Outdated Show resolved Hide resolved
packages/next/src/build/webpack-config.ts Show resolved Hide resolved
@huozhi huozhi requested a review from ztanner September 16, 2023 23:00
Copy link
Member

@shuding shuding 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 the code refactoring!

@@ -81,7 +81,7 @@ fn test(input: &Path, minify: bool) {
auto_modularize_imports: None,
optimize_barrel_exports: None,
optimize_server_react: None,
disable_checks: false,
bundle_target: String::from("default").into(),
Copy link
Member

Choose a reason for hiding this comment

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

This should be an enum, shouldn't it?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, enum will be better, add a reminder for myself to refactor it later. thanks for suggestion 👍

@kodiakhq kodiakhq bot merged commit ffc0e54 into canary Sep 18, 2023
96 of 101 checks passed
@kodiakhq kodiakhq bot deleted the fix/middleware-rsc-check branch September 18, 2023 12:27
@barryengineerapart
Copy link

@huozhi @timneutkens Just a big thank you from myself... This unblocks us for Next and so we can proceed. In general, how long does it take for canary to be cut into a new version.

However, thank you very much everyone. This has been tested and it works.

@huozhi
Copy link
Member Author

huozhi commented Sep 18, 2023

@barryengineerapart glad to hear it's confirmed working! thanks

ijjk pushed a commit that referenced this pull request Sep 18, 2023
We need to disable the default treat `middleware` and `pages/api` as
server-only, unless users explictly import "server-only" to poison it.

This will avoid the case that when a library is mixing "client-only" API
and shared components API in one bundle, and the shared API is used in
middleware or `pages/api` that might cause error. See the test case
added.

Follow up for #55394
@github-actions github-actions bot added the locked label Oct 3, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
6 participants