Skip to content

Latest commit

 

History

History
126 lines (86 loc) · 3.69 KB

File metadata and controls

126 lines (86 loc) · 3.69 KB

Contentful Logo

Contentful Personalization & Analytics

Node SDK SSR + Web SDK Vanilla JS Reference Implementation

Warning

The Optimization SDK Suite is pre-release (alpha). Breaking changes may be published at any time.

This is a reference implementation using both the Optimization Node SDK and Optimization Web SDK, and is part of the Contentful Optimization SDK Suite.

On the server side, the stateless Node SDK is created once at module load and each request passes its request-scoped options directly to stateless event methods.

The goal of this reference implementation is to illustrate the usage of cookie-based communication in both the Node and Web SDKs, which is an important component of many server-side/client-side hybrid SSR and ESR solutions.

Note

This hybrid architecture allows more cache flexibility when personalization is deferred to browser code. If the server embeds personalized output or profile-derived values, treat that response as personalized and avoid shared caching unless you vary on all relevant inputs.

Setup

All steps should be run from the monorepo root.

  1. Install pnpm packages:

    pnpm install
  2. Build the local package tarballs consumed by implementations:

    pnpm build:pkgs
  3. Install this implementation so its local @contentful/* dependencies resolve from pkgs/:

    pnpm implementation:run -- node-sdk+web-sdk implementation:install
  4. Configure the environment in a .env file in implementations/node-sdk+web-sdk based on the .env.example included file. The file is pre-populated with values that are valid only against the mock server implementation. To test the implementation against a live server environment, see the mocks package for information on how to set up Contentful space with test data.

  5. Start the mock API and application servers:

    pnpm --dir implementations/node-sdk+web-sdk --ignore-workspace serve
  6. The application can be accessed via Web browser at http://localhost:3000

  7. Stop the mock API and application servers:

    pnpm --dir implementations/node-sdk+web-sdk --ignore-workspace serve:stop

See implementations/node-sdk+web-sdk/package.json for more commands.

Running From Root Scripts

You can run this implementation from the monorepo root via the root package.json implementation scripts.

  1. Start servers:

    pnpm run implementation:node-sdk+web-sdk -- serve
  2. Stop servers:

    pnpm run implementation:node-sdk+web-sdk -- serve:stop
  3. Run E2E:

    pnpm run implementation:node-sdk+web-sdk -- test:e2e

Running E2E Tests

E2E tests are run using Playwright.

  1. Install Playwright dependencies:

    pnpm --dir implementations/node-sdk+web-sdk --ignore-workspace exec playwright install --with-deps
  2. Run the E2E test suite:

    pnpm --dir implementations/node-sdk+web-sdk --ignore-workspace test:e2e

    The tests can alternatively be run using Playwright's GUI:

    pnpm --dir implementations/node-sdk+web-sdk --ignore-workspace test:e2e:ui