Goal
This post puts together the pieces for rendering a AEM SPA Page Server Side, running a NodeJS Express Server locally. For more on this check documentation
Solution
1) Create the project with flag -DenableSSR=y
mvn -B org.apache.maven.plugins:maven-archetype-plugin:3.2.1:generate -D archetypeGroupId=com.adobe.aem
-D archetypeArtifactId=aem-project-archetype -D archetypeVersion=36 -D aemVersion="cloud"
-D appTitle="Experience AEM React SSR Sample" -D appId="eaem-sites-react-spa-ssr" -D groupId="apps.experienceaem.sites"
-D frontendModule=react -D includeExamples=n -D includeDispatcherConfig=n -DenableSSR=y
2) For this sample removed all additional components except the Text and Page components
3) Build and install the package to local AEM using cmd mvn -PautoInstallPackage clean install (make sure you have maven > 3.6.0), assuming you have node and npm installed for building the ui.frontend module
4) At this point you should be able to see the client side rendered SPA page http://localhost:4502/content/eaem-sites-react-spa-ssr/us/en/home.html
5) Build and Run the Local Express Server
eaem-sites-react-spa-ssr\ui.frontend> npm run build-with-express
eaem-sites-react-spa-ssr\ui.frontend> npm run start-ssr-express
<!-- this starts the SSR rendering chain !-->
<div id="spa-root" data-sly-resource="${resource @ resourceType='cq/remote/content/renderer/request/handler'}"></div>
const renderApp = () => {
ModelManager.initialize(modelManagerOptions).then(pageModel => {
const history = createBrowserHistory();
render(
<Router history={history}>
<App... />
</Router>,
document.getElementById('spa-root')
);
});
};
const hydrateApp = (initialState) => {
modelManagerOptions.model = initialState.rootModel;
ModelManager.initialize(modelManagerOptions).then(pageModel => {
const history = createBrowserHistory();
hydrate(
<Router history={history}>
<App.../>
</Router>,
document.getElementById('spa-root')
);
});
}
No comments:
Post a Comment