Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Personalize middleware fails after a site is renamed #2118

@jflheureux

Description

@jflheureux

Describe the Bug

When a headless site using JSS is renamed, the personalize middleware plugin fails on subsequent page loads. This failure occurs because the cookie sc_site still holds the old site name. The middleware attempts to resolve the site using this outdated name and throws an exception stating it cannot resolve the site.

To Reproduce

  1. Deploy a headless site using JSS.
  2. Access the site and confirm the sc_site cookie is set with the current site name.
  3. Rename the site in Sitecore.
  4. Redeploy the site after renaming.
  5. Attempt to load a page on the renamed site.
  6. The Personalize middleware throws an exception. HTTP 500 server error is displayed.

Here are Vercel logs:

Personalize middleware failed:
[Error: Could not resolve site for name oldname]
⨯ Error: Could not resolve site for name oldname
    at SiteResolver.getByName (/var/task/headapps/nextjs-starter/node_modules/@sitecore-jss/sitecore-jss/dist/cjs/site/site-resolver.js:38:23)
    at i.exec (/var/task/headapps/nextjs-starter/.next/server/chunks/687.js:402:3474)
    at /var/task/headapps/nextjs-starter/.next/server/chunks/687.js:401:18584
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async /var/task/headapps/nextjs-starter/.next/server/chunks/687.js:401:18561
    at async /var/task/headapps/nextjs-starter/.next/server/chunks/687.js:401:18561
    at async /var/task/headapps/nextjs-starter/.next/server/chunks/687.js:401:18561
    at async /var/task/headapps/nextjs-starter/.next/server/chunks/687.js:401:18561
    at async /var/task/headapps/nextjs-starter/.next/server/chunks/687.js:401:18561
    at async /var/task/headapps/nextjs-starter/.next/server/chunks/687.js:401:18561 {
  page: '/en/_site_navigator-ft/about-us'
}

Expected Behavior

The middleware should correctly resolve the site and not throw an exception.

Possible Fix

Wrap the getSite function site resolution logic from the cookie in a try-catch block. If the resolution by cookie fails, fall back to using the host header to resolve the site name. This ensures that the site is resolved all the time. The site name will probably be updated in the cookie once the page load is successful.

Provide environment information

  • Sitecore Version: XM Cloud
  • JSS Version: 22.4.1
  • Browser Name and version: Google Chrome v138.0.7204.158
  • Operating System and version (desktop or mobile): Windows 11 Pro 24H2 (desktop)

Metadata

Metadata

Assignees

No one assigned

    Labels

    backlogIssue/PR/discussion is reviewed and added to backlog for the further work🐞 bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions