From 0768ab2806564cc2ee0575b38927db2631b40339 Mon Sep 17 00:00:00 2001 From: Christian Westgaard Date: Tue, 11 Apr 2023 17:41:13 +0200 Subject: [PATCH] Fix #780 When ssr:false don't even try SSR --- .../resources/lib/enonic/react4xp/React4xp.ts | 2 ++ .../react4xp/React4xp/methods/renderBody.ts | 23 +++++++++++++++++-- .../methods/renderWarningPlaceholder.ts | 21 +++++++++++++++++ src/main/resources/types/React4xp.d.ts | 1 + 4 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 src/main/resources/lib/enonic/react4xp/React4xp/methods/renderWarningPlaceholder.ts diff --git a/src/main/resources/lib/enonic/react4xp/React4xp.ts b/src/main/resources/lib/enonic/react4xp/React4xp.ts index e8eb68454..08df68f8d 100644 --- a/src/main/resources/lib/enonic/react4xp/React4xp.ts +++ b/src/main/resources/lib/enonic/react4xp/React4xp.ts @@ -29,6 +29,7 @@ import {renderBody} from './React4xp/methods/renderBody'; import {renderPageContributions} from './React4xp/methods/renderPageContributions'; import {renderSSRIntoContainer} from './React4xp/methods/renderSSRIntoContainer'; import {renderTargetContainer} from './React4xp/methods/renderTargetContainer'; +import {renderWarningPlaceholder} from './React4xp/methods/renderWarningPlaceholder'; import {setHasRegions} from './React4xp/methods/setHasRegions'; import {setId} from './React4xp/methods/setId'; import {setIsPage} from './React4xp/methods/setIsPage'; @@ -225,6 +226,7 @@ export class React4xp< public renderPageContributions = renderPageContributions public renderSSRIntoContainer = renderSSRIntoContainer public renderTargetContainer = renderTargetContainer + public renderWarningPlaceholder = renderWarningPlaceholder public setHasRegions = setHasRegions public setId = setId public setIsPage = setIsPage diff --git a/src/main/resources/lib/enonic/react4xp/React4xp/methods/renderBody.ts b/src/main/resources/lib/enonic/react4xp/React4xp/methods/renderBody.ts index 3b87c976b..e6e50fe9a 100644 --- a/src/main/resources/lib/enonic/react4xp/React4xp/methods/renderBody.ts +++ b/src/main/resources/lib/enonic/react4xp/React4xp/methods/renderBody.ts @@ -1,5 +1,7 @@ -import type {Request} from '../../../../..'; -import type {React4xp} from '../../React4xp'; +import type { Request } from '../../../../..'; +import type { React4xp } from '../../React4xp'; +import type { AppConfig } from '/types/Application.d'; +import { isSet } from '@enonic/js-utils/value/isSet'; import shouldSSR from '/lib/enonic/react4xp/React4xp/shouldSSR'; @@ -13,6 +15,23 @@ export function renderBody(this: React4xp, { request?: Request } = {}): string { // log.debug('renderBody ssr:%s jsxPath:%s', ssr, this.jsxPath); + + // Client-side components, may import assets which are not available + // server-side. To avoid potential SSR errors just show placeholder: + if ( + ( + !request + || !request.mode + || request.mode === 'edit' + ) && ( + isSet(ssr) + ? !ssr + : (app.config as AppConfig)['react4xp.ssr'] === 'false' + ) + ) { + return this.renderWarningPlaceholder(); + } + return shouldSSR({ request, ssr diff --git a/src/main/resources/lib/enonic/react4xp/React4xp/methods/renderWarningPlaceholder.ts b/src/main/resources/lib/enonic/react4xp/React4xp/methods/renderWarningPlaceholder.ts new file mode 100644 index 000000000..9a2d9fe01 --- /dev/null +++ b/src/main/resources/lib/enonic/react4xp/React4xp/methods/renderWarningPlaceholder.ts @@ -0,0 +1,21 @@ +import type { React4xp } from '/types'; + + +export function renderWarningPlaceholder(this: React4xp) { + const {jsxPath, react4xpId} = this; + return `
+ +

+ This React4xp component "${jsxPath}" (with id ${react4xpId}) is configured to be rendered client-side and is thus disabled in Content Studio edit mode. +

+
`; +} diff --git a/src/main/resources/types/React4xp.d.ts b/src/main/resources/types/React4xp.d.ts index d698a2455..081482c75 100644 --- a/src/main/resources/types/React4xp.d.ts +++ b/src/main/resources/types/React4xp.d.ts @@ -64,6 +64,7 @@ export interface Instance< body: string content: string }) => string + renderWarningPlaceholder: () => string setHasRegions: (hasRegions: boolean) => Instance setId: (react4xpId: Id) => Instance setIsPage: (isPage: boolean) => Instance