Skip to content

Commit

Permalink
feat: always inject runtimeConfig to html (#575)
Browse files Browse the repository at this point in the history
  • Loading branch information
Repraance committed Dec 28, 2023
1 parent b4ef652 commit c1fabbb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
3 changes: 2 additions & 1 deletion packages/platform-shared/src/shared/helper/getAppData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export function getAppData(): IAppData {
ssr: false,
pageData: {},
filesByRoutId: {},
publicPath: '/'
publicPath: '/',
runtimeConfig: {}
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getPublicRuntimeConfig } from '@shuvi/platform-shared/shared/shuvi-singleton-runtimeConfig';
import { BaseRenderer, AppData } from './base';
import { IRenderViewOptions, IHtmlDocument } from './types';

Expand All @@ -10,8 +11,10 @@ export class SpaRenderer extends BaseRenderer {
const appData: AppData = {
ssr: false,
pageData: {},
basename
basename,
runtimeConfig: getPublicRuntimeConfig() || {}
};

const document: IHtmlDocument = {
htmlAttrs: {},
headTags: [...assets.styles],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ export class SsrRenderer extends BaseRenderer {
ssr: true,
appState: store.getState(),
pageData,
basename: router.basename
basename: router.basename,
runtimeConfig: getPublicRuntimeConfig() || {}
};
appData.runtimeConfig = getPublicRuntimeConfig() || {};

const document: IHtmlDocument = {
htmlAttrs: { ...result.htmlAttrs },
Expand Down
31 changes: 25 additions & 6 deletions test/e2e/runtime-config.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
import { AppCtx, Page, devFixture, check } from '../utils';
import {
AppCtx,
Page,
devFixture,
check,
buildFixture,
serveFixture,
ShuviConfig
} from '../utils';

let ctx: AppCtx;
let page: Page;

jest.setTimeout(5 * 60 * 1000);

describe('SSR: Runtime Config', () => {
describe.each([false, true])('SSR: Runtime Config', isProd => {
beforeAll(async () => {
ctx = await devFixture('runtime-config');
if (isProd) {
buildFixture('runtime-config');
ctx = await serveFixture('runtime-config');
} else {
ctx = await devFixture('runtime-config');
}
});

beforeEach(() => {
Expand Down Expand Up @@ -54,14 +67,20 @@ describe('SSR: Runtime Config', () => {
});
});

describe('CSR: Runtime Config', () => {
describe.each([false, true])('CSR: Runtime Config', isProd => {
beforeAll(async () => {
ctx = await devFixture('runtime-config', {
const config = {
ssr: false,
router: {
history: 'browser'
}
});
} as ShuviConfig;
if (isProd) {
buildFixture('runtime-config', config);
ctx = await serveFixture('runtime-config');
} else {
ctx = await devFixture('runtime-config', config);
}
});
afterAll(async () => {
await ctx.close();
Expand Down

0 comments on commit c1fabbb

Please sign in to comment.