From c1fabbb7cf0b754afdfbb25401b039e9365acc9e Mon Sep 17 00:00:00 2001 From: Repraance Date: Thu, 28 Dec 2023 20:54:56 +0800 Subject: [PATCH] feat: always inject runtimeConfig to html (#575) --- .../src/shared/helper/getAppData.ts | 3 +- .../features/html-render/lib/renderer/spa.ts | 5 ++- .../features/html-render/lib/renderer/ssr.ts | 4 +-- test/e2e/runtime-config.test.ts | 31 +++++++++++++++---- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/packages/platform-shared/src/shared/helper/getAppData.ts b/packages/platform-shared/src/shared/helper/getAppData.ts index f9fd62b8b..dc4cfb8f8 100644 --- a/packages/platform-shared/src/shared/helper/getAppData.ts +++ b/packages/platform-shared/src/shared/helper/getAppData.ts @@ -40,7 +40,8 @@ export function getAppData(): IAppData { ssr: false, pageData: {}, filesByRoutId: {}, - publicPath: '/' + publicPath: '/', + runtimeConfig: {} }; } diff --git a/packages/platform-web/src/node/features/html-render/lib/renderer/spa.ts b/packages/platform-web/src/node/features/html-render/lib/renderer/spa.ts index ef3aee8f1..1b48b54ce 100644 --- a/packages/platform-web/src/node/features/html-render/lib/renderer/spa.ts +++ b/packages/platform-web/src/node/features/html-render/lib/renderer/spa.ts @@ -1,3 +1,4 @@ +import { getPublicRuntimeConfig } from '@shuvi/platform-shared/shared/shuvi-singleton-runtimeConfig'; import { BaseRenderer, AppData } from './base'; import { IRenderViewOptions, IHtmlDocument } from './types'; @@ -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], diff --git a/packages/platform-web/src/node/features/html-render/lib/renderer/ssr.ts b/packages/platform-web/src/node/features/html-render/lib/renderer/ssr.ts index 421a5531e..52c27d55d 100644 --- a/packages/platform-web/src/node/features/html-render/lib/renderer/ssr.ts +++ b/packages/platform-web/src/node/features/html-render/lib/renderer/ssr.ts @@ -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 }, diff --git a/test/e2e/runtime-config.test.ts b/test/e2e/runtime-config.test.ts index 291782a7b..ecb5d8c0d 100644 --- a/test/e2e/runtime-config.test.ts +++ b/test/e2e/runtime-config.test.ts @@ -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(() => { @@ -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();