-
Notifications
You must be signed in to change notification settings - Fork 5
Support static hosting for web.runme.dev #120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5f2465a
fix(app): support subpath hosting
jlewi 310d0b6
fix(app): normalize index entry URLs
jlewi 656a963
fix(app): redirect index entry to home
jlewi 27ac3d5
feat(app): add hosted config and policy assets
jlewi 585ca8f
Use root-relative asset base for app build
jlewi 2387dd5
Add hosted app config
jlewi 2feb969
Cleanup.
jlewi f49efc7
Add static about and privacy policy pages
jlewi b029c9f
Add runtime ChatKit domain key config
jlewi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { | ||
| APP_ROUTE_PATHS, | ||
| deriveAppBasePath, | ||
| getAppPath, | ||
| getOidcCallbackUrl, | ||
| normalizeAppIndexUrl, | ||
| resolveAppUrl, | ||
| } from "./appBase"; | ||
|
|
||
| describe("deriveAppBasePath", () => { | ||
| it("uses the bundled asset location outside development", () => { | ||
| expect( | ||
| deriveAppBasePath({ | ||
| dev: false, | ||
| pathname: "/runme-dev-assets/runs", | ||
| moduleUrl: | ||
| "https://storage.googleapis.com/runme-dev-assets/index.BdN4INbO.js", | ||
| }), | ||
| ).toBe("/runme-dev-assets/"); | ||
| }); | ||
|
|
||
| it("falls back to the current document path in development", () => { | ||
| expect( | ||
| deriveAppBasePath({ | ||
| dev: true, | ||
| pathname: "/runme-dev-assets/index.html", | ||
| }), | ||
| ).toBe("/runme-dev-assets/"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("app base URL helpers", () => { | ||
| it("resolves app-relative paths beneath the mounted base path", () => { | ||
| window.history.replaceState(null, "", "/runme-dev-assets/index.html"); | ||
| const origin = window.location.origin; | ||
|
|
||
| expect(getAppPath(APP_ROUTE_PATHS.oidcCallback)).toBe( | ||
| "/runme-dev-assets/oidc/callback", | ||
| ); | ||
| expect(resolveAppUrl("configs/app-configs.yaml").toString()).toBe( | ||
| `${origin}/runme-dev-assets/configs/app-configs.yaml`, | ||
| ); | ||
| expect(getOidcCallbackUrl()).toBe( | ||
| `${origin}/runme-dev-assets/oidc/callback`, | ||
| ); | ||
| }); | ||
|
|
||
| it("normalizes index.html entry URLs to the directory path", () => { | ||
| window.history.replaceState( | ||
| null, | ||
| "", | ||
| "/runme-dev-assets/index.html?doc=foo#section-1", | ||
| ); | ||
|
|
||
| normalizeAppIndexUrl(); | ||
|
|
||
| expect(window.location.pathname).toBe("/runme-dev-assets/"); | ||
| expect(window.location.search).toBe("?doc=foo"); | ||
| expect(window.location.hash).toBe("#section-1"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| const ROOT_PATH = "/"; | ||
|
|
||
| export const APP_ROUTE_PATHS = { | ||
| home: "/", | ||
| indexEntry: "/index.html", | ||
| authStatus: "/auth/status", | ||
| oidcCallback: "/oidc/callback", | ||
| runs: "/runs", | ||
| run: (runName: string) => `/runs/${runName}`, | ||
| editRun: (runName: string) => `/runs/${runName}/edit`, | ||
| } as const; | ||
|
|
||
| function ensureTrailingSlash(pathname: string): string { | ||
| if (!pathname || pathname === ROOT_PATH) { | ||
| return ROOT_PATH; | ||
| } | ||
| return pathname.endsWith("/") ? pathname : `${pathname}/`; | ||
| } | ||
|
|
||
| function stripTrailingSlash(pathname: string): string { | ||
| if (!pathname || pathname === ROOT_PATH) { | ||
| return ROOT_PATH; | ||
| } | ||
| return pathname.endsWith("/") ? pathname.slice(0, -1) : pathname; | ||
| } | ||
|
|
||
| function getBasePathFromModuleUrl(moduleUrl: string): string { | ||
| const url = new URL(moduleUrl); | ||
| if (!["http:", "https:"].includes(url.protocol)) { | ||
| return ROOT_PATH; | ||
| } | ||
| return ensureTrailingSlash(new URL(".", url).pathname); | ||
| } | ||
|
|
||
| function getBasePathFromLocation(pathname: string): string { | ||
| if (!pathname || pathname === ROOT_PATH) { | ||
| return ROOT_PATH; | ||
| } | ||
|
|
||
| const lastSlashIndex = pathname.lastIndexOf("/"); | ||
| const lastSegment = pathname.slice(lastSlashIndex + 1); | ||
| if (lastSegment === "index.html") { | ||
| return ensureTrailingSlash(pathname.slice(0, -lastSegment.length)); | ||
| } | ||
| if (lastSegment.includes(".")) { | ||
| return ensureTrailingSlash(pathname.slice(0, lastSlashIndex + 1)); | ||
| } | ||
| return ROOT_PATH; | ||
| } | ||
|
|
||
| export function deriveAppBasePath(options?: { | ||
| dev?: boolean; | ||
| moduleUrl?: string; | ||
| pathname?: string; | ||
| }): string { | ||
| if (options?.dev) { | ||
| return getBasePathFromLocation(options.pathname ?? ROOT_PATH); | ||
| } | ||
| if (options?.moduleUrl) { | ||
| const fromModuleUrl = getBasePathFromModuleUrl(options.moduleUrl); | ||
| if (fromModuleUrl !== ROOT_PATH) { | ||
| return fromModuleUrl; | ||
| } | ||
| } | ||
| return getBasePathFromLocation(options?.pathname ?? ROOT_PATH); | ||
| } | ||
|
|
||
| let cachedAppBasePath: string | null = null; | ||
|
|
||
| export function getAppBasePath(): string { | ||
| if (cachedAppBasePath) { | ||
| return cachedAppBasePath; | ||
| } | ||
|
|
||
| if (typeof window === "undefined") { | ||
| return ROOT_PATH; | ||
| } | ||
|
|
||
| cachedAppBasePath = deriveAppBasePath({ | ||
| dev: import.meta.env.DEV, | ||
| moduleUrl: import.meta.url, | ||
| pathname: window.location.pathname, | ||
| }); | ||
| return cachedAppBasePath; | ||
| } | ||
|
|
||
| export function getAppRouterBasename(): string { | ||
| return stripTrailingSlash(getAppBasePath()); | ||
| } | ||
|
|
||
| export function resolveAppUrl(path = ""): URL { | ||
| if (typeof window === "undefined") { | ||
| return new URL(path || ROOT_PATH, "http://localhost"); | ||
| } | ||
| const baseUrl = new URL(getAppBasePath(), window.location.origin); | ||
| const normalizedPath = path.replace(/^\/+/, ""); | ||
| return new URL(normalizedPath, baseUrl); | ||
| } | ||
|
|
||
| export function getAppPath(path = ""): string { | ||
| const url = resolveAppUrl(path); | ||
| return `${url.pathname}${url.search}${url.hash}`; | ||
| } | ||
|
|
||
| export function getOidcCallbackUrl(): string { | ||
| return resolveAppUrl(APP_ROUTE_PATHS.oidcCallback).toString(); | ||
| } | ||
|
|
||
| export function normalizeAppIndexUrl(): void { | ||
| if (typeof window === "undefined") { | ||
| return; | ||
| } | ||
|
|
||
| const { pathname, search, hash } = window.location; | ||
| if (!pathname.endsWith("/index.html")) { | ||
| return; | ||
| } | ||
|
|
||
| const normalizedPath = pathname.slice(0, -"/index.html".length) || ROOT_PATH; | ||
| window.history.replaceState(null, "", `${ensureTrailingSlash(normalizedPath)}${search}${hash}`); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.