2

In my nextjs app I have profile pages. I want the routing to be like this:

/@username

That's why I have used this file format:

@[username].js

But this is not working. Is there any other way to accomplish this?

1 Answer 1

3

You'll have to add it under a page something like this:

pages/user/[username].js

Any route like /user/@john, /user/@mark, etc. will be matched by pages/user/[username].js. The matched path parameter will be sent as a query parameter to the page.

You'll have access to the query object as

{ "username": "@john" }

[username].js

export default function User(props) {
  return (
    <div>
      <h1>Username: {props.url.query.username} </h1>
      <pre>{JSON.stringify(props, null, 2)}</pre>
    </div>
  );
}

eg: Codesandbox

Hope this helps !

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.