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

Support crossOrigin in Turbopack #61461

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')])
Copy link
Member

Choose a reason for hiding this comment

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

Isn't that a breaking change?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's a bugfix, but regardless this option wasn't actually being used, it wasn't even documented actually, we're adding docs today 🙏

.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
Loading