Skip to content

Commit 57e8edb

Browse files
committed
feat: redirect subpaths
See: go-vikunja/vikunja#655 (comment)
1 parent ae38a9d commit 57e8edb

File tree

1 file changed

+39
-8
lines changed

1 file changed

+39
-8
lines changed

src/index.ts

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,59 @@ export default {
3636
async fetch(request: Request): Promise<Response> {
3737
const url = new URL(request.url);
3838
const path = url.pathname;
39-
40-
// Check if this is a repository path
39+
// Split the pathname by '/' and remove empty items (leading slash, etc.)
4140
const pathParts = path.split('/').filter(Boolean);
4241

42+
// -------------------------------------------------------------------
43+
// 1) Handle single-segment paths that correspond to "go-get" repos.
44+
//
45+
// That is, if someone does: `go get code.vikunja.io/vikunja`
46+
// or `go get code.vikunja.io/goget`, we serve the meta tags
47+
// so that "go get" knows which Git repo to use.
48+
// -------------------------------------------------------------------
4349
if (pathParts.length === 1) {
4450
const repo = pathParts[0];
4551

4652
// Check if this is one of our special repositories
4753
if (['goget', 'web', 'vikunja'].includes(repo)) {
48-
return showGoGetMeta(path);
54+
return showGoGetMeta(`/${repo}`);
4955
}
5056
}
5157

52-
if (path === '/desktop' || path === '/desktop/') {
53-
return redirectToBase('/vikunja/tree/main/desktop');
58+
// -------------------------------------------------------------------
59+
// 2) Handle /desktop and all its subpaths.
60+
//
61+
// Examples:
62+
// - /desktop => https://github.com/go-vikunja/vikunja/tree/main/desktop
63+
// - /desktop/main.js => https://github.com/go-vikunja/vikunja/tree/main/desktop/main.js
64+
// -------------------------------------------------------------------
65+
if (pathParts[0] === 'desktop') {
66+
const subPath = pathParts.slice(1).join('/');
67+
// If subPath is not empty, prepend '/'
68+
const maybeSlash = subPath ? `/${subPath}` : '';
69+
return redirectToBase(`/vikunja/tree/main/desktop${maybeSlash}`);
5470
}
5571

56-
if (path === '/frontend' || path === '/frontend/') {
57-
return redirectToBase('/vikunja/tree/main/frontend');
72+
// -------------------------------------------------------------------
73+
// 3) Handle /frontend and all its subpaths.
74+
//
75+
// Examples:
76+
// - /frontend => https://github.com/go-vikunja/vikunja/tree/main/frontend
77+
// - /frontend/lang/i18n => https://github.com/go-vikunja/vikunja/tree/main/frontend/lang/i18n
78+
// -------------------------------------------------------------------
79+
if (pathParts[0] === 'frontend') {
80+
const subPath = pathParts.slice(1).join('/');
81+
const maybeSlash = subPath ? `/${subPath}` : '';
82+
return redirectToBase(`/vikunja/tree/main/frontend${maybeSlash}`);
5883
}
5984

60-
// Default: redirect to base URL
85+
// -------------------------------------------------------------------
86+
// 4) Default fallback:
87+
// If nothing above applies, just redirect to the base URL
88+
// with the entire path appended. This ensures that any other
89+
// repos or subpaths which don't need special handling
90+
// still redirect to the correct GitHub path.
91+
// -------------------------------------------------------------------
6192
return redirectToBase(path);
6293
},
6394
} satisfies ExportedHandler;

0 commit comments

Comments
 (0)