From 563a135c41b588008ef03c14799cda43cdd6a0b5 Mon Sep 17 00:00:00 2001 From: Eric Guan Date: Tue, 7 Nov 2023 10:31:16 -0800 Subject: [PATCH] feat: check for custom sw install path --- src/widget/widget-config.js | 2 ++ src/widget/widget.js | 14 ++++++++------ test/widget-config.spec.js | 24 ++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 test/widget-config.spec.js diff --git a/src/widget/widget-config.js b/src/widget/widget-config.js index f9c3bca..64202de 100644 --- a/src/widget/widget-config.js +++ b/src/widget/widget-config.js @@ -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) @@ -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 } diff --git a/src/widget/widget.js b/src/widget/widget.js index d2c78d0..d69a049 100644 --- a/src/widget/widget.js +++ b/src/widget/widget.js @@ -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( @@ -55,7 +58,8 @@ function initWidget () { } const config = widgetConfigFromScriptTag() - const clientKey = config.clientKey + config.clientId = getRetrievalClientId() + addHeadElement('link', { href: process.env.L1_ORIGIN, crossOrigin: '', @@ -63,9 +67,7 @@ function initWidget () { id: 'saturn-preconnect' }) - const clientId = getRetrievalClientId() - const conf = { clientId, clientKey } - installSw(conf) + installSw(config) } initWidget() diff --git a/test/widget-config.spec.js b/test/widget-config.spec.js new file mode 100644 index 0000000..5f84c6a --- /dev/null +++ b/test/widget-config.spec.js @@ -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) + }) +})