Edgio
Edgio

Spartacus for SAP Commerce Cloud (formerly SAP Hybris)

This guide shows you how to deploy Spartacus apps on Layer0.

Connector

This framework has a connector developed for Layer0. See Connectors for more information.

System Requirements

Getting Started

If you don’t already have a Spartacus application, you can create one using:

1. Create a new Angular App

Spartacus 2.x only supports Angular version 9.x Spartacus 3.x only supports Angular version 10.x

Terminal
npm install -g @angular/cli@9
ng new my-layer0-spartacus-app

You should now have a working starter app. Run ng serve to see the application running on localhost:4200.

2. Add Spartacus with SSR

To deploy your Spartacus application on Layer0 it needs to support server-side rendering (SSR). To add SSR support, run:

Terminal
ng add @spartacus/schematics --ssr

Read more about server-side rendering in Spartacus here.

The previous command created:

  • A server-side application module (app.server.module.ts)
  • A bootstrapper for the server app (main.server.ts)
  • server.ts which exports an Express app
  • TypeScript configuration for the server (tsconfig.server.json)

You can now run npm run build:ssr && npm run serve:ssr to access your server-side rendered app at localhost:4000.

To prepare your Spartacus application for deployment on Layer0:

1. Install the Layer0 CLI globally:

Terminal
npm i -g @layer0/cli # yarn global add @layer0/cli
  1. Run the following in the root folder of your project. This will configure your project for Layer0.
Terminal
0 init

This will automatically add all of the required dependencies and files to your project. These include:

  • The @layer0/core package
  • The @layer0/angular package
  • The @layer0/cli package
  • The @layer0/spartacus package
  • The @layer0/prefetch package
  • layer0.config.js- Contains various configuration options for Layer0.
  • routes.js - A default routes file that sends all requests to the Angular Universal server. Update this file to add caching or proxy some URLs to a different origin.
  • The sw folder - Contains the files needed to build the service worker that that provides static asset and API prefetching.

3. Update layer0.config.js

For an app called my-layer0-spartacus-app the Layer0 config file created by 0 init will look like so:

JavaScript
// This file was automatically added by 0 deploy.
// You should commit this file to source control.

module.exports = {
  backends: {
    commerce: {
      domainOrIp: 'api-commerce.my-site.com',
      hostHeader: 'api-commerce.my-site.com',
    },
  },
}

If you have several projects and the defaultProject as specified in angular.json is not the project with the SSR build, specify the correct project with the ANGULAR_PROJECT environment variable. For example: ANGULAR_PROJECT=my-ssr-project 0 build.

4. Update OCC baseUrl endpoint

The baseUrl should be updated to use the remote URL when window is not defined (i.e., for SSR), and the current host when window is defined. For example:

JavaScript
baseUrl: typeof window !== 'undefined'
  ? `${window.location.protocol}//${window.location.host}`
  : 'https://api-commerce.my-site.com'

This value is defined in the backend property of the options parameter to B2cStorefrontModule.withConfig({}) in the app.module.ts file, but is best set using environment variables in the environment.ts and environment.prod.ts files.

Adding prefetching

Upstream request tracking

Prefetching for a Spartacus app can be enabled by listening to upstream requests made when server-side rendering a specific page. @layer0/prefetch library will pick up on the upstream requests made by reading the x-0-upstream-requests response header. An example scenario:

  1. User A lands on /product/1.
  2. /product/1 has not been cached in the edge and thus will be server-side rendered.
  3. The rendering server has been modified to track upstream requests by patching https.request.
  4. The rendering server sets x-0-upstream-requests to, for example: /rest/v2/1;/rest/v2/2;
  5. The HTML response for /product/1 is now cached and for future requests served from the edge along with the x-0-upstream-requests response header.
  6. User B lands on a page that has a link to /product/1. /product/:path* has been configured with cache.browser.spa: true. Because of this configuration, @layer0/prefetch will know to make a prefetch HEAD request for /product/1, and only if product/1 can be served from the edge will it prefetch all requests specified in x-0-upstream-requests response header.
  7. When User B click the link to /product/1, the navigation will be faster since the requests needed to render the new page will be in service worker cache.

Example implementation of upstream request tracking changes required in your server.ts file:

JavaScript
import 'zone.js/dist/zone-node'
import * as express from 'express'
import { join } from 'path'

+ // layer0
+ import * as http from 'http'
+ import * as https from 'https'
+ import createRenderCallback from '@layer0/spartacus/server/createRenderCallback'
+ import installLayer0Middleware from '@layer0/spartacus/server/installLayer0Middleware'


// Express server
const server = express()

+ installLayer0Middleware({ server, http, https });

const PORT = process.env.PORT || 4200
const DIST_FOLDER = join(process.cwd(), 'dist/<your-project-name>')

// * NOTE :: leave this as require() since this file is built Dynamically from webpack
const {
  AppServerModuleNgFactory,
  LAZY_MODULE_MAP,
  ngExpressEngine,
  provideModuleMap,
} = require('./dist/<your-project-name>-server/main')

server.engine(
  'html',
  ngExpressEngine({
    bootstrap: AppServerModuleNgFactory,
    providers: [provideModuleMap(LAZY_MODULE_MAP)],
  }),
)

server.set('view engine', 'html')
server.set('views', DIST_FOLDER)

server.get(
  '*.*',
  express.static(DIST_FOLDER, {
    maxAge: '1y',
  }),
)

// All regular routes use the Universal engine
server.get('*', (req, res) => {
  res.render(
    'index',
    { req },
+   createRenderCallback(res),
  )
})

export default server

Fixing response header overflows

Some CDNs, such as Akamai, impose low limits on the size of response headers. Prefetching works by listing all of the upstream API URLs fetched during SSR in a x-0-upstream-requests response header. If your application makes many upstream requests for each page during SSR, this header can be quite long and exceed the maximum length allowed by your CDN. To mitigate this, using the maxHeaderLength option when calling createRenderCallback:

JavaScript
createRenderCallback(res, { maxHeaderLength: 500 })

Service worker

The build command places the built service-worker.js under dist so @layer0/angular will know to static serve the file.

Installing the service worker and any further prefetching will be handled by @layer0/prefetch by invoking the install function imported from @layer0/prefetch/window/install.

Example implementation in app.component.ts:

JavaScript
import { Component, OnInit, Inject } from '@angular/core'
import { isPlatformBrowser } from '@angular/common'
import { PLATFORM_ID } from '@angular/core'
+ import install from '@layer0/prefetch/window/install'

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
})
export class AppComponent implements OnInit {
  isBrowser: boolean
  title = '<your-project-name>'

  constructor(@Inject(PLATFORM_ID) platformId: Object) {
    this.isBrowser = isPlatformBrowser(platformId)
  }

  ngOnInit() {
+   setTimeout(() => {
+     if (this.isBrowser) {
+       install()
+     }
+   })
  }
}

To avoid Spartacus installing ngsw-worker, set production: false in environment.prod.ts as a temporary workaround:

Diff
pwa: {
- enabled: environment.production
+ enabled: false
},

You may also need to disable it in your app.module.ts file:

Diff
ServiceWorkerModule.register(
  'ngsw-worker.js',
  {
-   enabled: environment.production,
+   enabled: false
  }
),

Add "skipLibCheck": true, to tsconfig.json to avoid type errors from workbox library during build.

Routing and Cache Configuration

The default routes.js file created by 0 init sends all requests to Angular server via a fallback route.

JavaScript
// This file was automatically added by 0 deploy.
// You should commit this file to source control.

import { Router } from '@layer0/core/router'
import { angularRoutes } from '@layer0/angular'

export default new Router()
  // other routes removed
  .use(angularRoutes)

The default router also includes common cache configurations for most Spartacus apps:

JavaScript
  return new Router()
    .match('/rest/v2/:path*', ({ cache, proxy }) => {
      cache({
        browser: {
          maxAgeSeconds: PAGE_TTL,
          serviceWorkerSeconds: PAGE_TTL,
        },
        edge: {
          maxAgeSeconds: PAGE_TTL,
          staleWhileRevalidateSeconds: PAGE_TTL,
        },
      })
      return proxy('commerce')
    })
    .match('/medias/:path*', ({ cache, proxy }) => {
      cache({
        browser: {
          maxAgeSeconds: FAR_FUTURE_TTL,
        },
        edge: {
          maxAgeSeconds: FAR_FUTURE_TTL,
          staleWhileRevalidateSeconds: 60 * 60 * 24,
        },
      })
      return proxy('commerce')
    })
    ...
}

These routes are set up to cache the default API endpoints from SAP Commerce Cloud, but should be configured to suit your application as needed.

Finally, to configure prefetching for your pages, configure the routes that use SSR using the prefetchUpstreamRequests: true flag for the cache function:

JavaScript
const CACHE_SSR_PAGE = {
  prefetchUpstreamRequests: true,
  edge: {
    maxAgeSeconds: PAGE_TTL * 365,
    staleWhileRevalidateSeconds: PAGE_TTL * 365,
    forcePrivateCaching: true,
  },
}

return new Router()
  ...
  .get('/base-site-path/:path*', ({ cache }) => {
    cache(CACHE_SSR_PAGE)
  })
}

Running Locally

To test your app locally, run:

Terminal
0 run

You can do a production build of your app and test it locally using:

Terminal
0 build && 0 run --production

Setting --production runs your app exactly as it will be uploaded to the Layer0 cloud using serverless-offline.

Deploying

Deploying requires an account on Layer0. Sign up here for free. Once you have an account, you can deploy to Layer0 by running the following in the root folder of your project:

Terminal
0 deploy