Archived
1
0

Register a service worker

To make installing as a PWA possible. Fixes #1181.
This commit is contained in:
Asher
2020-02-27 14:56:14 -06:00
parent eef2ed0e78
commit 963ebaca5b
17 changed files with 140 additions and 78 deletions

View File

@ -2,8 +2,9 @@ import { logger } from "@coder/logger"
export interface Options {
base: string
commit: string
logLevel: number
sessionId: string
sessionId?: string
}
/**
@ -25,6 +26,13 @@ export const generateUuid = (length = 24): string => {
.join("")
}
/**
* Remove extra slashes in a URL.
*/
export const normalize = (url: string, keepTrailing = false): string => {
return url.replace(/\/\/+/g, "/").replace(/\/+$/, keepTrailing ? "/" : "")
}
/**
* Get options embedded in the HTML from the server.
*/
@ -45,16 +53,15 @@ export const getOptions = <T extends Options>(): T => {
if (typeof options.logLevel !== "undefined") {
logger.level = options.logLevel
}
return options
const parts = window.location.pathname.replace(/^\//g, "").split("/")
parts[parts.length - 1] = options.base
const url = new URL(window.location.origin + "/" + parts.join("/"))
return {
...options,
base: normalize(url.pathname, true),
}
} catch (error) {
logger.warn(error.message)
return {} as T
}
}
/**
* Remove extra slashes in a URL.
*/
export const normalize = (url: string, keepTrailing = false): string => {
return url.replace(/\/\/+/g, "/").replace(/\/+$/, keepTrailing ? "/" : "")
}