Skip to content

Commit 7e67a25

Browse files
committed
fix(persona): fix selfhost app baseURL
1 parent a7da04d commit 7e67a25

File tree

10 files changed

+69
-51
lines changed

10 files changed

+69
-51
lines changed

packages/persona/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
},
3636
"main": "./dist/module.cjs",
3737
"module": "./dist/module.mjs",
38-
"types": "./dist/types.d.ts",
38+
"types": "./dist/module.d.ts",
3939
"typesVersions": {
4040
"*": {
4141
"core": [
@@ -95,10 +95,12 @@
9595
"lodash-es": "4.17.21",
9696
"marked": "14.1.2",
9797
"nanodelay": "2.0.2",
98+
"pathe": "^1.1.2",
9899
"pdfjs-dist": "4.6.82",
99100
"scroll-into-view": "1.16.2",
100101
"tabbable": "6.2.0",
101102
"tiptap-extension-font-size": "^1.2.0",
103+
"ufo": "^1.5.4",
102104
"vue-collapsed": "1.3.4",
103105
"vue-demi": "0.14.10",
104106
"vuedraggable": "next",

packages/persona/playground/app.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<div class="flex h-full min-h-screen">
1111
<p-sidebar-menu :menus="menus" />
1212

13-
<div>
13+
<div class="flex-grow">
1414
<div class="bg-default">
1515
<NuxtLayout>
1616
<NuxtPage />
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
import PersonaModule from '../src/module'
21
import { fileURLToPath } from 'node:url'
32
import { defineNuxtConfig } from 'nuxt/config'
43

54
export default defineNuxtConfig({
6-
modules : ['@nuxtjs/tailwindcss', PersonaModule],
7-
alias : { '@privyid/persona': fileURLToPath(new URL('../dist/', import.meta.url)) },
8-
typescript: {
5+
compatibilityDate: '2024-10-03',
6+
modules : ['@nuxtjs/tailwindcss', '../src/module'],
7+
alias : { '@privyid/persona': fileURLToPath(new URL('../dist/', import.meta.url)) },
8+
typescript : {
99
tsConfig: {
1010
compilerOptions: {
1111
strict : false,
1212
strictNullChecks: true,
1313
},
1414
},
1515
},
16+
persona: { useLocalPdfWorker: true },
1617
})
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
<template>
22
<h1>Dashboard</h1>
3+
<div class="w-full">
4+
<p-pdf-viewer :src="PDF_URL" />
5+
</div>
36
</template>
7+
8+
<script setup lang="ts">
9+
import PDF_URL from '../../../../src/public/assets/pdf/Calibrator-v3.pdf?url'
10+
</script>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "./.nuxt/tsconfig.json"
3+
}

packages/persona/src/module.ts

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import {
44
addPlugin,
55
addComponentsDir,
66
extendViteConfig,
7-
addPluginTemplate,
87
} from '@nuxt/kit'
9-
import path from 'pathe'
8+
import { join as joinPath } from 'pathe'
9+
import { defu } from 'defu'
10+
import { joinURL } from 'ufo'
11+
import { version as PDFJS_VERSION } from 'pdfjs-dist/legacy/build/pdf.mjs'
1012

1113
export interface ModuleOptions {
1214
/**
@@ -26,19 +28,25 @@ export interface ModuleOptions {
2628
useLocalPdfWorker?: boolean,
2729
}
2830

31+
export interface ModulePublicRuntimeConfig {
32+
persona: {
33+
cdnURL: string,
34+
},
35+
}
36+
2937
export default defineNuxtModule<ModuleOptions>({
3038
meta: {
3139
name : '@privyid/persona',
3240
configKey : 'persona',
33-
compatibility: { nuxt: '>=3.0.0' },
41+
compatibility: { nuxt: '>=3.4.0' },
3442
},
3543
defaults: {
3644
font : true,
3745
prefix : 'p',
3846
useLocalPdfWorker: false,
3947
},
4048
async setup (options, nuxt) {
41-
const { resolve } = createResolver(import.meta.url)
49+
const { resolve, resolvePath } = createResolver(import.meta.url)
4250

4351
// Add font CDN
4452
if (options.font) {
@@ -62,7 +70,7 @@ export default defineNuxtModule<ModuleOptions>({
6270
})
6371

6472
// Add Plugin
65-
addPlugin({ src: resolve('./runtime/plugin') })
73+
addPlugin({ src: resolve('./runtime/plugins/persona') })
6674

6775
// Extend vite config
6876
extendViteConfig((config) => {
@@ -82,45 +90,37 @@ export default defineNuxtModule<ModuleOptions>({
8290

8391
// Use local pdf worker
8492
if (options.useLocalPdfWorker) {
93+
nuxt.options.runtimeConfig.public.persona = defu(
94+
{ cdnURL: './_persona' },
95+
nuxt.options.runtimeConfig.public.persona,
96+
)
97+
8598
nuxt.hook('nitro:config', async (nitroConfig) => {
8699
// eslint-disable-next-line align-assignments/align-assignments
87100
nitroConfig.publicAssets ||= []
88101

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
102+
const pdfjsDir = joinPath(await resolvePath('pdfjs-dist'), '../../')
103+
const pdfjsBaseURL = `_persona/pdfjs-dist@${PDFJS_VERSION}`
104+
const assetsMaxAge = 60 * 60 * 24 * 30 // 1 month
95105

96106
nitroConfig.publicAssets.push(
97107
{
98-
baseURL: `${pdfjsBaseUrl}/build`,
99-
dir : pdfjsDir,
108+
baseURL: joinURL(pdfjsDir, 'build'),
109+
dir : joinPath(pdfjsDir, 'build'),
100110
maxAge : assetsMaxAge,
101111
},
102112
{
103-
baseURL: `${pdfjsBaseUrl}/cmaps`,
104-
dir : path.join(pdfjsDir, '../cmaps'),
113+
baseURL: joinURL(pdfjsBaseURL, 'legacy'),
114+
dir : joinPath(pdfjsDir, 'legacy'),
115+
maxAge : assetsMaxAge,
116+
},
117+
{
118+
baseURL: joinURL(pdfjsBaseURL, 'cmaps'),
119+
dir : joinPath(pdfjsDir, 'cmaps'),
105120
maxAge : assetsMaxAge,
106121
},
107122
)
108123
})
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-
})
124124
}
125125
},
126126
})

packages/persona/src/runtime/.gitkeep

Whitespace-only changes.

packages/persona/src/runtime/plugin.ts renamed to packages/persona/src/runtime/plugins/persona.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1-
import { defineNuxtPlugin, useRouter } from '#imports'
1+
import {
2+
defineNuxtPlugin,
3+
useRouter,
4+
useRuntimeConfig,
5+
} from '#imports'
26
import type { State } from '@privyid/persona/core'
37
import {
48
initStore,
59
installRouter,
10+
setCDN,
611
} from '@privyid/persona/core'
12+
import { joinURL, isRelative } from 'ufo'
713

814
export default defineNuxtPlugin({
9-
name : 'persona-setup',
15+
name : 'persona-plugin',
1016
setup: (nuxtApp) => {
1117
const router = useRouter()
1218
const store = initStore()
19+
const config = useRuntimeConfig()
1320

1421
installRouter({
1522
getURL () {
@@ -20,8 +27,16 @@ export default defineNuxtPlugin({
2027
},
2128
})
2229

30+
// Set CDN to self host
31+
if (config.public.persona.cdnURL) {
32+
if (isRelative(config.public.persona.cdnURL))
33+
setCDN(joinURL(config.app.baseURL, '_persona'))
34+
else
35+
setCDN(config.public.persona.cdnURL)
36+
}
37+
2338
// SSR Store & Hydrate
24-
if (process.server)
39+
if (import.meta.server)
2540
nuxtApp.payload.persona = store.value
2641
else if (nuxtApp.payload?.persona)
2742
store.value = nuxtApp.payload.persona as State

packages/persona/tsconfig.json

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
11
{
22
"extends": "./playground/.nuxt/tsconfig.json",
3-
"include": [
4-
"src/**/*.ts",
5-
"src/**/*.d.ts",
6-
"src/**/*.tsx",
7-
"src/**/*.vue",
8-
"script/*.ts",
9-
"playground/**/*.ts",
10-
"playground/**/*.d.ts",
11-
"playground/**/*.tsx",
12-
"playground/**/*.vue",
13-
"./build.config.ts"
14-
]
153
}

yarn.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3979,6 +3979,7 @@ __metadata:
39793979
marked: "npm:14.1.2"
39803980
nanodelay: "npm:2.0.2"
39813981
nuxt: "npm:3.13.2"
3982+
pathe: "npm:^1.1.2"
39823983
pdfjs-dist: "npm:4.6.82"
39833984
postcss-custom-properties: "npm:14.0.1"
39843985
postcss-hexrgba: "npm:2.1.0"
@@ -3987,6 +3988,7 @@ __metadata:
39873988
tabbable: "npm:6.2.0"
39883989
tailwind: "npm:4.0.0"
39893990
tiptap-extension-font-size: "npm:^1.2.0"
3991+
ufo: "npm:^1.5.4"
39903992
vue-collapsed: "npm:1.3.4"
39913993
vue-demi: "npm:0.14.10"
39923994
vuedraggable: "npm:next"

0 commit comments

Comments
 (0)