Fix base path
Now it should work whether you have a trailing slash or not.
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
import { field, logger } from "@coder/logger"
|
||||
import { getBasepath, navigate, setBasepath } from "hookrouter"
|
||||
import * as React from "react"
|
||||
import { Application, isExecutableApplication } from "../common/api"
|
||||
@ -14,16 +15,25 @@ interface RedirectedApplication extends Application {
|
||||
redirected?: boolean
|
||||
}
|
||||
|
||||
const origin = typeof window !== "undefined" ? window.location.origin + window.location.pathname : undefined
|
||||
|
||||
let resolved = false
|
||||
const App: React.FunctionComponent<AppProps> = (props) => {
|
||||
const [authed, setAuthed] = React.useState<boolean>(props.options.authed)
|
||||
const [app, setApp] = React.useState<RedirectedApplication | undefined>(props.options.app)
|
||||
const [error, setError] = React.useState<HttpError | Error | string>()
|
||||
|
||||
if (typeof window !== "undefined") {
|
||||
const url = new URL(origin + props.options.basePath)
|
||||
if (!resolved && typeof document !== "undefined") {
|
||||
// Get the base path. We need the full URL for connecting the web socket.
|
||||
// Use the path name plus the provided base path. For example:
|
||||
// foo.com/base + ./ => foo.com/base
|
||||
// foo.com/base/ + ./ => foo.com/base
|
||||
// foo.com/base/bar + ./ => foo.com/base
|
||||
// foo.com/base/bar/ + ./../ => foo.com/base
|
||||
const parts = window.location.pathname.replace(/^\//g, "").split("/")
|
||||
parts[parts.length - 1] = props.options.basePath
|
||||
const url = new URL(window.location.origin + "/" + parts.join("/"))
|
||||
setBasepath(normalize(url.pathname))
|
||||
logger.debug("resolved base path", field("base", getBasepath()))
|
||||
resolved = true
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
;(window as any).setAuthed = (a: boolean): void => {
|
||||
|
Reference in New Issue
Block a user