Skip to content

Commit b6d5d46

Browse files
committed
fix: isolate instance on ssr
1 parent 92ab11f commit b6d5d46

12 files changed

Lines changed: 1849 additions & 4497 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,5 @@ dist
129129
.yarn/build-state.yml
130130
.yarn/install-state.gz
131131
.pnp.*
132+
133+
.clinic/

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"coverage": "vitest run src --coverage --config ./vitest.config.ts"
4848
},
4949
"dependencies": {
50-
"@nuxt/kit": "^3.12.4",
50+
"@nuxt/kit": "^3.13.2",
5151
"axios": "^1.7.2",
5252
"defu": "^6.1.4",
5353
"ofetch": "^1.3.4",
@@ -62,7 +62,7 @@
6262
"@nuxt/schema": "3.13.2",
6363
"@nuxt/test-utils": "3.14.2",
6464
"@privyid/eslint-config-persona": "0.27.0",
65-
"@privyid/nhp": "1.0.0",
65+
"@privyid/nhp": "1.0.1",
6666
"@typescript-eslint/eslint-plugin": "5.62.0",
6767
"@typescript-eslint/parser": "5.62.0",
6868
"@vitest/coverage-v8": "^2.0.5",
@@ -85,5 +85,9 @@
8585
},
8686
"publishConfig": {
8787
"access": "public"
88+
},
89+
"resolutions": {
90+
"http-proxy": "npm:http-proxy-node16@1.0.5",
91+
"rollup": "npm:@rollup/wasm-node@*"
8892
}
8993
}

playground/pages/demo.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<template>
2+
<h1>
3+
Heeeelllo Wooorrrlllddd !!!!
4+
</h1>
5+
</template>

playground/plugins/api.server.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

playground/plugins/api.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { defineNuxtPlugin } from '#app'
2+
import { onRequest } from '@privyid/nuapi/core'
3+
4+
export default defineNuxtPlugin({
5+
dependsOn: ['nuapi:plugin'],
6+
setup () {
7+
onRequest((config) => {
8+
if (!config.headers.Authorization)
9+
config.headers.Authorization = 'Bearer 123456'
10+
11+
return config
12+
})
13+
},
14+
})

playground/server/routes/gc.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { defineEventHandler } from 'h3'
2+
import process from 'node:process'
3+
4+
export default defineEventHandler(() => {
5+
if (!global.gc) {
6+
return {
7+
code : 400,
8+
message: 'GC not available',
9+
}
10+
}
11+
12+
const before = process.memoryUsage()
13+
14+
global.gc()
15+
16+
const after = process.memoryUsage()
17+
const diff = {
18+
rss : before.rss - after.rss,
19+
heapTotal : before.heapTotal - after.heapTotal,
20+
heapUsed : before.heapUsed - after.heapUsed,
21+
external : before.external - after.external,
22+
arrayBuffers: before.arrayBuffers - after.arrayBuffers,
23+
}
24+
25+
return {
26+
code : 200,
27+
message: 'Memory clerer',
28+
data : {
29+
before,
30+
after,
31+
diff,
32+
},
33+
}
34+
})

playground/server/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "extends": "../.nuxt/tsconfig.server.json" }

src/core/global.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
import { createLazyton } from './instance'
2+
import type { ApiResolver } from './types'
3+
4+
let $resolver: ApiResolver
5+
6+
export function setResolver (resolver: ApiResolver) {
7+
$resolver = resolver
8+
}
9+
10+
const getApi = createLazyton({}, true)
11+
12+
/**
13+
* Set global api instance
14+
* @param instance
15+
*/
16+
export const setApi = getApi.setApi
217

318
/**
419
* Use global api instance
@@ -7,10 +22,8 @@ import { createLazyton } from './instance'
722
*
823
* api.get('/some/endpoint')
924
*/
10-
export const useApi = createLazyton({}, true)
11-
12-
/**
13-
* Set global api instance
14-
* @param instance
15-
*/
16-
export const setApi = useApi.setApi
25+
export const useApi = () => {
26+
return $resolver
27+
? $resolver()
28+
: getApi()
29+
}

src/core/instance.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@ function toValue<T extends object = any> (value: T | (() => T)) {
2828
* useApi().get('/url/endpoint/')
2929
*/
3030
export function createLazyton (options: ApiConfig | (() => ApiConfig) = {}, fresh = false): LazyInstance {
31-
let api: ApiInstance
31+
let $api: ApiInstance
3232

3333
const getApi = function () {
34-
if (!api) {
35-
api = fresh
34+
if (!$api) {
35+
$api = fresh
3636
? createApi(toValue(options))
3737
: useApi().create(toValue(options))
3838
}
3939

40-
return api
40+
return $api
4141
}
4242

4343
const setApi = function (instance: ApiInstance) {
44-
api = instance
44+
$api = instance
4545
}
4646

4747
return Object.assign(getApi, { setApi })

src/core/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,4 @@ export interface ApiInstance extends AxiosInstance {
107107
create: (this: ApiInstance, config?: ApiConfig) => ApiInstance,
108108
}
109109

110-
export type ApiResolver = () => ApiInstance | undefined
110+
export type ApiResolver = () => ApiInstance

0 commit comments

Comments
 (0)