Commit c7712014 authored by 郭志伟's avatar 郭志伟

fix: apollo问题修复

parent f13ba138
...@@ -9,15 +9,14 @@ export default class ActivityController extends Controller { ...@@ -9,15 +9,14 @@ export default class ActivityController extends Controller {
ctx.logger.info('请求redis成功 key: %j', redisKey); ctx.logger.info('请求redis成功 key: %j', redisKey);
} }
if (!body) { if (!body) {
body = await ctx.renderView('activity.js', { url: ctx.url }); let apollo = {};
// if (process.env.NODE_ENV === 'production') {
const { api, qiniu } = ctx.app.config.apollo || {};
apollo = { ...api, ...qiniu };
// }
body = await ctx.renderView('activity.js', { url: ctx.url, apollo });
await ctx.service.redis.set(redisKey, body); await ctx.service.redis.set(redisKey, body);
} }
if (process.env.NODE_ENV === 'production') {
const { api, qiniu } = await import(path.resolve('./config/apollo.json'));
const firstScriptTagIndex = body.indexOf('<script');
body = `${body.slice(0, firstScriptTagIndex)}<script>var apollo = ${JSON.stringify({...api, ...qiniu} )};</script>${body.slice(firstScriptTagIndex, body.length - 1)}`;
}
ctx.logger.info('body: %j', body);
ctx.body = body; ctx.body = body;
} }
} }
\ No newline at end of file
...@@ -70,14 +70,13 @@ export default class App { ...@@ -70,14 +70,13 @@ export default class App {
server() { server() {
return context => { return context => {
// console.log('server', context.state);
const vm = this.create(context.state); const vm = this.create(context.state);
const { store, router } = vm; const { store, router } = vm;
router.push(context.state.url); router.push(context.state.url);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
router.onReady(() => { router.onReady(() => {
this.fetch(vm).then(() => { this.fetch(vm).then(() => {
context.state = store.state; context.state = {...store.state, apollo: context.state.apollo};
return resolve(new Vue(vm)); return resolve(new Vue(vm));
}); });
}); });
......
...@@ -23,7 +23,7 @@ export default class EditorModule implements Module<EditorState, RootState> { ...@@ -23,7 +23,7 @@ export default class EditorModule implements Module<EditorState, RootState> {
const [res] = await api.getPageById(condition); const [res] = await api.getPageById(condition);
let { page, ...rest } = res as PageInfo; let { page, ...rest } = res as PageInfo;
if (!page) { if (!page) {
page = "[]"; page = '[]';
} }
commit(SET_PAGE_INFO, { ...rest, page: JSON.parse(page as string) }); commit(SET_PAGE_INFO, { ...rest, page: JSON.parse(page as string) });
} }
......
import { Application, EggAppConfig } from 'egg'; import { Application, EggAppConfig } from 'egg';
const path = require('path'); const path = require('path');
export default (appInfo: EggAppConfig) => { export default (appInfo: EggAppConfig) => {
const exports: any = {}; const exports: any = {};
const { redis } = require(path.resolve('./config/apollo.json')); const { redis } = require(path.resolve('./config/apollo.json'));
......
declare module "*.json" {
const value: any;
export default value;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment