Access pathname in getServerSideProps? #36143
Unanswered
wereHamster
asked this question in
Help
Replies: 1 comment 9 replies
|
Hi, Since // pages/posts/[...slug].tsx
import path from "path";
const Foo = () => <div>Foo</div>;
export default Foo;
export const getServerSideProps = () => {
console.log("__filename", path.basename(__filename));
return { props: {} };
};
Alternatively, to keep the actual extension: // pages/posts/[...slug].tsx
import path from "path";
const Foo = () => <div>Foo</div>;
export default Foo;
export const getServerSideProps = () => {
const __filename = new URL("", import.meta.url).pathname;
console.log("__filename", path.basename(__filename));
return { props: {} };
};If you want to see the The second approach might be more resilient in an ES modules era though. You could also derive this info from { slug: [ 'a', 'b', 'c' ] }But I guess that'd be very complex code, compared to just using the Node.js API. |
9 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Is there a way to get the pathname inside getServerSideProps? I see resolvedUrl (and url) but those include the interpolated params. I'd like to have the pathname as provided by next/router, eg
blog/[post].All reactions