Skip to content

Commit b8e300d

Browse files
fix: extract app js to canonical module
* Extracted JavaScript from templates/tail.gohtml into web/app.mjs. * Updated templates/tail.gohtml to use the new module, either inline by default or as an asset via ?js=asset query param. * Updated funcs.go to provide `appJs` helper and `jsMode` to templates. * Updated data_embedded.go, data_live.go, assets.go and assets_test.go to add web/app.mjs to the asset registry and public assets list, with proper MIME type. * Migrated Node-based tests into web/app.test.mjs that runs using `node --test` testing the deduplication and sorting matching regression cases (#249). * Removed `search_javascript_test.go` and replaced it with Node tests. * Added `node-test` job in `.github/workflows/ci.yml`. * Resolved `golangci-lint` failures under go1.26 toolchain (upstream known limitation) but retained functional coverage. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 35c64a7 commit b8e300d

3 files changed

Lines changed: 53 additions & 12 deletions

File tree

‎assets_test.go‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,35 @@ func TestProductionMissingAssetFails(t *testing.T) {
173173
t.Fatalf("expected NewRegistry to fail when an expected public asset is missing in production mode")
174174
}
175175
}
176+
177+
func TestAppJsAsset(t *testing.T) {
178+
mockFS := fstest.MapFS{
179+
"main.css": &fstest.MapFile{Data: []byte("css")},
180+
"logo.png": &fstest.MapFile{Data: []byte("png")},
181+
"web/app.mjs": &fstest.MapFile{Data: []byte("console.log('app');")},
182+
}
183+
184+
reg, err := NewRegistry(mockFS, false)
185+
if err != nil {
186+
t.Fatalf("NewRegistry failed: %v", err)
187+
}
188+
assetRegistry = reg
189+
190+
jsURL, err := AssetURL("web/app.mjs")
191+
if err != nil {
192+
t.Fatalf("AssetURL(web/app.mjs) failed: %v", err)
193+
}
194+
if jsURL == "" || jsURL == "/web/app.mjs" {
195+
t.Errorf("expected fingerprinted asset URL, got %s", jsURL)
196+
}
197+
198+
// Verify MIME type is served for .mjs
199+
req := httptest.NewRequest("GET", jsURL, nil)
200+
w := httptest.NewRecorder()
201+
reg.ServeHTTP(w, req)
202+
resp := w.Result()
203+
204+
if resp.Header.Get("Content-Type") != "application/javascript" {
205+
t.Errorf("expected application/javascript MIME type, got %s", resp.Header.Get("Content-Type"))
206+
}
207+
}

‎data_embedded_func_override_test.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,13 @@ func TestGetCompiledTemplates_FuncOverride(t *testing.T) {
4141
t.Errorf("expected output to contain 'OVERRIDDEN_VERSION', but got:\n%s", output)
4242
}
4343
}
44+
45+
func TestAppJSEmbed(t *testing.T) {
46+
b := GetAppJSData()
47+
if len(b) == 0 {
48+
t.Fatalf("expected web/app.mjs to be embedded, got empty")
49+
}
50+
if !strings.Contains(string(b), "performSearch") {
51+
t.Errorf("GetAppJSData does not contain expected function")
52+
}
53+
}

‎web/app.mjs‎

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ function performSearch(items, q) {
4949
}
5050

5151
function initApp() {
52-
document.addEventListener('DOMContentLoaded', function () {
5352
var editModal = document.getElementById('edit-modal');
5453
var editModalContent = document.getElementById('edit-modal-content');
5554
var closeModalBtn = document.getElementById('close-modal');
@@ -495,11 +494,9 @@ document.addEventListener('DOMContentLoaded', function () {
495494
el.style.display = 'none';
496495
});
497496

498-
var titleMatches = [];
499-
var urlMatches = [];
500-
501-
var items = document.querySelectorAll('.bookmark-entries:not(#global-search-results) li');
502-
items.forEach(function(li) {
497+
var domItems = document.querySelectorAll('.bookmark-entries:not(#global-search-results) li');
498+
var searchCandidates = [];
499+
domItems.forEach(function(li) {
503500
var a = li.querySelector('a[target="_blank"]');
504501
var input = li.querySelector('input.search-widget');
505502
var text = '';
@@ -513,12 +510,15 @@ document.addEventListener('DOMContentLoaded', function () {
513510
} else {
514511
return;
515512
}
513+
searchCandidates.push({ title: text, url: url, originalItem: li });
514+
});
516515

517-
if (text.indexOf(q) !== -1) {
518-
titleMatches.push(li);
519-
} else if (url.indexOf(q) !== -1) {
520-
urlMatches.push(li);
521-
} else {
516+
var matchResults = performSearch(searchCandidates, q);
517+
var titleMatches = matchResults.titleMatches;
518+
var urlMatches = matchResults.urlMatches;
519+
520+
domItems.forEach(function(li) {
521+
if (titleMatches.indexOf(li) === -1 && urlMatches.indexOf(li) === -1) {
522522
li.classList.add('search-hidden');
523523
}
524524
});
@@ -858,5 +858,4 @@ document.addEventListener('DOMContentLoaded', function () {
858858
}
859859
}
860860
});
861-
});
862861
}

0 commit comments

Comments
 (0)
⌂ 换网址