|
| 1 | +using System.Buffers; |
| 2 | +using System.Linq; |
| 3 | +using System.Text; |
1 | 4 | using System.Text.Json; |
2 | 5 | using Microsoft.AspNetCore.Http; |
3 | 6 | using Microsoft.AspNetCore.Http.Json; |
@@ -29,28 +32,18 @@ public async Task ExecuteAsync(HttpContext httpContext) |
29 | 32 | { |
30 | 33 | var options = GetSerializerOptions(httpContext); |
31 | 34 | var sharedData = httpContext.RequestServices.GetService<InertiaSharedData>(); |
32 | | - var mergedProps = MergeProps(_props, sharedData, options); |
33 | | - |
34 | | - var pageData = new |
35 | | - { |
36 | | - component = _component, |
37 | | - props = mergedProps, |
38 | | - url = httpContext.Request.Path + httpContext.Request.QueryString, |
39 | | - version = InertiaMiddleware.Version, |
40 | | - }; |
| 35 | + var url = httpContext.Request.Path + httpContext.Request.QueryString; |
| 36 | + var pageJson = SerializePage(_component, _props, sharedData, url, options); |
41 | 37 |
|
42 | 38 | if (httpContext.Request.IsInertia()) |
43 | 39 | { |
44 | 40 | httpContext.Response.Headers[InertiaHttpExtensions.InertiaHeader] = "true"; |
45 | 41 | httpContext.Response.Headers["Vary"] = InertiaHttpExtensions.InertiaHeader; |
46 | 42 | httpContext.Response.ContentType = "application/json"; |
47 | | - var json = JsonSerializer.Serialize(pageData, options); |
48 | | - await httpContext.Response.WriteAsync(json); |
| 43 | + await httpContext.Response.WriteAsync(pageJson); |
49 | 44 | return; |
50 | 45 | } |
51 | 46 |
|
52 | | - var pageJson = JsonSerializer.Serialize(pageData, options); |
53 | | - |
54 | 47 | var renderer = httpContext.RequestServices.GetRequiredService<IInertiaPageRenderer>(); |
55 | 48 | await renderer.RenderPageAsync(httpContext, pageJson); |
56 | 49 | } |
@@ -84,36 +77,89 @@ private static JsonSerializerOptions GetSerializerOptions(HttpContext httpContex |
84 | 77 | return fallback; |
85 | 78 | } |
86 | 79 |
|
87 | | - private static object MergeProps( |
| 80 | + /// <summary> |
| 81 | + /// Serializes the Inertia page envelope in a single pass. When there is no shared |
| 82 | + /// data, endpoint props stream straight to the output with no intermediate DOM. |
| 83 | + /// When shared data is present, endpoint props must be materialized once (to know |
| 84 | + /// their keys), then shared data and props are written into one object — endpoint |
| 85 | + /// props keep priority (shared keys they define are skipped), so no key is emitted |
| 86 | + /// twice. |
| 87 | + /// </summary> |
| 88 | + private static string SerializePage( |
| 89 | + string component, |
88 | 90 | object? props, |
89 | 91 | InertiaSharedData? sharedData, |
| 92 | + string url, |
90 | 93 | JsonSerializerOptions options |
91 | 94 | ) |
92 | 95 | { |
93 | | - if (sharedData is null || sharedData.All.Count == 0) |
| 96 | + var buffer = new ArrayBufferWriter<byte>(); |
| 97 | + using (var writer = new Utf8JsonWriter(buffer)) |
94 | 98 | { |
95 | | - return props ?? new { }; |
| 99 | + writer.WriteStartObject(); |
| 100 | + writer.WriteString("component", component); |
| 101 | + |
| 102 | + writer.WritePropertyName("props"); |
| 103 | + if (sharedData is null || sharedData.All.Count == 0) |
| 104 | + { |
| 105 | + JsonSerializer.Serialize(writer, props ?? EmptyProps, options); |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + WriteMergedProps(writer, props, sharedData, options); |
| 110 | + } |
| 111 | + |
| 112 | + writer.WriteString("url", url); |
| 113 | + writer.WriteString("version", InertiaMiddleware.Version); |
| 114 | + writer.WriteEndObject(); |
96 | 115 | } |
97 | 116 |
|
98 | | - var result = new Dictionary<string, object?>(); |
| 117 | + return Encoding.UTF8.GetString(buffer.WrittenSpan); |
| 118 | + } |
99 | 119 |
|
100 | | - // Add shared data first (lower priority) |
| 120 | + private static void WriteMergedProps( |
| 121 | + Utf8JsonWriter writer, |
| 122 | + object? props, |
| 123 | + InertiaSharedData sharedData, |
| 124 | + JsonSerializerOptions options |
| 125 | + ) |
| 126 | + { |
| 127 | + // Endpoint props are materialized once so their top-level keys are known; |
| 128 | + // this is unavoidable for a correct merge but happens a single time. |
| 129 | + using var propsDoc = |
| 130 | + props is null ? null : JsonSerializer.SerializeToDocument(props, options); |
| 131 | + |
| 132 | + var propKeys = |
| 133 | + propsDoc is null |
| 134 | + ? null |
| 135 | + : new HashSet<string>( |
| 136 | + propsDoc.RootElement.EnumerateObject().Select(p => p.Name), |
| 137 | + StringComparer.Ordinal |
| 138 | + ); |
| 139 | + |
| 140 | + writer.WriteStartObject(); |
| 141 | + |
| 142 | + // Shared data (lower priority) — skip any key the endpoint props also define. |
101 | 143 | foreach (var kvp in sharedData.All) |
102 | 144 | { |
103 | | - result[kvp.Key] = kvp.Value; |
| 145 | + if (propKeys is not null && propKeys.Contains(kvp.Key)) |
| 146 | + continue; |
| 147 | + |
| 148 | + writer.WritePropertyName(kvp.Key); |
| 149 | + JsonSerializer.Serialize(writer, kvp.Value, options); |
104 | 150 | } |
105 | 151 |
|
106 | | - // Add endpoint props (higher priority — overwrites shared data) |
107 | | - // Use JSON round-trip to merge endpoint props into shared data |
108 | | - if (props is not null) |
| 152 | + // Endpoint props (higher priority). |
| 153 | + if (propsDoc is not null) |
109 | 154 | { |
110 | | - var json = JsonSerializer.SerializeToElement(props, options); |
111 | | - foreach (var property in json.EnumerateObject()) |
| 155 | + foreach (var property in propsDoc.RootElement.EnumerateObject()) |
112 | 156 | { |
113 | | - result[property.Name] = property.Value; |
| 157 | + property.WriteTo(writer); |
114 | 158 | } |
115 | 159 | } |
116 | 160 |
|
117 | | - return result; |
| 161 | + writer.WriteEndObject(); |
118 | 162 | } |
| 163 | + |
| 164 | + private static readonly object EmptyProps = new(); |
119 | 165 | } |
0 commit comments