Skip to content

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

Merged
kodiakhq[bot] merged 20 commits into
canaryfrom
fix/middleware-rsc-check
Sep 18, 2023
Merged

Allow server-only in server targets and client-only in client components targets to be available#55394
kodiakhq[bot] merged 20 commits into
canaryfrom
fix/middleware-rsc-check

Conversation

@huozhi

@huozhi huozhi commented Sep 14, 2023

Copy link
Copy Markdown
Contributor

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

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

Copy link
Copy Markdown

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

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

Copy link
Copy Markdown

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

ijjk commented Sep 14, 2023

Copy link
Copy Markdown
Member

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 force-pushed the fix/middleware-rsc-check branch from 373984e to 7737a0d Compare September 15, 2023 22:34
@huozhi huozhi marked this pull request as ready for review September 16, 2023 00:38
@huozhi huozhi requested review from a team, ijjk, shuding and timneutkens as code owners September 16, 2023 00:38
Comment thread packages/next/src/build/webpack-config.ts Outdated
Comment thread packages/next/src/build/webpack-config.ts Outdated
Comment thread packages/next/src/build/webpack-config.ts
@huozhi huozhi requested a review from ztanner September 16, 2023 23:00

@shuding shuding left a comment

Copy link
Copy Markdown
Member

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!

optimize_barrel_exports: None,
optimize_server_react: None,
disable_checks: false,
bundle_target: String::from("default").into(),

Copy link
Copy Markdown
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
Copy Markdown
Contributor 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
@kodiakhq kodiakhq Bot deleted the fix/middleware-rsc-check branch September 18, 2023 12:27
@barryengineerapart

Copy link
Copy Markdown

@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

huozhi commented Sep 18, 2023

Copy link
Copy Markdown
Contributor Author

@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