Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Follow up - Engage upgrade #1633

Merged
merged 6 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ Our versioning strategy is as follows:
* `[templates/nextjs-sxa]` Fix shown horizontal scrollbar in EE mode. ([#1625](https://github.com/Sitecore/jss/pull/1625)), ([#1626](https://github.com/Sitecore/jss/pull/1626))
* `[sitecore-jss-nextjs]` Fix issue when redirects works each every other times. ([#1629](https://github.com/Sitecore/jss/pull/1629))

## 21.5.0

### 🐛 Bug Fixes

* `[templates/nextjs-personalize]` Fix cookie domain for localhost ([#1609](https://github.com/Sitecore/jss/pull/1609))

### 🛠 Breaking Changes

* `[templates/nextjs]` `[sitecore-jss-nextjs]` Upgrade of @sitecore/engage to 1.4.0, now this dependency has been added as a peer dependency to @sitecore-jss-nextjs package in order to replace the existing CDP service in the @sitecore-jss/sitecore-jss with the new functions/features introduced in the engage SDK. ([#1609](https://github.com/Sitecore/jss/pull/1609)) ([#1633](https://github.com/Sitecore/jss/pull/1633))

## 21.4.0

### 🎉 New Features & Improvements
Expand Down Expand Up @@ -64,14 +74,9 @@ Our versioning strategy is as follows:
* `[templates/nextjs-sxa]` Don't let Image component wrap <img> with <a> tag when TargetUrl is not configured. ([#1593](https://github.com/Sitecore/jss/issues/1593))
* `[templates/nextjs]` Next config header plugin for CORS. ([#1597](https://github.com/Sitecore/jss/pull/1597))
* `[templates/nextjs]` Ensure dictionary data is only fetched when layout data is present for a route ([#1608](https://github.com/Sitecore/jss/pull/1608))
* `[templates/nextjs-personalize]` Fix cookie domain for localhost ([#1609](https://github.com/Sitecore/jss/pull/1609))
* `[sitecore-jss-react-forms]` Form should be blocked while submit is in progress to avoid submit spam ([#1611](https://github.com/Sitecore/jss/pull/1611))
* `[templates/nextjs]` Fix linting errors, fix type error by upgrading @react/types to v18.2.22 ([#1613](https://github.com/Sitecore/jss/pull/1613))

### 🛠 Breaking Changes

* `[templates/nextjs]` `[sitecore-jss-nextjs]` Upgrade @sitecore/engage to 1.4.0 also, this dependency has been added to @sitecore-jss-nextjs package to make use of the latest features from the SDK. ([#1609](https://github.com/Sitecore/jss/pull/1609))

## 21.3.1

### 🐛 Bug Fixes
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"dependencies": {
"@sitecore/engage": "^1.4.0"
"@sitecore/engage": "^1.4.1"
}
}
4 changes: 2 additions & 2 deletions packages/sitecore-jss-nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"url": "https://github.com/sitecore/jss/issues"
},
"devDependencies": {
"@sitecore/engage": "^1.4.0",
"@sitecore/engage": "^1.4.1",
"@types/chai": "^4.3.4",
"@types/chai-as-promised": "^7.1.5",
"@types/chai-string": "^1.4.2",
Expand Down Expand Up @@ -66,7 +66,7 @@
"typescript": "~4.9.4"
},
"peerDependencies": {
"@sitecore/engage": "^1.4.0",
"@sitecore/engage": "^1.4.1",
"next": "^13.4.16",
"react": "^18.2.0",
"react-dom": "^18.2.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,18 @@ export class PersonalizeMiddleware extends MiddlewareBase {
};
}

protected initializeEngageServer(site: SiteInfo, language: string): EngageServer {
protected initializeEngageServer(
hostName: string,
site: SiteInfo,
language: string
): EngageServer {
const engageServer = initServer({
clientKey: this.config.cdpConfig.clientKey,
targetURL: this.config.cdpConfig.endpoint,
pointOfSale: this.config.getPointOfSale
? this.config.getPointOfSale(site, language)
: PosResolver.resolve(site, language),
cookieDomain: hostName,
forceServerCookieMode: true,
});

Expand Down Expand Up @@ -171,16 +176,6 @@ export class PersonalizeMiddleware extends MiddlewareBase {
}

const site = this.getSite(req, response);
const engageServer = this.initializeEngageServer(site, language);

// creates the browser ID cookie on the server side
// and includes the cookie in the response header
try {
await engageServer.handleCookie(req, response, timeout);
} catch (error) {
debug.personalize('skipped (browser id generation failed)');
throw error;
}

// Get personalization info from Experience Edge
const personalizeInfo = await this.personalizeService.getPersonalizeInfo(
Expand All @@ -199,6 +194,16 @@ export class PersonalizeMiddleware extends MiddlewareBase {
return response;
}

const engageServer = this.initializeEngageServer(hostname, site, language);

// creates the browser ID cookie on the server side
// and includes the cookie in the response header
try {
await engageServer.handleCookie(req, response, timeout);
} catch (error) {
debug.personalize('skipped (browser id generation failed)');
throw error;
}
const params = this.getExperienceParams(req);

debug.personalize('executing experience for %s %s %o', personalizeInfo.contentId, params);
Expand Down Expand Up @@ -240,18 +245,24 @@ export class PersonalizeMiddleware extends MiddlewareBase {
const rewritePath = getPersonalizedRewrite(basePath, { variantId });
// Note an absolute URL is required: https://nextjs.org/docs/messages/middleware-relative-urls
const rewriteUrl = req.nextUrl.clone();

rewriteUrl.pathname = rewritePath;
response = NextResponse.rewrite(rewriteUrl);
response = NextResponse.rewrite(rewriteUrl, response);

const setCookieHeader = response.headers.get('set-cookie');

// Disable preflight caching to force revalidation on client-side navigation (personalization may be influenced)
// See https://github.com/vercel/next.js/issues/32727
response.headers.set('x-middleware-cache', 'no-cache');
// Share rewrite path with following executed middlewares
// Share rewrite path with following executed middleware
response.headers.set('x-sc-rewrite', rewritePath);

// Share site name with the following executed middlewares
response.cookies.set(this.SITE_SYMBOL, site.name);

if (setCookieHeader) {
response.headers.set('set-cookie', setCookieHeader);
addy-pathania marked this conversation as resolved.
Show resolved Hide resolved
}

debug.personalize('personalize middleware end in %dms: %o', Date.now() - startTimestamp, {
rewritePath,
headers: this.extractDebugHeaders(response.headers),
Expand Down
12 changes: 6 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6606,7 +6606,7 @@ __metadata:
"@sitecore-jss/sitecore-jss": 21.6.0-canary.4
"@sitecore-jss/sitecore-jss-dev-tools": 21.6.0-canary.4
"@sitecore-jss/sitecore-jss-react": 21.6.0-canary.4
"@sitecore/engage": ^1.4.0
"@sitecore/engage": ^1.4.1
"@types/chai": ^4.3.4
"@types/chai-as-promised": ^7.1.5
"@types/chai-string": ^1.4.2
Expand Down Expand Up @@ -6646,7 +6646,7 @@ __metadata:
ts-node: ^10.9.1
typescript: ~4.9.4
peerDependencies:
"@sitecore/engage": ^1.4.0
"@sitecore/engage": ^1.4.1
next: ^13.4.16
react: ^18.2.0
react-dom: ^18.2.0
Expand Down Expand Up @@ -6887,10 +6887,10 @@ __metadata:
languageName: unknown
linkType: soft

"@sitecore/engage@npm:^1.4.0":
version: 1.4.0
resolution: "@sitecore/engage@npm:1.4.0"
checksum: f656045dfcb65f63ff44af195c9ae92d375a237bdf91a4638e9d296df2d143837145f13ca033aeabd03323c900ee5c78249d2967c7665368fde82ef2e758ad43
"@sitecore/engage@npm:^1.4.1":
version: 1.4.1
resolution: "@sitecore/engage@npm:1.4.1"
checksum: 582b7a55ba407765def12518114790fb1735c359e60fe4594102a5e643539b148a494b02bf71970bcafc807b92fe19a1ab8ff7c2af3db2c080ab36ec2eb16d57
languageName: node
linkType: hard

Expand Down