Skip to content

Commit

Permalink
fix: merge serviceworkers for seedless onboarding (#2750)
Browse files Browse the repository at this point in the history
Side effect: The redirect URI is changed from /serviceworker/redirect to /redirect
  • Loading branch information
schmanu authored Nov 7, 2023
1 parent 232674c commit 8e2d324
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 28 deletions.
2 changes: 1 addition & 1 deletion next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const nextConfig = {
dirs: ['src'],
},
experimental: {
optimizePackageImports: ['@mui/material', '@mui/icons-material', 'lodash', 'date-fns']
optimizePackageImports: ['@mui/material', '@mui/icons-material', 'lodash', 'date-fns'],
},
webpack(config) {
config.module.rules.push({
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/wallets/mpc/useMPC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const useInitMPC = () => {
web3AuthClientId: SOCIAL_WALLET_OPTIONS.web3AuthClientId,
// Available networks are "sapphire_devnet", "sapphire_mainnet"
web3AuthNetwork: WEB3AUTH_NETWORK.MAINNET,
baseUrl: `${window.location.origin}/serviceworker`,
baseUrl: `${window.location.origin}/`,
uxMode: 'popup',
enableLogging: true,
chainConfig,
Expand Down
3 changes: 3 additions & 0 deletions src/service-workers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
/// <reference lib="webworker" />

import { firebaseMessagingSw } from './firebase-messaging/firebase-messaging-sw'
import { mpcCoreKitServiceWorker } from './mpc-core-kit-sw'

firebaseMessagingSw()

mpcCoreKitServiceWorker()
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
/* eslint-disable */
function getScope() {
return self.registration.scope
}
/// <reference lib="webworker" />

declare const self: ServiceWorkerGlobalScope

self.addEventListener('message', function (event) {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting()
export const mpcCoreKitServiceWorker = () => {
/* eslint-disable */
function getScope() {
return self.registration.scope
}
})

self.addEventListener('fetch', function (event) {
try {
const url = new URL(event.request.url)
const redirectURL = self.location.url
if (url.pathname.includes('redirect') && url.href.includes(getScope())) {
event.respondWith(
new Response(
new Blob(
[
`
self.addEventListener('message', function (event) {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting()
}
})

self.addEventListener('fetch', function (event) {
try {
const url = new URL(event.request.url)
//@ts-expect-error
const redirectURL = self.location.url
if (url.pathname.includes('redirect') && url.href.includes(getScope())) {
event.respondWith(
new Response(
new Blob(
[
`
<!DOCTYPE html>
<html lang="en">
<head>
Expand Down Expand Up @@ -306,13 +312,14 @@ self.addEventListener('fetch', function (event) {
${''}
`,
],
{ type: 'text/html' },
],
{ type: 'text/html' },
),
),
),
)
)
}
} catch (error) {
console.error(error)
}
} catch (error) {
console.error(error)
}
})
})
}

0 comments on commit 8e2d324

Please sign in to comment.