Skip to content

Commit

Permalink
feat: check for custom sw install path
Browse files Browse the repository at this point in the history
  • Loading branch information
guanzo committed Nov 7, 2023
1 parent cd3b0c2 commit 563a135
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/widget/widget-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
export const WIDGET_BASE_URL = `${process.env.STATIC_FILE_ORIGIN}/widget.js`

const CLIENT_KEY_IDENTIFIER = 'integration'
const INSTALL_PATH_KEY = 'installPath'

export function isWidgetUrl (url) {
const { href } = new URL(url)
Expand All @@ -14,6 +15,7 @@ function getConf (urlObj, conf = {}) {
const [_, queryStr] = urlObj.href.split(/#|[?]/)
const searchParams = new URLSearchParams(queryStr)
conf.clientKey = searchParams.get(CLIENT_KEY_IDENTIFIER)
conf.installPath = searchParams.get(INSTALL_PATH_KEY) ?? '/'
return conf
}

Expand Down
14 changes: 8 additions & 6 deletions src/widget/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ const MDN_SW_DOCS_URL = 'https://developer.mozilla.org/en-US/docs/Web' +
'#Why_is_my_service_worker_failing_to_register'

async function installSw (conf) {
const { clientId, clientKey } = conf
const { clientId, clientKey, installPath } = conf
try {
const path = `${SW_PATH}?clientId=${clientId}&clientKey=${clientKey}`
let path = `${SW_PATH}?clientId=${clientId}&clientKey=${clientKey}`
if (installPath !== '/') {
path = installPath + path
}
await navigator.serviceWorker.register(path)
} catch (err) {
console.warn(
Expand Down Expand Up @@ -55,17 +58,16 @@ function initWidget () {
}

const config = widgetConfigFromScriptTag()
const clientKey = config.clientKey
config.clientId = getRetrievalClientId()

addHeadElement('link', {
href: process.env.L1_ORIGIN,
crossOrigin: '',
rel: 'preconnect',
id: 'saturn-preconnect'
})

const clientId = getRetrievalClientId()
const conf = { clientId, clientKey }
installSw(conf)
installSw(config)
}

initWidget()
24 changes: 24 additions & 0 deletions test/widget-config.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import assert from 'node:assert/strict'
import { describe, it } from 'node:test'
import { widgetConfigFromUrl } from '#src/widget/widget-config.js'

describe('widget-config', () => {
it('should get default config from URL', () => {
const clientKey = 'abc123'
const url = `https://portal.saturn.tech/widget.js#integration=${clientKey}`

const config = widgetConfigFromUrl(url)
assert.strictEqual(config.clientKey, clientKey)
assert.strictEqual(config.installPath, '/')
})

it('should get config from URL', () => {
const clientKey = 'abc123'
const installPath = '/test'
const url = `https://portal.saturn.tech/widget.js#integration=${clientKey}&installPath=${installPath}`

const config = widgetConfigFromUrl(url)
assert.strictEqual(config.clientKey, clientKey)
assert.strictEqual(config.installPath, installPath)
})
})

0 comments on commit 563a135

Please sign in to comment.