Skip to content

Commit 2b6e38c

Browse files
authored
Rollup merge of rust-lang#106491 - ehuss:error-index-redirect, r=GuillaumeGomez,notriddle
Fix error-index redirect to work with the back button. This fixes the redirect page at https://doc.rust-lang.org/error-index.html so that it works with the browser's back-button. The solution is to use `window.location.replace()`, which avoids adding the page to the browser history stack. This also cleans up the code a little bit, since it looks like it was written with different assumptions (maybe before f5857d5). I don't think there is a need to have a redirect at https://doc.rust-lang.org/error_codes/error-index.html since I don't think fragment-based links were ever used there. I have tested with Firefox, Chrome, and Safari. Going to `/error-index.html` redirects without adding to the stack, and can use the back button. Additionally, `/error-index.html#E0005` redirects correctly to the error page. Finally, there is an unrelated commit to remove the 404 hack. There is an official way of avoiding the generation of the 404 page with setting input-404 to an empty value. Fixes rust-lang#106485
2 parents 6ae0f08 + 72c3082 commit 2b6e38c

File tree

3 files changed

+11
-14
lines changed

3 files changed

+11
-14
lines changed

src/tools/error_index_generator/book_config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ src = ""
77
git-repository-url = "https://github.com/rust-lang/rust/"
88
additional-css = ["error-index.css"]
99
additional-js = ["error-index.js"]
10+
input-404 = ""
1011

1112
[output.html.search]
1213
enable = true

src/tools/error_index_generator/main.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ fn add_rust_attribute_on_codeblock(explanation: &str) -> String {
9898

9999
fn render_html(output_path: &Path) -> Result<(), Box<dyn Error>> {
100100
let mut introduction = format!(
101-
"<script src='redirect.js'></script>
102-
# Rust error codes index
101+
"# Rust error codes index
103102
104103
This page lists all the error codes emitted by the Rust compiler.
105104
@@ -149,7 +148,12 @@ This page lists all the error codes emitted by the Rust compiler.
149148
book.book.sections.push(BookItem::Chapter(chapter));
150149
book.build()?;
151150

152-
// We can't put this content into another file, otherwise `mbdbook` will also put it into the
151+
// The error-index used to be generated manually (without mdbook), and the
152+
// index was located at the top level. Now that it is generated with
153+
// mdbook, error-index.html has moved to error_codes/error-index.html.
154+
// This adds a redirect so that old links go to the new location.
155+
//
156+
// We can't put this content into another file, otherwise `mdbook` will also put it into the
153157
// output directory, making a duplicate.
154158
fs::write(
155159
output_path.join("error-index.html"),
@@ -163,14 +167,10 @@ This page lists all the error codes emitted by the Rust compiler.
163167
</head>
164168
<body>
165169
<div>If you are not automatically redirected to the error code index, please <a id="index-link" href="./error_codes/error-index.html">here</a>.
166-
<script>document.getElementById("index-link").click()</script>
167170
</body>
168171
</html>"#,
169172
)?;
170173

171-
// No need for a 404 file, it's already handled by the server.
172-
fs::remove_file(output_path.join("error_codes/404.html"))?;
173-
174174
Ok(())
175175
}
176176

src/tools/error_index_generator/redirect.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@
33
let code = window.location.hash.replace(/^#/, '');
44
// We have to make sure this pattern matches to avoid inadvertently creating an
55
// open redirect.
6-
if (!/^E[0-9]+$/.test(code)) {
6+
if (/^E[0-9]+$/.test(code)) {
7+
window.location.replace('./error_codes/' + code + '.html');
78
return;
89
}
9-
if (window.location.pathname.indexOf("/error_codes/") !== -1) {
10-
// We're not at the top level, so we don't prepend with "./error_codes/".
11-
window.location = './' + code + '.html';
12-
} else {
13-
window.location = './error_codes/' + code + '.html';
14-
}
1510
}
11+
window.location.replace('./error_codes/error-index.html');
1612
})()

0 commit comments

Comments
 (0)