Skip to content

Commit 953e0e5

Browse files
committed
Update 404.html again
1 parent 67aa614 commit 953e0e5

File tree

1 file changed

+51
-90
lines changed

1 file changed

+51
-90
lines changed
Lines changed: 51 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,68 @@
1-
{% extends "layout.html" %}
1+
{% extends "!layout.html" %}
22

33
{% block extrahead %}
44
{{ super() }}
5-
<!-- Dynamic base URL calculation for static hosting -->
65
<script>
7-
// Calculate the correct base path based on URL depth and site structure
8-
function calculateBasePath() {
9-
const pathname = window.location.pathname;
10-
const hostname = window.location.hostname;
11-
12-
// Handle root path
13-
if (pathname === '/' || pathname === '') {
14-
return './';
6+
// Fix static asset paths for 404 page on static hosting
7+
(function () {
8+
// Skip on localhost/development
9+
if (window.location.hostname === 'localhost' ||
10+
window.location.hostname === '127.0.0.1') {
11+
return;
1512
}
1613

17-
// Split path into segments and remove empty ones
18-
const pathSegments = pathname.split('/').filter(segment => segment !== '');
19-
20-
// Check if the last segment is a file
21-
const lastSegment = pathSegments[pathSegments.length - 1] || '';
22-
const fileExtensions = ['.html', '.htm', '.php', '.jsp', '.asp'];
23-
const isFile = fileExtensions.some(ext => lastSegment.toLowerCase().endsWith(ext));
24-
25-
// Calculate current directory depth (exclude filename if present)
26-
let currentDepth = isFile ? pathSegments.length - 1 : pathSegments.length;
27-
28-
// Determine the target depth based on site structure
29-
let targetDepth = 0;
14+
// Get the base path for _static assets
15+
let basePath = '/';
3016

31-
if (hostname.includes('docs-preview.pytorch.org')) {
32-
// For docs-preview: _static is at /pytorch/<project>/<version>/ level
33-
// Pattern: /pytorch/tutorials/1234/ or /pytorch/pytorch/5678/ or /pytorch/executorch/9101/
34-
const pytorchIndex = pathSegments.indexOf('pytorch');
35-
if (pytorchIndex >= 0 && pytorchIndex + 2 < pathSegments.length) {
36-
const projectSegment = pathSegments[pytorchIndex + 1]; // tutorials, pytorch, executorch, etc.
37-
const versionSegment = pathSegments[pytorchIndex + 2];
38-
39-
// Check if the third segment is a version number
40-
if (/^\d+$/.test(versionSegment)) {
41-
targetDepth = pytorchIndex + 3; // Go to /pytorch/<project>/<version>/ level
42-
}
43-
}
44-
} else if (hostname.includes('docs.pytorch.org')) {
45-
// For docs.pytorch.org: different projects may have different structures
46-
// Common patterns: /tutorials/, /docs/stable/, etc.
47-
const tutorialsIndex = pathSegments.indexOf('tutorials');
48-
if (tutorialsIndex >= 0) {
49-
targetDepth = tutorialsIndex + 1; // Go to tutorials level
50-
} else {
51-
// For other docs projects, try to find common patterns
52-
const docsIndex = pathSegments.indexOf('docs');
53-
if (docsIndex >= 0 && docsIndex + 1 < pathSegments.length) {
54-
const versionSegment = pathSegments[docsIndex + 1]; // stable, master, etc.
55-
targetDepth = docsIndex + 2; // Go to /docs/<version>/ level
56-
}
17+
// On docs.pytorch.org, use first subdirectory (e.g. /tutorials, /docs) as base
18+
if (window.location.hostname === 'docs.pytorch.org') {
19+
const pathParts = window.location.pathname.split('/').filter(part => part);
20+
if (pathParts.length > 0) {
21+
basePath = '/' + pathParts[0] + '/';
5722
}
5823
}
5924

60-
// Fallback: go to root if we can't determine structure
61-
if (targetDepth === 0) {
62-
targetDepth = 0;
63-
}
64-
65-
// Calculate how many levels to go up
66-
const levelsUp = Math.max(0, currentDepth - targetDepth);
67-
68-
// Generate the base path
69-
return levelsUp > 0 ? '../'.repeat(levelsUp) : './';
70-
}
25+
// Fix CSS links
26+
document.querySelectorAll('link[href*="_static"]').forEach(link => {
27+
if (!link.href.startsWith('http') && !link.href.startsWith('/')) {
28+
link.href = basePath + link.getAttribute('href');
29+
}
30+
});
7131

72-
const basePath = calculateBasePath();
73-
document.write('<base href="' + basePath + '">');
32+
// Fix script sources
33+
document.querySelectorAll('script[src*="_static"]').forEach(script => {
34+
if (!script.src.startsWith('http') && !script.src.startsWith('/')) {
35+
script.src = basePath + script.getAttribute('src');
36+
}
37+
});
7438

75-
// Debug logging (remove in production)
76-
console.log('Current path:', window.location.pathname);
77-
console.log('Current hostname:', window.location.hostname);
78-
console.log('Calculated base path:', basePath);
39+
// Fix image sources
40+
document.querySelectorAll('img[src*="_static"]').forEach(img => {
41+
if (!img.src.startsWith('http') && !img.src.startsWith('/')) {
42+
img.src = basePath + img.getAttribute('src');
43+
}
44+
});
45+
})();
7946
</script>
47+
{% endblock extrahead %}
8048

81-
<!-- Meta tags for 404 page -->
82-
<meta name="robots" content="noindex">
83-
<meta property="og:title" content="404 - Page Not Found">
84-
<meta property="og:description" content="The page you are looking for could not be found.">
85-
{% endblock %}
86-
87-
{% block docs_navbar %}
88-
{# Skip the docs navbar for 404 page #}
89-
{% endblock %}
90-
91-
{% block docs_body %}
49+
{% block body %}
9250
<article class="bd-article" id="pytorch-article">
93-
<div class="container text-center error-404-container">
94-
<h1 class="error-404-title">404</h1>
95-
<h2 class="error-404-subtitle">Page Not Found</h2>
96-
<p class="error-404-text">The page you are looking for might have been removed, had its name changed, or is temporarily unavailable.</p>
97-
<div class="error-404-buttons">
98-
<a href="{{ html_baseurl or '/' }}" class="btn btn-primary error-404-button">
99-
Back Home <i class="fa fa-arrow-right" aria-hidden="true"></i>
100-
</a>
101-
<a href="{{ theme_feedback_url|default('https://github.com/pytorch/pytorch') }}/issues/new?template=documentation.yml&labels=module:%20docs,broken-link" class="btn btn-secondary error-404-button error-404-issue-button">
102-
Found a broken link? File an issue <i class="fa-brands fa-github" aria-hidden="true"></i>
103-
</a>
51+
<div class="container text-center error-404-container">
52+
<h1 class="error-404-title">404</h1>
53+
<h2 class="error-404-subtitle">Page Not Found</h2>
54+
<p class="error-404-text">The page you are looking for might have been removed, had its name changed, or is
55+
temporarily unavailable.</p>
56+
<div class="error-404-buttons">
57+
<a href="{{ html_baseurl or '/' }}" class="btn btn-primary error-404-button">
58+
Back Home <i class="fa fa-arrow-right" aria-hidden="true"></i>
59+
</a>
60+
<a href="{{ theme_feedback_url|default('https://github.com/pytorch/pytorch') }}/issues/new?template=documentation.yml&labels=module:%20docs,broken-link"
61+
class="btn btn-secondary error-404-button error-404-issue-button">
62+
Found a broken link? File an issue <i class="fa-brands fa-github" aria-hidden="true"></i>
63+
</a>
64+
</div>
10465
</div>
105-
</div>
10666
</article>
107-
{% endblock %}
67+
68+
{% endblock body %}

0 commit comments

Comments
 (0)