Skip to content

Commit

Permalink
Support crossOrigin in Turbopack (#61461)
Browse files Browse the repository at this point in the history
## What?

Implements `crossOrigin` in the React Client Components manifest for
Turbopack. This feature was missing.

Removes the `false` value as it doesn't actually do anything, the
default is no crossorigin property.

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->


Closes NEXT-2313
  • Loading branch information
timneutkens authored Jan 31, 2024
1 parent d6badf8 commit 724c446
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 18 deletions.
5 changes: 1 addition & 4 deletions packages/next-swc/crates/next-api/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -705,10 +705,7 @@ impl AppEndpoint {
client_references_chunks,
this.app_project.project().client_chunking_context(),
ssr_chunking_context,
this.app_project
.project()
.next_config()
.computed_asset_prefix(),
this.app_project.project().next_config(),
runtime,
);
server_assets.push(entry_manifest);
Expand Down
9 changes: 8 additions & 1 deletion packages/next-swc/crates/next-core/src/next_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub struct NextConfig {
pub skip_middleware_url_normalize: Option<bool>,
pub skip_trailing_slash_redirect: Option<bool>,
pub i18n: Option<I18NConfig>,
pub cross_origin: Option<String>,
pub cross_origin: Option<CrossOriginConfig>,
pub dev_indicators: Option<DevIndicatorsConfig>,
pub output: Option<OutputType>,
pub analytics_id: Option<String>,
Expand Down Expand Up @@ -140,6 +140,13 @@ pub struct NextConfig {
webpack: Option<serde_json::Value>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, TraceRawVcs)]
#[serde(rename_all = "kebab-case")]
pub enum CrossOriginConfig {
Anonymous,
UseCredentials,
}

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, TraceRawVcs)]
#[serde(rename_all = "camelCase")]
struct AmpConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use super::{ClientReferenceManifest, ManifestNode, ManifestNodeEntry, ModuleId};
use crate::{
next_app::ClientReferencesChunks,
next_client_reference::{ClientReferenceGraphResult, ClientReferenceType},
next_config::NextConfig,
util::NextRuntime,
};

Expand All @@ -30,16 +31,22 @@ impl ClientReferenceManifest {
client_references_chunks: Vc<ClientReferencesChunks>,
client_chunking_context: Vc<Box<dyn EcmascriptChunkingContext>>,
ssr_chunking_context: Vc<Box<dyn EcmascriptChunkingContext>>,
asset_prefix: Vc<Option<String>>,
next_config: Vc<NextConfig>,
runtime: NextRuntime,
) -> Result<Vc<Box<dyn OutputAsset>>> {
let mut entry_manifest: ClientReferenceManifest = Default::default();
entry_manifest.module_loading.prefix = asset_prefix
entry_manifest.module_loading.prefix = next_config
.computed_asset_prefix()
.await?
.as_ref()
.map(|p| p.to_owned())
.unwrap_or_default();
entry_manifest.module_loading.cross_origin = None;

entry_manifest.module_loading.cross_origin = next_config
.await?
.cross_origin
.as_ref()
.map(|p| p.to_owned());
let client_references_chunks = client_references_chunks.await?;
let client_relative_path = client_relative_path.await?;
let node_root_ref = node_root.await?;
Expand Down
4 changes: 2 additions & 2 deletions packages/next-swc/crates/next-core/src/next_manifests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use indexmap::IndexSet;
use serde::{Deserialize, Serialize};
use turbo_tasks::{trace::TraceRawVcs, TaskInput};

use crate::next_config::Rewrites;
use crate::next_config::{CrossOriginConfig, Rewrites};

#[derive(Serialize, Default, Debug)]
pub struct PagesManifest {
Expand Down Expand Up @@ -247,7 +247,7 @@ pub struct ClientReferenceManifest {
#[serde(rename_all = "camelCase")]
pub struct ModuleLoading {
pub prefix: String,
pub cross_origin: Option<String>,
pub cross_origin: Option<CrossOriginConfig>,
}

#[derive(Serialize, Default, Debug, Clone)]
Expand Down
6 changes: 1 addition & 5 deletions packages/next/src/server/config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
compress: z.boolean().optional(),
configOrigin: z.string().optional(),
crossOrigin: z
.union([
z.literal(false),
z.literal('anonymous'),
z.literal('use-credentials'),
])
.union([z.literal('anonymous'), z.literal('use-credentials')])
.optional(),
devIndicators: z
.object({
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ export interface NextConfig extends Record<string, any> {
*
* @see [`crossorigin` attribute documentation](https://developer.mozilla.org/docs/Web/HTML/Attributes/crossorigin)
*/
crossOrigin?: false | 'anonymous' | 'use-credentials'
crossOrigin?: 'anonymous' | 'use-credentials'

/**
* Use [SWC compiler](https://swc.rs) to minify the generated JavaScript
Expand Down
4 changes: 2 additions & 2 deletions test/turbopack-tests-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2427,10 +2427,10 @@
"runtimeError": false
},
"test/e2e/app-dir/app-config-crossorigin/index.test.ts": {
"passed": [],
"failed": [
"passed": [
"app dir - crossOrigin config should render correctly with assetPrefix: \"/\""
],
"failed": [],
"pending": [],
"flakey": [],
"runtimeError": false
Expand Down

0 comments on commit 724c446

Please sign in to comment.