1

I wanted to try out the redirects module from SXA though something is quite not right. I created the redirect maps on the Site and republished the Site item as I’ve read in an existing question here to reset the caching but my site still wouldn’t honor my redirect rules.

I also opened the debug for redirects and I found these:

sitecore-jss:redirects redirects middleware start: { pathname: ‘/sample-source’, language: ‘en’, hostname: ‘localhost’ }
sitecore-jss:redirects redirects middleware end in 1ms: { redirected: false, status: 302, url: ‘’, headers: { location: ‘http://localhost:3000/sample-target’,set-cookie:’sc_site=TSTWebsite; Path=/‘ }}

I needed help in figuring out what else I need to do because as I understand this log correctly, it was able to see my redirect map and find the right match between my source and target, but it’s just not redirected.

EDIT: Here’s what my query looked like:

{
 "data": {
  "site": {
   "siteInfo": {
    "redirects": [
     {
       "redirectType": "REDIRECT_302",
      "isQueryStringPreserved": false,
      "target": "/sample-target",
      "pattern": "/sample-source/",
    “locale”: “”
     }
    ]
   }
  }
 }
}
2
  • Can you add more detail? What does your redirect map look like? If you run this query against Experience Edge or your preview edge end point what do you get? doc.sitecore.com/xmc/en/developers/xm-cloud/…
    – Richard Seal
    Commented Dec 22, 2023 at 16:36
  • @RichardSeal I have added the result of the query
    – Lala
    Commented Dec 22, 2023 at 23:52

1 Answer 1

-1

If you are using your local instance (HTTP://localhost:3000) to test the redirects make sure you have "disabled" parameter set to false in the redirect.ts

import { NextRequest, NextResponse } from 'next/server';
import { RedirectsMiddleware } from '@sitecore-jss/sitecore-jss-nextjs/middleware';
import { MiddlewarePlugin } from '..';
import { siteResolver } from 'lib/site-resolver';
import clientFactory from 'lib/graphql-client-factory';

class RedirectsPlugin implements MiddlewarePlugin {
  private redirectsMiddleware: RedirectsMiddleware;
  order = 0;

  constructor() {
    this.redirectsMiddleware = new RedirectsMiddleware({
      // Client factory implementation
      clientFactory,
      // These are all the locales you support in your application.
      // These should match those in your next.config.js (i18n.locales).
      locales: ['en'],
      // This function determines if a route should be excluded from RedirectsMiddleware.
      // Certain paths are ignored by default (e.g. Next.js API routes), but you may wish to exclude more.
      // This is an important performance consideration since Next.js Edge middleware runs on every request.
      excludeRoute: () => false,
      // This function determines if the middleware should be turned off.
      // By default it is disabled while in development mode.
      disabled: () => false,
      // Site resolver implementation
      siteResolver,
    });
  }

  /**
   * exec async method - to find coincidence in url.pathname and redirects of site
   * @param req<NextRequest>
   * @returns Promise<NextResponse>
   */
  async exec(req: NextRequest, res?: NextResponse): Promise<NextResponse> {
    return this.redirectsMiddleware.getHandler()(req, res);
  }
}

export const redirectsPlugin = new RedirectsPlugin();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.