Skip to content

Commit 546932a

Browse files
committed
feat: 🎸 支持 ssg 模式
1 parent bd5b8a0 commit 546932a

File tree

7 files changed

+18
-13
lines changed

7 files changed

+18
-13
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export function clientFactory<ClientType extends 'GQL'| 'REST'>(
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 (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 = 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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ export const ROOT_STORE_MAP = new WeakMap<
1515
RootStore
1616
>();
1717

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

20-
export function setMode(mode: 'SPA' | 'SSR') {
20+
export type Mode = 'SPA' | 'SSR' | 'SSG';
21+
22+
export function setMode(mode: Mode) {
2123
MODE = mode;
2224
}
2325

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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 && MODE === 'SPA') {
364364
queryList.forEach(query => query.init());
365365
}
366366

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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 && 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, 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(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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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(MODE !== 'SPA') {
7474
return
7575
}
7676
clearTimeout(timeout);

packages/model/src/store/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ 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, setMode, type Mode } from '../const';
88

99
export type GetModelInstance = ReturnType<typeof storeFactory>['getModelInstance'];
1010

@@ -75,7 +75,7 @@ export function createStore() {
7575
}
7676

7777
// 为了适配 vue 不同版本这里只能用 any 了
78-
function install(app: any, ssrMode: boolean = false) {
78+
function install(app: any, ssrMode: boolean | Mode = false) {
7979
// vue3
8080
if (app.config && typeof app.config.globalProperties === 'object') {
8181
app.config.globalProperties.rebornStore = store;
@@ -110,7 +110,7 @@ export function createStore() {
110110
});
111111
}
112112

113-
setMode(ssrMode ? 'SSR' : 'SPA');
113+
setMode(typeof ssrMode === 'string' ? ssrMode : ssrMode ? 'SSR' : 'SPA');
114114
}
115115

116116
const result = {

0 commit comments

Comments
 (0)