Skip to content
Closed
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
8 changes: 6 additions & 2 deletions framework-views/Build-DocfxFrameworkViews.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,13 @@ try {
$root = if ($view -ceq $defaultView) { '""' } else { "/_frameworks/$directory" }
' ~^{0}$ {1};' -f [regex]::Escape($directory), $root
}
Write-Json "$site/framework-views.json" ([ordered]@{ schemaVersion = 1; apiPath = "/$ApiPath/"; defaultView = $defaultView; viewOrder = @($views.Keys); views = $views })
$availability = Get-FrameworkPageAvailability $site $ApiPath $defaultView $views
Merge-SharedNavigationToc $site $work $buildDestination $ApiPath $defaultView $views $availability
$fallbackRoutes = Get-FrameworkNginxFallbackRules $defaultView $views
Write-Json "$site/framework-views.json" ([ordered]@{ schemaVersion = 1; apiPath = "/$ApiPath/"; defaultView = $defaultView; viewOrder = @($views.Keys); views = $views; availability = $availability })
(Get-Content -LiteralPath "$PSScriptRoot/framework-views.nginx.conf" -Raw).Replace('__API__', "/$ApiPath/").
Replace('__VIEWS__', ($routes -join "`n")).Replace('__SEARCH__', ($searchRoutes -join "`n")) | Set-Content -LiteralPath "$work/nginx.conf"
Replace('__VIEWS__', ($routes -join "`n")).Replace('__SEARCH__', ($searchRoutes -join "`n")).
Replace('__FALLBACK__', ($fallbackRoutes -join "`n")) | Set-Content -LiteralPath "$work/nginx.conf"
Write-Host "Site: $site"
} finally {
$outputLock.Dispose()
Expand Down
107 changes: 107 additions & 0 deletions framework-views/FrameworkViewConventions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,110 @@ function Get-FrameworkOutputEntries([string] $Path, [string] $OwnershipFile, [st
$links = Get-ChildItem -LiteralPath $Path -Force -Recurse -Attributes ReparsePoint | Select-Object -First 1
if ($links) { throw "Framework output contains a link. Move '$($links.FullName)' before rebuilding." }
}

function Get-FrameworkPageAvailability([string] $SiteRoot, [string] $ApiPath, [string] $DefaultView, $Views) {
$pageViews = [Collections.Generic.Dictionary[string, Collections.Generic.List[string]]]::new([StringComparer]::Ordinal)
foreach ($view in $Views.Keys) {
$viewApiRoot = if ($view -ceq $DefaultView) {
Join-Path $SiteRoot $ApiPath
} else {
Join-Path $SiteRoot "_frameworks/$($Views[$view].directory)/$ApiPath"
}
if (!(Test-Path -LiteralPath $viewApiRoot -PathType Container)) { continue }
foreach ($file in [IO.Directory]::EnumerateFiles($viewApiRoot, '*.html', [IO.SearchOption]::AllDirectories)) {
$relative = [IO.Path]::GetRelativePath($viewApiRoot, $file).Replace('\', '/')
if (!$pageViews.ContainsKey($relative)) {
$pageViews[$relative] = [Collections.Generic.List[string]]::new()
}
$pageViews[$relative].Add($view)
}
}

$setIndexes = [Collections.Generic.Dictionary[string, int]]::new([StringComparer]::Ordinal)
$sets = [Collections.Generic.List[string[]]]::new()
$pages = [ordered]@{}
$sortedKeys = [string[]]$pageViews.Keys
[Array]::Sort($sortedKeys, [StringComparer]::Ordinal)
foreach ($relative in $sortedKeys) {
$available = $pageViews[$relative].ToArray()
$key = $available -join ','
if (!$setIndexes.ContainsKey($key)) {
$setIndexes[$key] = $sets.Count
$sets.Add($available)
}
$pages[$relative] = $setIndexes[$key]
}
[ordered]@{ sets = $sets.ToArray(); pages = $pages }
}

function Get-FrameworkNginxFallbackRules([string] $DefaultView, $Views) {
foreach ($view in $Views.Keys) {
$prefix = if ($view -ceq $DefaultView) { '' } else { "/_frameworks/$($Views[$view].directory)" }
' if (-f $document_root{0}$framework_fallback_target) {{ return 302 $uri?${{framework_query_prefix}}view={1}; }}' -f $prefix, $view
}
}

function Resolve-FrameworkApiRelativePage([string] $Href, [string] $ApiPrefix) {
if (!$Href) { return $null }
$normalized = ($Href -split '[?#]', 2)[0].TrimStart('/')
if (!$normalized.StartsWith($ApiPrefix, [StringComparison]::Ordinal)) { return $null }
$relative = $normalized.Substring($ApiPrefix.Length)
if (!$relative -or $relative.EndsWith('/')) { "${relative}index.html" } else { $relative }
}

function Merge-NavigationTocItems($TargetItems, $CandidateItemLists, [string] $ApiPrefix, $Availability) {
$changed = $false
if (!$TargetItems) { return $false }
for ($index = 0; $index -lt $TargetItems.Count; $index++) {
$item = $TargetItems[$index]
$href = if ($item.Contains('href')) { $item['href'] }
$relative = Resolve-FrameworkApiRelativePage $href $ApiPrefix
if ($relative -and !$Availability.pages.Contains($relative)) {
foreach ($candidateList in $CandidateItemLists) {
if (!$candidateList -or $index -ge $candidateList.Count) { continue }
$candidate = $candidateList[$index]
$candidateHref = if ($candidate.Contains('href')) { $candidate['href'] }
$candidateRelative = Resolve-FrameworkApiRelativePage $candidateHref $ApiPrefix
if ($candidateRelative -and $Availability.pages.Contains($candidateRelative)) {
$item['href'] = $candidateHref
foreach ($field in 'topicHref', 'tocHref') {
if ($candidate.Contains($field)) { $item[$field] = $candidate[$field] }
}
$changed = $true
break
}
}
}
if ($item.Contains('items') -and $item['items']) {
$childCandidates = @(
foreach ($candidateList in $CandidateItemLists) {
if ($candidateList -and $index -lt $candidateList.Count -and $candidateList[$index].Contains('items')) {
, $candidateList[$index]['items']
}
}
)
if (Merge-NavigationTocItems $item['items'] $childCandidates $ApiPrefix $Availability) { $changed = $true }
}
}
$changed
}

function Merge-SharedNavigationToc([string] $SiteRoot, [string] $WorkRoot, [string] $BuildDestination, [string] $ApiPath, [string] $DefaultView, $Views, $Availability) {
$tocPath = Join-Path $SiteRoot 'toc.json'
if (!(Test-Path -LiteralPath $tocPath -PathType Leaf)) { return }
$targetToc = Read-Json $tocPath
if (!$targetToc -or !$targetToc.Contains('items')) { return }
$candidateLists = @(
foreach ($view in $Views.Keys) {
if ($view -ceq $DefaultView) { continue }
$candidatePath = Join-Path $WorkRoot "$($Views[$view].directory)/$BuildDestination/toc.json"
if (!(Test-Path -LiteralPath $candidatePath -PathType Leaf)) { continue }
$candidateToc = Read-Json $candidatePath
if ($candidateToc -and $candidateToc.Contains('items')) { , $candidateToc['items'] }
}
)
if (!$candidateLists.Count) { return }
if (Merge-NavigationTocItems $targetToc['items'] $candidateLists "$ApiPath/" $Availability) {
Write-Json $tocPath $targetToc
}
}
17 changes: 12 additions & 5 deletions framework-views/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,20 @@ The generator uses the deepest common directory of the `metadata.dest` paths as

DocFX owns project mappings, filters, exclusions, overwrites and other metadata options. MSBuild supplies each mapped project's evaluated target frameworks, including inherited values. Remove any `TargetFramework` metadata property so the generator can choose the target per view. A project contributes documentation only to its evaluated targets. Unsupported TFMs, including platform-qualified TFMs, fail with the project path and the target in the diagnostic.

The default view is the first discovered target in this order. The same order determines fallback precedence.
The default view is the first discovered target in this order. The same order determines fallback precedence everywhere (hyperlinks, framework selector, and server-side HTTP routing):

1. Modern .NET, by descending numeric version
2. .NET Core, by descending numeric version
3. .NET Standard, by descending numeric version
4. .NET Framework, by descending numeric version

For example, the order is `net11.0`, `net10.0`, `net9.0`, `netcoreapp3.1`, `netstandard2.1`, `netstandard2.0`, `net481`, `net48`, `net462`. New unqualified versions follow the same rules when the installed SDK and DocFX support them.
For example, the order is `net11.0`, `net10.0`, `netstandard2.1`, `netstandard2.0`, `net481`, `net48`, `net462`. New unqualified versions follow the same rules when the installed SDK and DocFX support them.

The selected framework is treated as a preference rather than an unconditional routing constraint:

- Whenever a destination API page exists in the requested framework, that framework is preserved.
- Whenever a destination API page does not exist in the requested framework, resolution selects the first framework in precedence order that contains the destination (whether that target is the default framework or a non-default framework).
- Pages that do not exist in any framework return HTTP 404, and unknown framework identifiers return HTTP 400.

Keep build content, resources, and consumer template overlays under the configuration directory. Project sources can sit outside it. Omit a site-local entry from `build.xref` if it can resolve UIDs from another view. Duplicate UIDs within a view have the same constraints as a standard DocFX build.

Expand Down Expand Up @@ -67,9 +73,10 @@ docker run --rm --mount "type=bind,source=$($PWD.Path),target=/workspace" -w /wo

## Outputs and hosting

- `<OutputRoot>/site/` is the packaged static site. The default view is at the site root. Additional views retain native API HTML, xrefs and search indexes in `_frameworks/<directory>/`. Shared conceptual pages and assets are published once.
- `<OutputRoot>/site/framework-views.json` is the schema-version-1 manifest served at `/framework-views.json`. It contains the API root, default view, framework order, and per-view TFM, directory, xref URL and projects.
- `<OutputRoot>/nginx.conf` serves the public API path and native xref map by exact `?view=` value. Missing or empty view uses the default. Unknown API or xref views receive HTTP 400. Missing pages or slash-terminated directory indexes in a known view receive HTTP 404. The browser selector resolves fallback before navigating. The configuration also serves the view-specific search indexes and keeps physical framework copies internal.
- `<OutputRoot>/site/` is the packaged static site. The default view is at the site root. Additional views retain native API HTML, xrefs and search indexes in `_frameworks/<directory>/`. Shared conceptual pages and assets are published once. When shared root navigation (`toc.json`) references an API section absent from the default view, the generator merges its resolved HTML link from the first view in precedence order where it exists.
- `<OutputRoot>/site/framework-views.json` is the schema-version-1 manifest served at `/framework-views.json`. It contains the API root, default view, framework order, per-view TFM, directory, xref URL and projects, and a compact deduplicated `availability` inventory (`sets` and `pages`) derived from the actual generated native DocFX HTML output.
- `<OutputRoot>/nginx.conf` serves the public API path and native xref map by exact `?view=` value. Missing or empty view uses the default. Unknown API or xref views (or excess duplicate `view` parameters) receive HTTP 400. For HTML pages and `/`-terminated directory indexes under the API root that are unavailable in the requested view, NGINX checks the generated framework directories in precedence order and responds with HTTP 302 to the resolved public URL (`?view=<resolved>`), preserving unrelated query parameters while browsers preserve fragments. Pages missing from all views, non-HTML API resources (such as view-isolated `toc.json` files or missing static files), and direct requests under `/_frameworks/` receive HTTP 404 without redirecting. The configuration also serves view-specific search indexes and keeps physical framework copies internal.
- The `ms-style` client script (`public/framework-view.js`) applies the same destination-aware resolution policy across global navigation, TOCs, breadcrumbs, cross-references, search results, ordinary internal API hyperlinks, and the framework selector. Resolved `href` attributes are written directly to anchors so opening links in new tabs works natively without click interception. Single-framework DocFX builds omit `_frameworkApi` and keep standard native behavior.
- Per-TFM stage directories and logs are available under the output root for inspection.

Use [framework-views.Dockerfile](framework-views.Dockerfile) with a context containing `site/` and `nginx.conf`. It uses a pinned NGINX base by default, accepts `NGINX_IMAGE` as an explicit build argument, checks for the manifest and runs `nginx -t`. A consumer can provide its own Dockerfile or server configuration and preserve the public path, routing, search worker and cache contract.
Expand Down
38 changes: 37 additions & 1 deletion framework-views/framework-views.nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,36 @@ map $uri $framework_directory_index {
~/$ $framework_root${uri}index.html;
}

# Only HTML pages and directory indexes participate in destination-aware fallback.
map $uri $framework_fallback_target {
default "";
~\.html$ $uri;
~/$ ${uri}index.html;
}

# Strip case-insensitive view arguments while preserving unrelated query parameters.
map $args $framework_args_1 {
default $args;
~*^view=[^&]*&?$ "";
~*^view=[^&]*&(?<after>.+)$ $after;
~*^(?<before>.*)&view=[^&]*&?$ $before;
~*^(?<before>.*)&view=[^&]*&(?<after>.+)$ $before&$after;
}

map $framework_args_1 $framework_args_2 {
default $framework_args_1;
~*^view=[^&]*&?$ "";
~*^view=[^&]*&(?<after>.+)$ $after;
~*^(?<before>.*)&view=[^&]*&?$ $before;
~*^(?<before>.*)&view=[^&]*&(?<after>.+)$ $before&$after;
}

map $framework_args_2 $framework_query_prefix {
"" "";
~^(?<clean>.*)&$ "${clean}&";
default "${framework_args_2}&";
}

map $status $fingerprinted_asset_cache_control {
default "no-store";
~^2 "public, max-age=31536000, immutable";
Expand All @@ -38,7 +68,13 @@ server {

location ^~ __API__ {
if ($framework_root = invalid) { return 400 "Unknown documentation view."; }
try_files $framework_root$uri $framework_directory_index =404;
try_files $framework_root$uri $framework_directory_index @framework_fallback;
}
location @framework_fallback {
if ($framework_fallback_target = "") { return 404; }
if ($framework_args_2 ~* "(?:^|&)view=") { return 400 "Unknown documentation view."; }

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Duplicate views bypass rejection

The new check runs only after a page lookup fails. An existing API page requested with three valid view arguments still returns 200, and /xrefmap.yml skips this check too. This contradicts the new documentation promising 400 for excess view arguments, so callers cannot rely on that rule. Apply the check before both lookups, or narrow the documented promise to fallback requests.

Prompt To Fix With AI
This is a comment left during a code review.
Path: framework-views/framework-views.nginx.conf
Line: 75

Comment:
**Duplicate views bypass rejection**

The new check runs only after a page lookup fails. An existing API page requested with three valid `view` arguments still returns 200, and `/xrefmap.yml` skips this check too. This contradicts the new documentation promising 400 for excess `view` arguments, so callers cannot rely on that rule. Apply the check before both lookups, or narrow the documented promise to fallback requests.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Fix in Codex

__FALLBACK__
return 404;
}
location = /xrefmap.yml {
if ($framework_root = invalid) { return 400 "Unknown documentation view."; }
Expand Down
Loading