Skip to content

Commit 54456ed

Browse files
committed
refactor: merge duplicated Rust and TypeScript types, share one disk
cache Rust: - One RouteFileEntry for route file entries; layout and template entries embed it with serde(flatten). Manifest JSON is unchanged. - ComponentRef replaces TemplateInfo and ErrorBoundaryInfo. - The image cache stores OptimizedImage directly, so hits move the bytes instead of cloning them. - SetCookieArgs flattens PendingCookie instead of copying 11 fields. - ImageCache and OgImageCache wrap one DiskBackedCache with safe SHA-256 file names and atomic writes. Production folders are scoped by project hash and build ID, and both caches detect production through Config::is_production(). - Rename the user cache config to CacheSettings, and nest MemoryConfig in the runtime CacheConfig. TypeScript: - One MetadataFileGeneratorOptions for robots, feed and sitemap. - RouteFileEntry for route file entries; layouts and templates extend it. - Single definitions of NavigationOptions, ComponentRebuildResult and MetadataParams. - The proxy shares CookieOptions with cookies() and now writes boolean sameSite, priority and partitioned. - DataFetchResult derives from ServerSidePropsResult, and SimpleProxyResult from ProxyResult. - Rename the client registry's ComponentInfo to ClientRegistryEntry, and make WindowWithRari Window & GlobalWithRari. Closes RARI-45 Closes RARI-46 Closes RARI-47
1 parent ef4e194 commit 54456ed

34 files changed

Lines changed: 834 additions & 845 deletions

File tree

‎crates/rari/src/rendering/html_shell.rs‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ mod tests {
722722

723723
use super::*;
724724
use crate::server::routing::app_router::{
725-
AppRouteEntry, AppRouteMatch, LayoutEntry, LoadingEntry,
725+
AppRouteEntry, AppRouteMatch, LayoutEntry, LoadingEntry, RouteFileEntry,
726726
};
727727

728728
fn sample_route_match() -> AppRouteMatch {
@@ -739,13 +739,15 @@ mod tests {
739739
},
740740
params: FxHashMap::default(),
741741
layouts: vec![LayoutEntry {
742-
path: "/".to_string(),
743-
file_path: "layout.tsx".to_string(),
744-
component_id: None,
745-
css: vec!["/layout.css".to_string(), "/shared.css".to_string()],
742+
entry: RouteFileEntry {
743+
path: "/".to_string(),
744+
file_path: "layout.tsx".to_string(),
745+
component_id: None,
746+
css: vec!["/layout.css".to_string(), "/shared.css".to_string()],
747+
additional_paths: None,
748+
},
746749
parent_path: None,
747750
is_root: true,
748-
additional_paths: None,
749751
}],
750752
loading: Some(LoadingEntry {
751753
path: "/loading".to_string(),

‎crates/rari/src/rendering/layout/core.rs‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ use crate::{
2222
rendering::{
2323
base::RenderPipelines,
2424
html_shell::inject_css_links,
25-
layout::{
26-
LayoutInfo, RouteComposer,
27-
route_composer::{ErrorBoundaryInfo, TemplateInfo},
28-
},
25+
layout::{LayoutInfo, RouteComposer, route_composer::ComponentRef},
2926
},
3027
runtime::{
3128
JsExecutionRuntime,
@@ -1756,18 +1753,18 @@ impl LayoutRenderer {
17561753
})
17571754
.collect();
17581755

1759-
let templates: Vec<TemplateInfo> = route_match
1756+
let templates: Vec<ComponentRef> = route_match
17601757
.templates
17611758
.iter()
1762-
.map(|template| TemplateInfo {
1759+
.map(|template| ComponentRef {
17631760
component_id: utils::create_component_id(&template.file_path),
17641761
file_path: template.file_path.clone(),
17651762
})
17661763
.collect();
17671764

17681765
let error_boundary = route_match.error.as_ref().map(|error| {
17691766
let component_id = utils::create_client_component_id(&error.file_path);
1770-
ErrorBoundaryInfo { component_id, file_path: error.file_path.clone() }
1767+
ComponentRef { component_id, file_path: error.file_path.clone() }
17711768
});
17721769

17731770
let metadata = context

‎crates/rari/src/rendering/layout/layout_reuse.rs‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,19 @@ mod tests {
115115
use serde_json::json;
116116

117117
use super::*;
118-
use crate::server::routing::app_router::LayoutEntry;
118+
use crate::server::routing::app_router::{LayoutEntry, RouteFileEntry};
119119

120120
fn layout(path: &str, file_path: &str) -> LayoutEntry {
121121
LayoutEntry {
122-
path: path.to_string(),
123-
file_path: file_path.to_string(),
124-
component_id: None,
125-
css: vec![],
122+
entry: RouteFileEntry {
123+
path: path.to_string(),
124+
file_path: file_path.to_string(),
125+
component_id: None,
126+
css: vec![],
127+
additional_paths: None,
128+
},
126129
parent_path: None,
127130
is_root: path == "/",
128-
additional_paths: None,
129131
}
130132
}
131133

‎crates/rari/src/rendering/layout/mod.rs‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ mod tests {
2727
use crate::server::{
2828
middleware::request_context::RequestContext,
2929
routing::{
30-
app_router::{AppRouteEntry, AppRouteMatch, LayoutEntry},
30+
app_router::{AppRouteEntry, AppRouteMatch, LayoutEntry, RouteFileEntry},
3131
types::ParamValue,
3232
},
3333
};
@@ -219,12 +219,14 @@ mod tests {
219219
},
220220
params: FxHashMap::default(),
221221
layouts: vec![LayoutEntry {
222-
path: "/".to_string(),
223-
file_path: "app/layout.tsx".to_string(),
224-
component_id: None,
225-
css: vec![],
222+
entry: RouteFileEntry {
223+
path: "/".to_string(),
224+
file_path: "app/layout.tsx".to_string(),
225+
component_id: None,
226+
css: vec![],
227+
additional_paths: None,
228+
},
226229
parent_path: None,
227-
additional_paths: None,
228230
is_root: true,
229231
}],
230232
loading: None,

‎crates/rari/src/rendering/layout/route_composer.rs‎

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,7 @@ pub struct LayoutInfo {
1010
}
1111

1212
#[derive(Debug, Clone)]
13-
pub struct TemplateInfo {
14-
pub component_id: String,
15-
pub file_path: String,
16-
}
17-
18-
#[derive(Debug, Clone)]
19-
pub struct ErrorBoundaryInfo {
13+
pub struct ComponentRef {
2014
pub component_id: String,
2115
pub file_path: String,
2216
}
@@ -43,7 +37,7 @@ impl RouteComposer {
4337
page_render_script: &str,
4438
layouts: &[LayoutInfo],
4539
pathname_json: &str,
46-
error_boundary: Option<&ErrorBoundaryInfo>,
40+
error_boundary: Option<&ComponentRef>,
4741
metadata_json: &str,
4842
) -> String {
4943
Self::format_compose_route(
@@ -69,10 +63,10 @@ impl RouteComposer {
6963
pub fn format_compose_route(
7064
page_render_script: &str,
7165
layouts: &[LayoutInfo],
72-
templates: &[TemplateInfo],
66+
templates: &[ComponentRef],
7367
pathname_json: &str,
7468
template_key_json: &str,
75-
error_boundary: Option<&ErrorBoundaryInfo>,
69+
error_boundary: Option<&ComponentRef>,
7670
metadata_json: &str,
7771
defer_rsc: bool,
7872
action_post_url_json: &str,
@@ -101,10 +95,10 @@ impl RouteComposer {
10195
pub fn format_compose_route_with_binary_key(
10296
page_render_script: &str,
10397
layouts: &[LayoutInfo],
104-
templates: &[TemplateInfo],
98+
templates: &[ComponentRef],
10599
pathname_json: &str,
106100
template_key_json: &str,
107-
error_boundary: Option<&ErrorBoundaryInfo>,
101+
error_boundary: Option<&ComponentRef>,
108102
metadata_json: &str,
109103
defer_rsc: bool,
110104
action_post_url_json: &str,
@@ -183,7 +177,7 @@ impl RouteComposer {
183177
serde_json::to_string(&value).unwrap_or_else(|_| "[]".to_string())
184178
}
185179

186-
fn templates_json(templates: &[TemplateInfo]) -> String {
180+
fn templates_json(templates: &[ComponentRef]) -> String {
187181
let value: Vec<Value> = templates
188182
.iter()
189183
.map(|template| {
@@ -352,8 +346,8 @@ mod tests {
352346
assert_eq!(empty_tpl, no_tpl);
353347
}
354348

355-
fn template_info(file_path: &str) -> TemplateInfo {
356-
TemplateInfo {
349+
fn template_info(file_path: &str) -> ComponentRef {
350+
ComponentRef {
357351
component_id: format!("template:{file_path}"),
358352
file_path: file_path.to_string(),
359353
}
@@ -390,14 +384,14 @@ mod tests {
390384

391385
#[test]
392386
fn test_template_ssr_module_key_normalizes_app_relative_paths() {
393-
let already_prefixed = RouteComposer::templates_json(&[TemplateInfo {
387+
let already_prefixed = RouteComposer::templates_json(&[ComponentRef {
394388
component_id: "template:src/app/template.tsx".to_string(),
395389
file_path: "src/app/template.tsx".to_string(),
396390
}]);
397391
assert!(already_prefixed.contains(r#""ssrModuleKey":"src/app/template.tsx""#));
398392
assert!(!already_prefixed.contains(r#""ssrModuleKey":"src/app/src/app/template.tsx""#));
399393

400-
let app_prefixed = RouteComposer::templates_json(&[TemplateInfo {
394+
let app_prefixed = RouteComposer::templates_json(&[ComponentRef {
401395
component_id: "template:app/template.tsx".to_string(),
402396
file_path: "app/template.tsx".to_string(),
403397
}]);
@@ -645,7 +639,7 @@ mod tests {
645639

646640
#[test]
647641
fn test_error_boundary_id_serialized() {
648-
let error_boundary = ErrorBoundaryInfo {
642+
let error_boundary = ComponentRef {
649643
component_id: "src/app/test/error.tsx".to_string(),
650644
file_path: "test/error.tsx".to_string(),
651645
};

‎crates/rari/src/runtime/ext/rari/react/metadata_collector.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
interface MetadataParams {
2-
readonly params: Readonly<Record<string, string>>
3-
readonly searchParams: Readonly<Record<string, string>>
4-
}
1+
import type { MetadataParams } from './metadata_head.ts'
52

63
type PageMetadata = Readonly<Record<string, unknown>>
74

‎crates/rari/src/runtime/ext/rari/rsc/client_registry.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
interface ComponentInfo {
1+
interface ClientRegistryEntry {
22
readonly id: string
33
readonly path: string
44
readonly type: 'client'
@@ -99,7 +99,7 @@ function registerClientComponent(
9999
return
100100
}
101101

102-
const componentInfo: ComponentInfo = {
102+
const componentInfo: ClientRegistryEntry = {
103103
id: componentId,
104104
path: componentPath,
105105
type: 'client',
@@ -136,7 +136,7 @@ function registerClientComponentNameAliases(
136136

137137
function isClientComponent(
138138
componentType: unknown,
139-
registry?: Readonly<{ readonly [key: string]: ComponentInfo }>,
139+
registry?: Readonly<{ readonly [key: string]: ClientRegistryEntry }>,
140140
): boolean {
141141
const clientRegistry = registry ?? g['~clientComponents'] ?? {}
142142

@@ -164,7 +164,7 @@ function isClientComponent(
164164
return false
165165
}
166166

167-
function getClientComponentInfo(componentType: unknown): ComponentInfo | null {
167+
function getClientComponentInfo(componentType: unknown): ClientRegistryEntry | null {
168168
if (componentType == null) return null
169169

170170
if (typeof componentType === 'function') {
@@ -226,7 +226,7 @@ function extractComponentNameFromPath(componentPath: string): string | null {
226226
return nameWithoutExt
227227
}
228228

229-
function listClientComponents(): Record<string, ComponentInfo> {
229+
function listClientComponents(): Record<string, ClientRegistryEntry> {
230230
return { ...g['~clientComponents'] }
231231
}
232232

‎crates/rari/src/runtime/ops.rs‎

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -857,24 +857,10 @@ pub fn op_get_csp_nonce(state: Rc<RefCell<OpState>>, #[string] request_id: Strin
857857

858858
#[derive(serde::Deserialize)]
859859
pub struct SetCookieArgs {
860-
name: String,
861-
value: String,
862-
path: Option<String>,
863-
domain: Option<String>,
864-
expires: Option<String>,
865-
#[serde(rename = "maxAge")]
866-
max_age: Option<i64>,
867-
#[serde(rename = "httpOnly", default)]
868-
http_only: bool,
869-
#[serde(default)]
870-
secure: bool,
871-
#[serde(rename = "sameSite")]
872-
same_site: Option<String>,
873-
priority: Option<String>,
874-
#[serde(default)]
875-
partitioned: bool,
876860
#[serde(rename = "requestId", default)]
877861
request_id: Option<String>,
862+
#[serde(flatten)]
863+
cookie: PendingCookie,
878864
}
879865

880866
#[allow(clippy::allow_attributes, clippy::needless_pass_by_value)]
@@ -884,53 +870,42 @@ pub fn op_set_cookie(
884870
state: Rc<RefCell<OpState>>,
885871
#[serde] args: SetCookieArgs,
886872
) -> Result<(), JsErrorBox> {
887-
if !actions::is_valid_cookie_name(&args.name) {
888-
return Err(JsErrorBox::type_error(format!("Invalid cookie name: '{}'", args.name)));
873+
let SetCookieArgs { request_id, mut cookie } = args;
874+
if !actions::is_valid_cookie_name(&cookie.name) {
875+
return Err(JsErrorBox::type_error(format!("Invalid cookie name: '{}'", cookie.name)));
889876
}
890877

891-
if !actions::is_valid_cookie_value(&args.value) {
878+
if !actions::is_valid_cookie_value(&cookie.value) {
892879
return Err(JsErrorBox::type_error(format!(
893880
"Invalid cookie value for '{}': contains invalid characters",
894-
args.name
881+
cookie.name
895882
)));
896883
}
897884

898-
if let Some(ref path) = args.path
885+
if let Some(ref path) = cookie.path
899886
&& !actions::is_valid_attr_value(path)
900887
{
901888
return Err(JsErrorBox::type_error(format!(
902889
"Invalid cookie path for '{}': '{}'",
903-
args.name, path
890+
cookie.name, path
904891
)));
905892
}
906893

907-
if let Some(ref domain) = args.domain
894+
if let Some(ref domain) = cookie.domain
908895
&& !actions::is_valid_attr_value(domain)
909896
{
910897
return Err(JsErrorBox::type_error(format!(
911898
"Invalid cookie domain for '{}': '{}'",
912-
args.name, domain
899+
cookie.name, domain
913900
)));
914901
}
915902

916903
let op_state_ref = state.borrow();
917-
if let Some(ctx) = resolve_request_context(&op_state_ref, args.request_id.as_deref()) {
918-
let path = args.path.or_else(|| Some("/".to_string()));
904+
if let Some(ctx) = resolve_request_context(&op_state_ref, request_id.as_deref()) {
905+
cookie.path = cookie.path.or_else(|| Some("/".to_string()));
919906
ctx.pending_cookies.insert(
920-
PendingCookieKey::new(&args.name, path.as_deref(), args.domain.as_deref()),
921-
PendingCookie {
922-
name: args.name,
923-
value: args.value,
924-
path,
925-
domain: args.domain,
926-
expires: args.expires,
927-
max_age: args.max_age,
928-
http_only: args.http_only,
929-
secure: args.secure,
930-
same_site: args.same_site,
931-
priority: args.priority,
932-
partitioned: args.partitioned,
933-
},
907+
PendingCookieKey::new(&cookie.name, cookie.path.as_deref(), cookie.domain.as_deref()),
908+
cookie,
934909
);
935910
}
936911

0 commit comments

Comments
 (0)