Skip to content

Commit 8ddd5e1

Browse files
committed
fix: use dynamic imports for @e4a/pg-js to support SSR
pg-js bundles WASM and browser APIs that can't load during SSR. Use lazy getPostGuard() and dynamic import() for error classes so the module is only loaded in the browser.
1 parent d8c83e2 commit 8ddd5e1

5 files changed

Lines changed: 28 additions & 14 deletions

File tree

src/lib/components/fallback/Decrypt.svelte

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
1616
import { locale, _ } from 'svelte-i18n'
1717
18-
import { pg } from '$lib/postguard'
19-
import { IdentityMismatchError } from '@e4a/pg-js'
18+
import { getPostGuard } from '$lib/postguard'
2019
2120
import YiviQRCode from '$lib/components/filesharing/YiviQRCode.svelte'
2221
import Chip from '$lib/components/Chip.svelte'
@@ -129,9 +128,11 @@
129128
130129
run(() => {
131130
if (decryptState === STATES.Uninit && readable) {
131+
getPostGuard().then((pg) => {
132132
const o = pg.open({ data: readable })
133133
opened = o
134-
o.inspect()
134+
return o.inspect()
135+
})
135136
.then((info) => {
136137
decryptState = STATES.Init
137138
policies = info.policies

src/lib/components/filesharing/SendButton.svelte

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
<script lang="ts">
22
import { _, locale } from 'svelte-i18n'
33
import { isValidPhoneNumber } from 'libphonenumber-js/mobile'
4-
import { NetworkError } from '@e4a/pg-js'
54
65
import yiviLogoDark from '$lib/assets/images/non-free/yivi-logo-dark.svg'
76
import { EncryptionState, type EncryptState } from '$lib/types/filesharing/attributes'
8-
import { pg } from '$lib/postguard'
7+
import { getPostGuard } from '$lib/postguard'
98
import { browser } from '$app/environment'
109
import { isMobile } from '$lib/browser-detect'
1110
import YiviQRCode from './YiviQRCode.svelte'
@@ -92,6 +91,8 @@
9291
try {
9392
if (!canEncrypt()) return
9493
94+
const pg = await getPostGuard()
95+
9596
// Build recipients
9697
const recipients = EncryptState.recipients.map(({ email, extra }) => {
9798
if (extra.length > 0) {
@@ -146,6 +147,7 @@
146147
EncryptState.selfAborted = false
147148
EncryptState.encryptStartTime = 0
148149
} else {
150+
const { NetworkError } = await import('@e4a/pg-js')
149151
EncryptState.serverError = e instanceof NetworkError && e.status >= 500
150152
EncryptState.encryptionState = EncryptionState.Error
151153
}

src/lib/postguard.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
import { PostGuard } from '@e4a/pg-js'
2-
import { PKG_URL, FILEHOST_URL } from '$lib/env'
1+
import type { PostGuard as PostGuardType } from '@e4a/pg-js'
32

4-
export const pg = new PostGuard({
5-
pkgUrl: PKG_URL,
6-
cryptifyUrl: FILEHOST_URL,
7-
})
3+
let instance: PostGuardType | null = null
4+
5+
export async function getPostGuard(): Promise<PostGuardType> {
6+
if (!instance) {
7+
const { PostGuard } = await import('@e4a/pg-js')
8+
const { PKG_URL, FILEHOST_URL } = await import('$lib/env')
9+
instance = new PostGuard({
10+
pkgUrl: PKG_URL,
11+
cryptifyUrl: FILEHOST_URL,
12+
})
13+
}
14+
return instance
15+
}

src/routes/debug/qr/+page.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<script lang="ts">
22
import { onMount } from 'svelte'
33
import YiviQRCode from '$lib/components/filesharing/YiviQRCode.svelte'
4-
import { pg } from '$lib/postguard'
4+
import { getPostGuard } from '$lib/postguard'
55
66
onMount(async () => {
77
// Initialize Yivi session for testing
88
// Note: this page just displays a QR code component for visual testing.
99
// The actual Yivi session is handled by pg-js when sign.yivi() is called.
10+
const pg = await getPostGuard()
1011
console.log('QR test page loaded. PostGuard instance:', pg)
1112
})
1213
</script>

src/routes/download/+page.svelte

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
import { onMount, tick } from 'svelte'
33
import { browser, dev } from '$app/environment'
44
import { _ } from 'svelte-i18n'
5-
import { pg } from '$lib/postguard'
6-
import { IdentityMismatchError, NetworkError } from '@e4a/pg-js'
5+
import { getPostGuard } from '$lib/postguard'
76
import type { DecryptFileResult, FriendlySender, InspectResult } from '@e4a/pg-js'
87
import YiviQRCode from '$lib/components/filesharing/YiviQRCode.svelte'
98
import FileList from '$lib/components/filesharing/FileList.svelte'
@@ -46,6 +45,7 @@
4645
async function startDownload() {
4746
downloadState = 'Downloading'
4847
try {
48+
const pg = await getPostGuard()
4949
opened = pg.open({ uuid })
5050
const info: InspectResult = await opened.inspect()
5151
@@ -55,6 +55,7 @@
5555
5656
checkRecipients(info.recipients)
5757
} catch (e) {
58+
const { NetworkError } = await import('@e4a/pg-js')
5859
if (e instanceof NetworkError && e.status >= 500) {
5960
downloadState = 'ServerError'
6061
} else {
@@ -107,6 +108,7 @@
107108
} catch (e) {
108109
if (dev) console.error('[download] decrypt error:', e)
109110
err = String(e)
111+
const { IdentityMismatchError, NetworkError } = await import('@e4a/pg-js')
110112
if (e instanceof IdentityMismatchError) {
111113
downloadState = 'IdentityMismatch'
112114
} else if (e instanceof NetworkError && e.status >= 500) {

0 commit comments

Comments
 (0)