Skip to content

Commit 2a6e8e4

Browse files
committed
feat: 🎸 支持 ssg 模式
1 parent bd5b8a0 commit 2a6e8e4

File tree

7 files changed

+32
-25
lines changed

7 files changed

+32
-25
lines changed

packages/model/src/clients/client-factory.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { CommonResponse } from './interceptor';
33
import type { ClientOptions, Params, FetchPolicy, RequestConfig, RestParams } from './types';
44
import type { HydrationStatus } from '../store';
55
import { ReplaySubject } from 'rxjs';
6-
import { MODE } from '../const';
6+
import { RENDER_MODE } from '../const';
77

88
import { createCache } from '../cache';
99
import { hash } from '../cache/hash';
@@ -117,14 +117,14 @@ export function clientFactory<ClientType extends 'GQL'| 'REST'>(
117117

118118
if (!opts.fetch) {
119119
// 避免Node环境下的判断,所以没法简化写=。=,因为window.fetch会触发一次RHS导致报错
120-
if (MODE === 'SPA') {
120+
if (RENDER_MODE === 'SPA') {
121121
// 小程序因为没有window,所以需要这里绕一下
122122
if (typeof window !== 'undefined' && window.fetch) {
123123
opts.fetch = (resource, options) => window.fetch(resource, options);
124124
} else {
125125
throw new Error('There is no useful "fetch" function');
126126
}
127-
} else if (MODE === 'SSR') {
127+
} else {
128128
if (globalThis.fetch) {
129129
opts.fetch = (resource, options) => globalThis.fetch(resource, options);
130130
} else {
@@ -142,7 +142,7 @@ export function clientFactory<ClientType extends 'GQL'| 'REST'>(
142142
const timeoutPromise = new Promise<DOMException | TimeoutError>((resolve) => {
143143
setTimeout(
144144
() => {
145-
if (MODE !== 'SSR' && typeof DOMException !== 'undefined') {
145+
if (RENDER_MODE === 'SPA' && typeof DOMException !== 'undefined') {
146146
resolve(new DOMException('The request has been timeout'))
147147
} else {
148148
resolve(new TimeoutError('The request has been timeout'))
@@ -155,7 +155,7 @@ export function clientFactory<ClientType extends 'GQL'| 'REST'>(
155155
}).then((res) => {
156156
// 浏览器断网情况下有可能会是null
157157
if (res === null) {
158-
res = MODE !== 'SSR' && typeof DOMException !== 'undefined'
158+
res = RENDER_MODE === 'SPA' && typeof DOMException !== 'undefined'
159159
? new DOMException('The request has been timeout')
160160
: new TimeoutError('The request has been timeout');
161161
}

packages/model/src/const.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { RebornClient } from './clients';
2-
import type { storeFactory } from './store';
2+
import type { RenderMode, storeFactory } from './store';
33

44
import { getCurrentInstance, inject } from 'vue-demi';
55

@@ -15,10 +15,10 @@ export const ROOT_STORE_MAP = new WeakMap<
1515
RootStore
1616
>();
1717

18-
export let MODE: 'SPA' | 'SSR' = 'SPA';
18+
export let RENDER_MODE: RenderMode = 'SPA';
1919

20-
export function setMode(mode: 'SPA' | 'SSR') {
21-
MODE = mode;
20+
export function setRenderMode(mode: RenderMode) {
21+
RENDER_MODE = mode;
2222
}
2323

2424
export function getRootStore(): RootStore {

packages/model/src/model/class-type.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
createRestQuery,
1010
} from '../operations';
1111

12-
import { getRootStore, MODE } from '../const';
12+
import { getRootStore, RENDER_MODE } from '../const';
1313
import { computed, reactive, getCurrentInstance, type ComputedRef } from 'vue-demi';
1414
import { type StateStatus, useStatus } from '../operations/status';
1515

@@ -360,7 +360,7 @@ export function createModelFromClass<T>(ctor: Constructor<T>): ModelCotrInfo<T>
360360
});
361361

362362
// 延迟初始化,保证query间依赖
363-
if (queryList.length && MODE !== 'SSR') {
363+
if (queryList.length && RENDER_MODE === 'SPA') {
364364
queryList.forEach(query => query.init());
365365
}
366366

packages/model/src/model/fn-type.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
createRestQuery,
1717
} from '../operations';
1818
import { getCurrentInstance, shallowReactive, toRefs, watch } from 'vue-demi';
19-
import { getRootStore, MODE } from '../const';
19+
import { getRootStore, RENDER_MODE } from '../const';
2020
import { StateStatus, useStatus } from '../operations/status';
2121

2222

@@ -142,7 +142,7 @@ export function createModelFromCA<T>(
142142
const { model, queryList } = fn.creator();
143143

144144
// 延迟初始化,保证query间依赖
145-
if (queryList.length && MODE !== 'SSR') {
145+
if (queryList.length && RENDER_MODE === 'SPA') {
146146
queryList.forEach(query => query.init?.());
147147
}
148148

packages/model/src/model/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
import { createModelFromCA } from './fn-type'
1414
import { createModelFromClass } from './class-type';
1515

16-
import { getRootStore } from '../const';
16+
import { getRootStore, RENDER_MODE } from '../const';
1717

1818
export type * from './types';
1919
import { createModel, type FNModelConstructor } from './fn-type';
@@ -84,7 +84,10 @@ export function useModel<T extends MyCon<any> = MyCon<any>>(ctor: T): RebornInst
8484
}
8585
});
8686
onServerPrefetch(async () => {
87-
await storeModelInstance.instance?.prefetch();
87+
// SSG 不进行请求
88+
if(RENDER_MODE !== 'SSG') {
89+
await storeModelInstance.instance?.prefetch();
90+
}
8891
storeModelInstance.count--;
8992
if (storeModelInstance.count === 0 && storeModelInstance.instance) {
9093
storeModelInstance.instance.destroy();

packages/model/src/operations/core.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { RequestReason, type InfoDataType } from './status';
1212
import { reactive, computed, watch, type ComputedRef } from 'vue-demi';
1313
import { fromWatch } from '../utils';
1414
import { Observable, merge, map } from 'rxjs';
15-
import { MODE } from '../const';
15+
import { RENDER_MODE } from '../const';
1616

1717
export { isDef } from '../utils';
1818

@@ -70,7 +70,7 @@ export function generateQueryOptions<ModelType, DataType, VariablesType>(
7070
let timeout: ReturnType<typeof setTimeout>;
7171

7272
const poll = () => {
73-
if(MODE === 'SSR') {
73+
if(RENDER_MODE !== 'SPA') {
7474
return
7575
}
7676
clearTimeout(timeout);

packages/model/src/store/index.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import type { Ref } from 'vue-demi';
44
import type { Client, RebornClient } from '../clients';
55

66
import { ref } from 'vue-demi';
7-
import { INJECT_KEY, ROOT_STORE_MAP, setMode } from '../const';
7+
import { INJECT_KEY, ROOT_STORE_MAP, setRenderMode } from '../const';
8+
9+
export type RenderMode = 'SPA' | 'SSR' | 'SSG';
810

911
export type GetModelInstance = ReturnType<typeof storeFactory>['getModelInstance'];
1012

@@ -16,7 +18,9 @@ export type HydrationStatus = Ref<0 | 1 | 2>;
1618
export function storeFactory() {
1719
const modelMap = new Map<ModelInfo<any>['constructor'], ModelInfo<any>>();
1820

19-
function getModelInstance<T>(constructor: ModelInfo<T>['constructor']): RebornInstanceType<typeof constructor> | null {
21+
function getModelInstance<T>(
22+
constructor: ModelInfo<T>['constructor'],
23+
): RebornInstanceType<typeof constructor> | null {
2024
return modelMap.get(constructor)?.instance?.model as unknown as RebornInstanceType<typeof constructor>;
2125
}
2226

@@ -75,7 +79,7 @@ export function createStore() {
7579
}
7680

7781
// 为了适配 vue 不同版本这里只能用 any 了
78-
function install(app: any, ssrMode: boolean = false) {
82+
function install(app: any, renderMode: boolean | RenderMode = false) {
7983
// vue3
8084
if (app.config && typeof app.config.globalProperties === 'object') {
8185
app.config.globalProperties.rebornStore = store;
@@ -85,7 +89,7 @@ export function createStore() {
8589
store,
8690
rebornClient,
8791
});
88-
// vue2.7
92+
// vue2.7
8993
} else {
9094
app.mixin({
9195
provide(this) {
@@ -94,7 +98,7 @@ export function createStore() {
9498
[INJECT_KEY]: {
9599
store,
96100
rebornClient,
97-
}
101+
},
98102
};
99103
}
100104
},
@@ -104,13 +108,13 @@ export function createStore() {
104108
ROOT_STORE_MAP.set(this, {
105109
store,
106110
rebornClient,
107-
})
111+
});
108112
}
109-
}
113+
},
110114
});
111115
}
112116

113-
setMode(ssrMode ? 'SSR' : 'SPA');
117+
setRenderMode(typeof renderMode === 'string' ? renderMode : renderMode ? 'SSR' : 'SPA');
114118
}
115119

116120
const result = {

0 commit comments

Comments
 (0)