|
1 | | -{% extends "layout.html" %} |
| 1 | +{% extends "!layout.html" %} |
2 | 2 |
|
3 | 3 | {% block extrahead %} |
4 | 4 | {{ super() }} |
5 | | -<!-- Dynamic base URL calculation for static hosting --> |
6 | 5 | <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; |
15 | 12 | } |
16 | 13 |
|
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 = '/'; |
30 | 16 |
|
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] + '/'; |
57 | 22 | } |
58 | 23 | } |
59 | 24 |
|
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 | + }); |
71 | 31 |
|
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 | + }); |
74 | 38 |
|
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 | + })(); |
79 | 46 | </script> |
| 47 | +{% endblock extrahead %} |
80 | 48 |
|
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 %} |
92 | 50 | <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> |
104 | 65 | </div> |
105 | | - </div> |
106 | 66 | </article> |
107 | | -{% endblock %} |
| 67 | + |
| 68 | +{% endblock body %} |
0 commit comments