Skip to content

Commit 3ca58b9

Browse files
authored
Merge pull request #17 from Unity-Lab-AI/codex/analyze-github-pages-deployment-issue
Fix GitHub Pages asset loading
2 parents 903ca10 + bce8375 commit 3ca58b9

2 files changed

Lines changed: 23 additions & 47 deletions

File tree

app.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,30 @@ let hasMicPermission = false;
1414
const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
1515
const synth = window.speechSynthesis;
1616

17-
const directoryUrl = window.__UNITY_VOICE_DIRECTORY_URL__
18-
?? (() => {
19-
const href = window.location.href;
20-
const pathname = window.location.pathname || '';
21-
const lastSegment = pathname.substring(pathname.lastIndexOf('/') + 1);
22-
23-
if (href.endsWith('/')) {
24-
return href;
17+
const currentScript = document.currentScript;
18+
const directoryUrl = (() => {
19+
if (currentScript?.src) {
20+
try {
21+
return new URL('./', currentScript.src).toString();
22+
} catch (error) {
23+
console.error('Failed to derive directory from script src:', error);
2524
}
25+
}
2626

27-
if (lastSegment && lastSegment.includes('.')) {
28-
return href.substring(0, href.lastIndexOf('/') + 1);
29-
}
27+
const href = window.location.href;
28+
const pathname = window.location.pathname || '';
29+
const lastSegment = pathname.substring(pathname.lastIndexOf('/') + 1);
30+
31+
if (href.endsWith('/')) {
32+
return href;
33+
}
34+
35+
if (lastSegment && lastSegment.includes('.')) {
36+
return href.substring(0, href.lastIndexOf('/') + 1);
37+
}
3038

31-
return `${href}/`;
32-
})();
39+
return `${href}/`;
40+
})();
3341

3442
function resolveAssetPath(relativePath) {
3543
try {

index.html

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,8 @@
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<title>Unity Voice Chat</title>
7-
<script>
8-
(() => {
9-
const href = window.location.href;
10-
const pathname = window.location.pathname || '';
11-
const lastSegment = pathname.substring(pathname.lastIndexOf('/') + 1);
12-
13-
const directoryUrl = href.endsWith('/')
14-
? href
15-
: lastSegment && lastSegment.includes('.')
16-
? href.substring(0, href.lastIndexOf('/') + 1)
17-
: `${href}/`;
18-
19-
window.__UNITY_VOICE_DIRECTORY_URL__ = directoryUrl;
20-
21-
const resolveAsset = (relativePath) => {
22-
try {
23-
return new URL(relativePath, directoryUrl).toString();
24-
} catch (error) {
25-
console.error('Failed to resolve asset path:', error);
26-
return relativePath;
27-
}
28-
};
29-
30-
const styleLink = document.createElement('link');
31-
styleLink.rel = 'stylesheet';
32-
styleLink.href = resolveAsset('style.css');
33-
document.head.appendChild(styleLink);
34-
35-
const script = document.createElement('script');
36-
script.defer = true;
37-
script.src = resolveAsset('app.js');
38-
document.head.appendChild(script);
39-
})();
40-
</script>
7+
<link rel="stylesheet" href="./style.css">
8+
<script defer src="./app.js"></script>
419
</head>
4210
<body>
4311
<div id="background" aria-hidden="true"></div>

0 commit comments

Comments
 (0)