Skip to content

Commit 1b6264e

Browse files
authored
Merge pull request #1482 from privy-open-source/feat/local-pdf-worker-option
feat(pdf-worker): add option to use local PDF.js worker
2 parents e921b50 + 21ed17d commit 1b6264e

File tree

2 files changed

+74
-17
lines changed

2 files changed

+74
-17
lines changed

packages/persona/src/module.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import {
44
addPlugin,
55
addComponentsDir,
66
extendViteConfig,
7+
addPluginTemplate,
78
} from '@nuxt/kit'
9+
import path from 'pathe'
810

911
export interface ModuleOptions {
1012
/**
@@ -17,6 +19,11 @@ export interface ModuleOptions {
1719
* @default 'p'
1820
*/
1921
prefix?: string,
22+
/**
23+
* Use local pdf worker
24+
* @default false
25+
*/
26+
useLocalPdfWorker?: boolean,
2027
}
2128

2229
export default defineNuxtModule<ModuleOptions>({
@@ -25,7 +32,11 @@ export default defineNuxtModule<ModuleOptions>({
2532
configKey : 'persona',
2633
compatibility: { nuxt: '>=3.0.0' },
2734
},
28-
defaults: { font: true, prefix: 'p' },
35+
defaults: {
36+
font : true,
37+
prefix : 'p',
38+
useLocalPdfWorker: false,
39+
},
2940
async setup (options, nuxt) {
3041
const { resolve } = createResolver(import.meta.url)
3142

@@ -68,5 +79,48 @@ export default defineNuxtModule<ModuleOptions>({
6879
'vuedraggable',
6980
)
7081
})
82+
83+
// Use local pdf worker
84+
if (options.useLocalPdfWorker) {
85+
nuxt.hook('nitro:config', async (nitroConfig) => {
86+
// eslint-disable-next-line align-assignments/align-assignments
87+
nitroConfig.publicAssets ||= []
88+
89+
// eslint-disable-next-line unicorn/prefer-module
90+
const pdfjsDir = path.dirname(require.resolve('pdfjs-dist'))
91+
const { default: { version } } = await import('pdfjs-dist/package.json')
92+
const pdfjsBaseUrl = `_persona/pdjs-dist@${version}`
93+
94+
const assetsMaxAge = 60 * 60 * 24 * 30
95+
96+
nitroConfig.publicAssets.push(
97+
{
98+
baseURL: `${pdfjsBaseUrl}/build`,
99+
dir : pdfjsDir,
100+
maxAge : assetsMaxAge,
101+
},
102+
{
103+
baseURL: `${pdfjsBaseUrl}/cmaps`,
104+
dir : path.join(pdfjsDir, '../cmaps'),
105+
maxAge : assetsMaxAge,
106+
},
107+
)
108+
})
109+
110+
addPluginTemplate({
111+
filename : 'persona-local-pdf-worker.mjs',
112+
getContents: () => `
113+
import { setCDN } from '@privyid/persona/core'
114+
115+
export default defineNuxtPlugin({
116+
name: 'persona-local-pdf-worker',
117+
dependsOn: ['persona-setup'],
118+
setup () {
119+
setCDN('/_persona/')
120+
},
121+
})
122+
`,
123+
})
124+
}
71125
},
72126
})

packages/persona/src/runtime/plugin.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@ import {
55
installRouter,
66
} from '@privyid/persona/core'
77

8-
export default defineNuxtPlugin((nuxtApp) => {
9-
const router = useRouter()
10-
const store = initStore()
8+
export default defineNuxtPlugin({
9+
name : 'persona-setup',
10+
setup: (nuxtApp) => {
11+
const router = useRouter()
12+
const store = initStore()
1113

12-
installRouter({
13-
getURL () {
14-
return router.currentRoute.value.fullPath
15-
},
16-
async toURL (url: string) {
17-
return await router.push(url)
18-
},
19-
})
14+
installRouter({
15+
getURL () {
16+
return router.currentRoute.value.fullPath
17+
},
18+
async toURL (url: string) {
19+
return await router.push(url)
20+
},
21+
})
2022

21-
// SSR Store & Hydrate
22-
if (process.server)
23-
nuxtApp.payload.persona = store.value
24-
else if (nuxtApp.payload?.persona)
25-
store.value = nuxtApp.payload.persona as State
23+
// SSR Store & Hydrate
24+
if (process.server)
25+
nuxtApp.payload.persona = store.value
26+
else if (nuxtApp.payload?.persona)
27+
store.value = nuxtApp.payload.persona as State
28+
},
2629
})

0 commit comments

Comments
 (0)