From dfbe4fea1d71834a8f0922f0dc8cf63b66baabd6 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Thu, 4 May 2023 10:09:51 +0000 Subject: [PATCH] pass original url with content source data --- crates/turbopack-dev-server/src/source/mod.rs | 10 ++++++++++ crates/turbopack-dev-server/src/source/resolve.rs | 9 +++++++-- crates/turbopack-node/src/render/mod.rs | 1 + crates/turbopack-node/src/render/node_api_source.rs | 3 +++ crates/turbopack-node/src/render/rendered_source.rs | 3 +++ 5 files changed, 24 insertions(+), 2 deletions(-) diff --git a/crates/turbopack-dev-server/src/source/mod.rs b/crates/turbopack-dev-server/src/source/mod.rs index c431261f1c6b2..0af0c8dc54b75 100644 --- a/crates/turbopack-dev-server/src/source/mod.rs +++ b/crates/turbopack-dev-server/src/source/mod.rs @@ -236,6 +236,9 @@ pub struct ContentSourceData { pub method: Option, /// The full url (including query string), if requested. pub url: Option, + /// The full url (including query string) before rewrites where applied, if + /// requested. + pub original_url: Option, /// Query string items, if requested. pub query: Option, /// raw query string, if requested. Does not include the `?`. @@ -368,6 +371,7 @@ impl ContentSourceDataFilter { pub struct ContentSourceDataVary { pub method: bool, pub url: bool, + pub original_url: bool, pub query: Option, pub raw_query: bool, pub headers: Option, @@ -387,6 +391,7 @@ impl ContentSourceDataVary { let ContentSourceDataVary { method, url, + original_url, query, raw_query, headers, @@ -397,6 +402,7 @@ impl ContentSourceDataVary { } = self; *method = *method || other.method; *url = *url || other.url; + *original_url = *original_url || other.original_url; *body = *body || other.body; *cache_buster = *cache_buster || other.cache_buster; *raw_query = *raw_query || other.raw_query; @@ -412,6 +418,7 @@ impl ContentSourceDataVary { let ContentSourceDataVary { method, url, + original_url, query, raw_query, headers, @@ -426,6 +433,9 @@ impl ContentSourceDataVary { if other.url && !url { return false; } + if other.original_url && !original_url { + return false; + } if other.body && !body { return false; } diff --git a/crates/turbopack-dev-server/src/source/resolve.rs b/crates/turbopack-dev-server/src/source/resolve.rs index 10e46ec084db0..9ef1f2d9ad9f0 100644 --- a/crates/turbopack-dev-server/src/source/resolve.rs +++ b/crates/turbopack-dev-server/src/source/resolve.rs @@ -47,11 +47,12 @@ pub async fn resolve_source_request( ContentSourceResult::NeedData(needed) => { current_source = needed.source.resolve().await?; current_asset_path = needed.path.clone(); - data = request_to_data(&request_overwrites, &needed.vary).await?; + data = request_to_data(&request_overwrites, &request, &needed.vary).await?; } ContentSourceResult::Result { get_content, .. } => { let content_vary = get_content.vary().await?; - let content_data = request_to_data(&request_overwrites, &content_vary).await?; + let content_data = + request_to_data(&request_overwrites, &request, &content_vary).await?; let content = get_content.get(Value::new(content_data)); match &*content.await? { ContentSourceContent::Rewrite(rewrite) => { @@ -96,6 +97,7 @@ static CACHE_BUSTER: AtomicU64 = AtomicU64::new(0); async fn request_to_data( request: &SourceRequest, + original_request: &SourceRequest, vary: &ContentSourceDataVary, ) -> Result { let mut data = ContentSourceData::default(); @@ -105,6 +107,9 @@ async fn request_to_data( if vary.url { data.url = Some(request.uri.to_string()); } + if vary.original_url { + data.original_url = Some(original_request.uri.to_string()); + } if vary.body { data.body = Some(request.body.clone().into()); } diff --git a/crates/turbopack-node/src/render/mod.rs b/crates/turbopack-node/src/render/mod.rs index 8b7a52571edfb..2b72d7f6475ef 100644 --- a/crates/turbopack-node/src/render/mod.rs +++ b/crates/turbopack-node/src/render/mod.rs @@ -17,6 +17,7 @@ pub struct RenderData { params: IndexMap, method: String, url: String, + original_url: String, raw_query: String, raw_headers: Vec<(String, String)>, path: String, diff --git a/crates/turbopack-node/src/render/node_api_source.rs b/crates/turbopack-node/src/render/node_api_source.rs index 8c2618338b906..6e095fcdc6dab 100644 --- a/crates/turbopack-node/src/render/node_api_source.rs +++ b/crates/turbopack-node/src/render/node_api_source.rs @@ -114,6 +114,7 @@ impl GetContentSourceContent for NodeApiGetContentResult { ContentSourceDataVary { method: true, url: true, + original_url: true, raw_headers: true, raw_query: true, body: true, @@ -131,6 +132,7 @@ impl GetContentSourceContent for NodeApiGetContentResult { let ContentSourceData { method: Some(method), url: Some(url), + original_url: Some(original_url), raw_headers: Some(raw_headers), raw_query: Some(raw_query), body: Some(body), @@ -153,6 +155,7 @@ impl GetContentSourceContent for NodeApiGetContentResult { params: params.clone(), method: method.clone(), url: url.clone(), + original_url: original_url.clone(), raw_query: raw_query.clone(), raw_headers: raw_headers.clone(), path: format!("/{}", self.path), diff --git a/crates/turbopack-node/src/render/rendered_source.rs b/crates/turbopack-node/src/render/rendered_source.rs index 25933503b90b9..4272ba5def26b 100644 --- a/crates/turbopack-node/src/render/rendered_source.rs +++ b/crates/turbopack-node/src/render/rendered_source.rs @@ -186,6 +186,7 @@ impl GetContentSourceContent for NodeRenderGetContentResult { ContentSourceDataVary { method: true, url: true, + original_url: true, raw_headers: true, raw_query: true, ..Default::default() @@ -202,6 +203,7 @@ impl GetContentSourceContent for NodeRenderGetContentResult { let ContentSourceData { method: Some(method), url: Some(url), + original_url: Some(original_url), raw_headers: Some(raw_headers), raw_query: Some(raw_query), .. @@ -224,6 +226,7 @@ impl GetContentSourceContent for NodeRenderGetContentResult { params: params.clone(), method: method.clone(), url: url.clone(), + original_url: original_url.clone(), raw_query: raw_query.clone(), raw_headers: raw_headers.clone(), path: source.pathname.await?.clone_value(),