# How to set \`returnTo\` for SSR for a dynamic route?

**URL:** <https://community.auth0.com/t/how-to-set-returnto-for-ssr-for-a-dynamic-route/77755>\
**Category:** Get Help\
**Tags:** nextjs\
**Created:** [February 6, 2022, 6:09am UTC](https://community.auth0.com/t/how-to-set-returnto-for-ssr-for-a-dynamic-route/77755 "2022-02-06T06:09:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![marclemagne](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/marclemagne/32/34995_2.png) [@marclemagne](https://community.auth0.com/u/marclemagne)\
**Post date:** [February 6, 2022, 6:09am UTC](https://community.auth0.com/t/how-to-set-returnto-for-ssr-for-a-dynamic-route/77755/1 "2022-02-06T06:09:30Z")

</div>

Please include the following information in your post:

- **Which SDK this is regarding:** @auth0/nextjs-auth0
- **SDK Version:** 1.7.0
- **Platform Version:** Node v17.1.0 (on macOS 12.2)
- **Code Snippets/Error Messages/Supporting Details/Screenshots:**

**Is this a feature request or bug report?** No.

I am following the instructions referenced [here](https://auth0.github.io/nextjs-auth0/modules/helpers_with_page_auth_required.html#withpageauthrequiredoptions) for an SSR protected page.

I am using this snippet with a fresh/clean install of NextJS (version 12.0.10):

```auto
export const getServerSideProps = withPageAuthRequired({
  async getServerSideProps(ctx) {
    console.log("query", ctx.query);
    // This was in the documentation but doesn't seem to be needed:
    // https://auth0.github.io/nextjs-auth0/modules/helpers_with_page_auth_required.html#withpageauthrequiredoptions
    //
    // const session = getSession(ctx.req, ctx.res);
    return { props: { customProp: "bar" } };
  },
});

```

I am trying to link to a route `/test/[id].tsx`

This functions correctly when I omit `returnTo` but it changes the URL to: `http://localhost:3000/test/something?id=something` instead of `http://localhost:3000/test/something`

If I set the `returnTo` value to `/test/something` the URL is fine.

Is there a way to set the `returnTo` dynamically? Or am I doing something incorrect? Or is it just something I will have to live with?

Thanks a lot!  
Marc

---

<div class="post-metadata">

**Author:** ![marclemagne](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/marclemagne/32/34995_2.png) [@marclemagne](https://community.auth0.com/u/marclemagne)\
**Post date:** [February 21, 2022, 7:42am UTC](https://community.auth0.com/t/how-to-set-returnto-for-ssr-for-a-dynamic-route/77755/2 "2022-02-21T07:42:07Z")

</div>

Based on the [README](https://github.com/auth0/nextjs-auth0#base-path-and-internationalized-routing) for `nextjs-auth0` repository, it seems like it’s possible to do something like this, although I’m not sure it is recommended (just to get a prettier URL):

```auto
const getReturnTo = (
  ctx: GetServerSidePropsContext<ParsedUrlQuery, PreviewData>
) => {
  // Lots of better/different ways to do this
  const [path] = ctx.resolvedUrl.split("?");

  return path;
};

export const getServerSideProps: GetServerSideProps = (ctx) => {
  const returnTo = getReturnTo(ctx);

  return withPageAuthRequired<{ customProp: string }>({
    returnTo,
    async getServerSideProps(ctx) {
      return { props: { customProp: "bar" } };
    },
  })(ctx);
};

```

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [February 21, 2022, 11:48am UTC](https://community.auth0.com/t/how-to-set-returnto-for-ssr-for-a-dynamic-route/77755/3 "2022-02-21T11:48:13Z")

</div>

Hey there marclemagne!

I guess in this case it will be worth discussing with the SDK maintainer through a GitHub issue so they can share their thoughts. Would you be able to create one and share it here so we can ping them? Thanks!

---

<div class="post-metadata">

**Author:** ![marclemagne](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/marclemagne/32/34995_2.png) [@marclemagne](https://community.auth0.com/u/marclemagne)\
**Post date:** [February 21, 2022, 8:35pm UTC](https://community.auth0.com/t/how-to-set-returnto-for-ssr-for-a-dynamic-route/77755/4 "2022-02-21T20:35:36Z")

</div>

Created this [issue](https://github.com/auth0/nextjs-auth0/issues/599). Thanks.

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [February 22, 2022, 11:14am UTC](https://community.auth0.com/t/how-to-set-returnto-for-ssr-for-a-dynamic-route/77755/5 "2022-02-22T11:14:56Z")

</div>

Perfect! I’ll ping them in a minute!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [March 9, 2022, 11:15am UTC](https://community.auth0.com/t/how-to-set-returnto-for-ssr-for-a-dynamic-route/77755/6 "2022-03-09T11:15:34Z")

</div>

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.
